that i started to learn programming and i started with java. so there is a book that i'm on it right now called "Pearson Absolute Java 5th Edition" by Walter Savitch anyway i'm on a project in fifth season which i have to create a class named HotDogStand that operates several hotdog stands distributed throughout town. the whole program is clear. although its so easy to accomplish , my question is more about debugging. so here is the code:
public class HotDogStand
{
private int id; //id number of hotdog stand
private int hotDogsPerDay; //hotdogs sold by one stand
private static int totalHotDogs; //the static value for total hotdogs sold by all the stands
public HotDogStand (int newID, int hotDogsPerDay) {
this.id = newID;
this.hotDogsPerDay = hotDogsPerDay;
totalHotDogs += hotDogsPerDay; //to add the value every time the user uses the constructor (every time the user creates an object)
[code]....
everything works fine. but my question is what if someone use a constructor again? you see if in the main method someone do this after creating the object "stand3":
HotDogStand stand3 = new HotDogStand(3, 43);
stand3 = new HotDogStand(3,40);
i know that it's logical to use setter method but this program doesn't have one. if someone do the thing i wrote above, the calculation of static variable, totalHotDogs will be all wrong. because of totalHotDogs += hotDogsPerDay; it will add another value for the same object. how can i tell the machine to ignore the second (or more) invocation of the constructor for the same object?
I have written a program in JAVA which fetches an image from MYSql Database. I get a BLOB object then I am trying to convert that BLOB object to string; again I am using that object to write the image to a file. the image file is getting created in E: but it does display any image
import java.util.ArrayList; public class LectureRoom{ private String courseName; private String roomNumber; private String Lecturer; private ArrayList <Student> studentList;
[Code] .....
Question:
Given the following BlueJ class diagram
Lecturer class (same with previous lab, no changes needed) Student class (same with previous lab, no changes needed)
LectureRoom (changes occurs here)
1. LectureRoom has roomNumber (e.g. A301), courseName (e.g. Java), lecturer (a reference to a Lecturer object), and studentList (a reference to an ArrayList that stores Student object). 2. LectureRoom has a constructor that receives courseName, roomNumber, and Lecturer. The constructor then sets/assign the courseName, roomNumber and Lecturer. This constructor also creates the studentList arraylist object.
Its written that every constructor calls its super class constructor. And we know, constructors are called to create an object. Does it mean that if I am creating an object of a class, I am actually creating objects of all its super class???
So I want to write a constructor that creates a new object with the data from the array values. I don't know where to start. It's the last method in the code:
public class Measurements { private double[] values; private double[] newArray; private int n; //numberofvalues private double[] ms; public Measurements(int max) { //constructor
I have a class of Date with a constructor with 3 parameters in it. Those 3 parameters are int data type just to enter month, year, day.
I have another class called Author which has a constructor of Date diedDate; as a parameter passing to the Author constructor.
I was asked to call the Date parameter is null, call the default constructor but I thought for the Date parameter I could only enter something like 0,0,0 instead of typing in null, null, null because null is for String data type isn't it?
what have I done wrong n the following code? I'm trying to create a new instance carte of object Carti using the constructor and then to insert a row into a table created with SQL.The error I'm getting is:
Exception in thread "main" java.lang.NullPointerException at Carti.Carti.InsertCarti(Carti.java:103) at Main.main(Main.java:37) Java Result: 1 BUILD SUCCESSFUL (total time: 28 seconds)
The line Main.main(Main.java:37) is when I try to insert the row. The line Carti.Carti.InsertCarti(Carti.java:103) is when I do the PreparedStatement st = conn.prepareStatement("insert into Carti (Id,titlu" + ", descriere, autor, editie, anPublicare) values (?,?,?,?,?,?)");
I want to learn more on motion tracking with java. Found some artikles and some examples with dead links. I haven't found much on it on google.I know it has to be done with JMF but besides that I cannot find any useful stuff on the internet.
I want to validate the user session everyone when ever the request comes for any jsp page. I am able to validate the user in a filter for the first time. But i am confused what would happen when the request comes for other pages...how will i be able to get the same session from the server?
I made a kind of maze game that includes the class keylistener and orients a object, i can't find where the program tracks this object (where its x and y coordinates are). So now my object can move freely through all walls and i want it to bounce back or at least something to happen when the object reaches a wall.
I want to find a way to limit my objects movement and because i cant find where the coordinates or variable for this object is i cannot limit its movement
I was practicing my java skills and came across an exercise in which a non parameter constructor calls a two parameter constructor. I tried a few searches online but they all came back unsuccessful. This is the part I am working on:
public PairOfDice(int val1, int val2) { // Constructor. Creates a pair of dice that // are initially showing the values val1 and val2. die1 = val1; // Assign specified values die2 = val2; // to the instance variables. } public PairOfDice() { // Constructor that calls two parameter constructor }
I tried calling the two constructor using the line "this(val1, val2)" but I get an error because val1 and val2 are local variables.
Then I tried to use the same signature: "this(int val1, int val2)" but that didn't work either.
Create an equals method that takes an object reference and returns true if the given object equals this object.
Hint: You'll need 'instanceof' and cast to a (Geocache)
So far I have:
public boolean equals(Object O){ if(O instanceof Geocache){ Geocache j=(Geocache) O; if (this.equals(j)) //I know this is wrong... but I can't figure it out return true; }
else return false; }
I think I have it correct up to the casting but I don't understand what I'm suppose to do with the this.equals(). Also I'm getting an error that I'm not returning a boolean... I get this all the time in other problems. I don't get why since I have to instances of returning booleans in this. "returns true if the given object equals this object" makes no sense to me. I assume the given object, in my case, is 'O'. What is 'this' object referring to?
I am new to Java and have read books, the Java docs, and searched the Internet for my problem to no avail. I have an Array of objects that contains strings. How can I get the object's strings to print in a list so that the user can select that object to manipulate its attributes? For example, the user can select "Guitar 1" from a list and manipulate its attributes like tuning it, playing it, etc. I have a class called Instruments and created 10 guitar objects.Here is the code:
Instrument [] guitar = new Instrument[10]; for (int i = 0; i < 10; i++) { guitar[0] = new Instrument("Guitar 1"); guitar[1] = new Instrument("Guitar 2"); guitar[2] = new Instrument("Guitar 3"); guitar[3] = new Instrument("Guitar 4"); guitar[4] = new Instrument("Guitar 5"); guitar[5] = new Instrument("Guitar 6");
Now lets say that I want to access a method 'addInterest()' that is in the 'SavingsAccount' class I would have to do: '((SavingsAccount)s).addInterest();'
The question I have is why do I have to cast 'b' to SavingsAccount? Isn't the actual object reference of 'b' already an instance of 'SavingsAccount' class? How does the 'BankAccount' affect the object itself? I'm really confused as to what class is truly getting instantiated and how BankAccount and SavingsAccount are both functioning to make the object 'b'.
I don't understand why the object reference variable 'a' cannot be recast from a thisA object reference to a thisB object reference.Is it the case that once a reference variable is linked to a particular object type then it cannot switch object types later on.I am facing the Java Associate Developer exam soon and I am just clearing up some issues in my head around object reference variable assignment,
class thisA {} class thisB extends thisA { String testString = "test";} public class CastQuestion2 { public static void main(String[] args) { thisA a = new thisA(); thisB b = new thisB();
I am trying to get this to where I can type in a name and it will search through each object and print back the corresponding object info.
Java Code:
import java.util.Scanner; public class MyPeople { public static void main(String[] args) { Person[] p = new Person[] { new Person("Chris", 26, "Male", "NJ", "Single"), new Person("JoAnna", 23, "Female", "NJ", "Single"), new Person("Dana", 24, "Female", "NJ", "Single"), new Person("Dan", 25, "Male", "NJ", "Single"), new Person("Mike", 31, "Male", "NJ", "Married") };
Task:The main method of the class Things below creates an object called printer deriving from the class PrintingClass and uses that object to print text. Your task is to write the PrintingClass class.
Program to complete: import java.util.Scanner; public class Things { public static void main(String args[]) { String characterString; Scanner reader = new Scanner(System.in); PrintingClass printer = new PrintingClass(); System.out.print("Type in the character string for printing: "); characterString = reader.nextLine(); printer.Print(characterString); } }
// Write the missing class here
Note: In this exercise the solution is part of a conversion unit where many classes have been declared. Because of this the classes are not declared as public using the public attribute.
Example output
Type in the character string for printing: John Doe
John Doe
My Class: class PrintingClass { public void print(){ System.out.println(characterString); } }
I have just started working with linked lists. I have a linked list of Objects and I want to be able to search for a specific object. But currently my code continues to return false. Also how would I go about removing the first index of the linked list.
public static void main(String[] args) { LinkedList<Cookies> ml = new LinkedList<>(); int choice = 0; while (choice >= 0) { choice = menu();
I am reading Head First: Java and got to Object References. In the book I got a little bit confused on what happens when two object reference's point at the same object so I wrote a small crude test, the below code. This of course clarified what happens but what I am interested in knowing is in what circumstances would you want to have two separate references for the same object when you could just use the original? Eg. v1
class ObjectValue{ int objVal = 1; } class ObjectValueTestDrive{ public static void main(String [] args){ // "Value of v# should be" refers to if it copied the given object values, instead of referencing the same object ObjectValue v1 = new ObjectValue(); System.out.println("Value of v1 should be 1:" + " "+ v1.objVal);
Explain anonymous objects with example clearly...i read some where anonymous objects advantage is saving memory...it is benificiable when there is only one time object usage in our program..i can't understand one time usage of object ....i know anonymous objects but i don't know in which context we use them in our programs...i did the anonymous object program with my own example but i can't differentiate this one with normal object..i'm providing my own example below
//anonymous object public class anonymous { int x=10; int y=25; void display() { System.out.println("anomymous");