public class StudentNumber { /* public StudentNumber(){ System.out.println("test"); } */ private char c='W'; public StudentNumber(float i){ System.out.println(i);
[Code] ....
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - c has private access in extention.pkgsuper.StudentNumber at extention.pkgsuper.ExtentionSuper.main
Being new to java I am a bit lost as to why my session attribute for this banking app wont add the deposits to the session var... it just keeps going back to the amount I set it to originally - so if I set the beginning balance to 3000 then deposit 100 it becomes 3100, but if I then try deposit another amount eg. a extra 200 it becomes 3200 not 3300 like it should be !!
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import java.net.*; import java.text.DecimalFormat; public class SessionBank extends HttpServlet
I am getting the above error and am not sure which direction to proceed. In the class, the only way we have discussed getting user input is by System.in.read. I have searched and apparently found better methods for getting user input, but wanted to stick with what has been presented thus far.
Assignment Directions:
1. Create a new class named “valuemethod”
2. Create a new method named “Main”
3. In Main Write the code that will call the method EnterPay and YearlySal
4. Create a new method named “EnterPay”
5. In the EnterPay method Write the code that asks the user to input their hourly wage. Use the formula to calculate their yearly salary: wage * 2040. Return the yearly salary to the main method
6. In the main method write the code that will display this: “Your yearly salary is:
Java Code:
package valuemethod; public class Valuemethod { public static void main(String[] args) //throws java.io.IOException -- moved to EnterPay
The 2 minute drill from page 69 SCJP kathy and bert book, says regarding Interfaces, that - "A legal nonabstract implementing class must not declare any new checked exceptions for an implementation method."
When I try the below given code in eclipse , it does not throw any errors . (Here I have tried to throw NullPointerException from testFunc whereas the interface function throws IllegalStateExc)
package abstracttesting; public class StaticCheck implements check{ public void testFunc() throws NullPointerException{ // TODO Auto-generated method stub } public static void main(String[] args) { // TODO Auto-generated method stub } } interface check{ void testFunc() throws IllegalStateException; }
So I'd like to know what all the statement, not sure if it's the correct way of calling them but I'd like to know what like all of those purple-ish colored words in eclipse actually mean.Here's what I know so far, so if you can add some stuff to it or just correct me:
public - can be accessed by different classes. private - can't be accessed by different classes. static - adds a '.' which pretty much lets you like use methods on it? Not really sure about this one. final - a final value of a variable meaning it couldn't and willn't change? super - I have no clue, maybe something that has to be executed first? Not sure. void - bassicly you don't have to use return as it doesn't return any value. this - Uh-.. I think it has something to do with the class this keyword has been entered into, not quite sure what it does though.
I assume there are alot more but I am just not sure about these common ones, what the actually do and what's their purpose?
I am creating a slot machine using eclipse. I am trying to get the "winnings" JTextField to be updated in a way so that when the random images have been selected it adds to the number that is already displayed in the JTextField as opposed to what it is doing at the minute which is just displaying how much was won on that particular spin. I am also struggling to set a code for when noting is won, nothing is added. My code is below.
I'm working on a project that contains multiple classes. Each class contains and must contain only PRIVATE variables. Here's my issue. When my test code calls for a new instance of "StudentClass" as so:
StudentClass studentClass = new StudentClass(offeredClass.getClassIdNumber(), offeredClass.getClassName(), offeredClass.getClassroom());
The corresponding constructor won't let me initialize it's variables because they are declared private within another class, as shown here:
When getClassName, getClassroom, and getClassIdNumber are passed to a toString() method elsewhere in my test code. the output is returned just fine. When passed through the StudentClass, I'm getting Null across the board.
The first is clear , new Person().printPerson(); displays Person but for the second : new Student().printPerson(); it accesses the Student constructor that points to the Person class => object. It builds the Person instance then goes back to the Student constuctor .Both methods are private and to my knowledge invisible one to the other , except that you cant run the the Person one because it's private so the only one in the Student class is the Student one . Guess it 's incorrect , but why ? (is because private methods cant be overriden and somehow the super class one always has priority ? , even if it's private?)
public class Test { public static void main(String[] args) { new Person().printPerson(); new Student().printPerson();
I have a project for a class where I'm supposed to be something with private arrays and private integers and I still don't understand the point of private anything within java. if I want to change a variable I'll change it. if I want it to stay the same I'll leave it the same. so what point is all this private/public nonsense unless I'm trying to stop hackers or something?
import java.util.Scanner; import java.util.ArrayList; public class Problem1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); ArrayList<String> list = new ArrayList<String>();
[Code] ....
There is an error and says that my ArrayList has private access. I can't figure out how to fix it.
The code runs but when I enter "Quit", the program just stops. The arraylist isn't printed out?
When creating a class with a constructor, why does one have to create private variables (attributes) to be used as parameters by the object? The object's parameters will be set to be exactly equal to the private variables (attributes), so what is the point of having the private variables (attributes) Why are both private variables (attributes) and parameters needed when they are set to be equal each other anyway?
I just recently started learning about encapsulation, how to set variables private and only accessible to other classes through setters() and getters(). I've seen a couple of programming examples, perhaps not enough but some from here, and I sort of built the assumption that by default all variables need to be private. to make things more clear, here's a card dealer I made which simply
1- generates a fulll deck of 52 cards 2- lets user decide how many players, with 5 as Max number allowed as each player is dealt 10 cards. 3- deal cards
I approached this by making A deck , card , player and game class
import java.util.ArrayList; public class Deck { //an Object of this Class would generate a full deck ie an ArrayList of 52 Cards private String[] suits={"Spades","Diamond","Clubs","Hearts"}; private int[] number={1,2,3,4,5,6,7,8,9,10,11,12,13}; ArrayList<Cards> deck= new ArrayList<Cards>();
[code]....
I can understand why for example Deck class's suit and number arrays are set to private , as they only need to be accessed by this class only. however, both the Deck class's deck arraylist and the Player class arraylist are not private, as I need to transfer these values from one to the other and it felt to me that setting them to private would make it more difficult to deal with them, but what I did instead is to set the Game class dealCard(), which is the only method that have access to them as private. does this achieve the same effect or do I need to set both of these arrayList to private?a follow up question, this is more related to the actual card dealer program, in this code
private void dealCards(){
for(int x = 0 ; x < playerCount ; x++){ for(int y = 0 ; y < 10; y++){ playerList.get(x).pile.add(deck.deck.get(0)); deck.deck.remove(0); } } }
is there an API in ArrayList class that moves(adds to receiver and remove from giver) element across ArrayLists?
1) When a variables are declared "Private" How should it be accessed from the driver class ? Sometimes i get an error in driver class saying "your variable is declared Private" why am I getting this error ...
The document says "Private" declared variables should be accessed only through methods. What does that mean.
As below code showing that you cannot directly access the private variable as i understood,
public class EncapsulationDemo{ private int ssn; private String empName; private int empAge; //Getter and Setter methods public int getEmpSSN(){ return ssn; } public String getEmpName(){ return empName;
I have a class named Base and a private variable named _hopcount i have 10 instances of class base i use _hopcount as creteria to some if but other instances edit _hopcount so i want to prevent _hopcount edit by other instances; I want to have private variable which other instances of same class can't modify it.
public class Base extends TypedAtomicActor { private int _hopcount = 0; if(_hopcount <= 3) { some code; } public function() { _hopCount += 1; } }
I have problems with private static void print section, something is missing? And in case 4, I want it to stop the program(end the loop) but it keeps going.
import java.util.Scanner; import java.util.ArrayList; public class Dogregister16 { public static ArrayList<Dog> dogregister = new ArrayList(); private static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { initiate();
[Code] ....
case 4: System.exit(0); System.out.println("Exit program"); } } } }