What Does Creating ArrayList Object In A Constructor Mean
Mar 22, 2014
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???
I have stumbled onto a problem with ArrayLists (not sure if nested ArrayList objects would be a more accurate description) ....
As simply put as I can describe it, I have an Inventory class which creates an ArrayList of a Product superclass, which has two subclasses, PerishableProduct and ItemisedProduct.
By default, the program is meant to have a starting inventory, which is why I have added them in the constructor
public class Inventory { private List<Product> products; public Inventory() { addProduct(new Product("Frying pan", 15, 20)); addProduct(new PerishableProduct("Apple", 5.8, 30, 7)); addProduct(new ItemisedProduct("Cereal", 5.8, 0)); // this is where I am having problems. Wanting to add // objects to the ItemisedProduct's ArrayList for cereal. }
Within the ItemisedProduct subclass is yet another ArrayList which is meant to hold serial numbers.
public class ItemisedProduct extends Product { private ArrayList<String> serialNumbers = new ArrayList(); public ItemisedProduct(String name, double price, int qty) { super(name, price, qty)
[Code] .....
My problem is that I do not know how to populate the serialNumbers ArrayList from the Inventory class' constructor. Because technically, quantity is defined by how many serial numbers are created.
class GenericQueue<E> { private LinkedList<E> list = new LinkedList<E>(); public void push(E element) { list.addFirst(element); } public E pull() { return list.removeLast();
[code]...
Is a constructor required to create an object, if one of its instance or class variables haven't been instantiated? Like private String string;
I want to create a simple app that takes a name from the console then compares the name to a small phone book,when the name matches another name it will return the associated phone number.
I have a small contacts class which has name and number fields,Then I have a phone book class which populates an array with 4 contact objects that I can compare the entered number against.
here is my contacts class
public class Contact { String name; int number;
[Code].....
In the main method I am just trying to print out one of the fields for one contact to see if I can actually access it to compare it to the name entered.Its saying "MaryJones" cannot be resolved to a type.I'm guessing I cant create all that code in the constructor?
I thought you can only create a new object using private implementations and then using a constructor to set your arguments inside the parameters of the constructor to the instance variables but how come he created an object without any private implementations and just methods inside the constructor.
import javax.swing.JFrame; public class MyWindow extends JFrame { public static void main(String[]args){ new MyWindow(); } public MyWindow(){ setSize(500,500); setVisible(true); setTitle("MyWindow"); } }
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'm writing a program that acts as a 'pocket' where the user is able to enter a kind of coin, such as, a quarter and the amount of quarters it has. I was assigned to do 3 different class, the Coin Class in which the coins and their values can be instatiated from, a Pocket Class, where I have to write a method that can add the coins of the user (basically the method would act like ArrayList .add() ) and the PocketClass tester. I have already written most of the code, but I am stuck as to how I could write the following method:
Java Code:
public void addCoin(String s, int i) { // s is type of coin, you are using s to instantiate a Coin and get value // i is number of coins, you are using i to keep adding value to the totalValuefor } mh_sh_highlight_all('java');
Would I use a for-loop in order to keep track of the number of coins? I would use ArrayList but the assignment calls for creating a method similar to that of .add()
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'm creating a card game assignment... so i have an arraylist called cards that has 20 cards and every card has contains 2 objects, suit and the point Value.
I shuffled the deck, now i want to add half of it to player 1 and the rest of the cards goes to the bot or computer.
how can i add the cards to the player one arraylist and have all the information of the cards?
here is my Deck class code :-
public class Deck { private ArrayList cards; private int size; private ArrayList player1; private ArrayList bot;
[code]....
the problem i have is this one doesn't work
size = cards.size() / 2; for (int i = 0; i < size - 1; i++) { player1.add(cards.get(i)); } for (int s = size; s < cards.size() - 1; s++) { bot.add(cards.get(s)); }
I've a parent class with a argument constructor like below(a sample code)
public class Parent { Parent(String name) { System.out.println(name); } public static void main(String[] args) { } }
Also I've child.class which extends Parent.class as shown below,
public class child extends Parent { child(String name) { super(name); } }
Now, I want create/modify the constructor which is in child, by taking "int i" as an input instead of "String name". How can I do that? Run time I want to execute child constructor not a parent constructor.
Condition is: Without making any changes to the Parent class
I'm really new to object oriented coding. A good way to create a Text Based Game that used objects as rooms, and items. However, I can't figure out what the best way to define and use rooms and items would be. I've been at it for the past few days, and just can't think of something that works well and is easy to use.
I want to have each room have it's own "inventory" of items that the player can pickup, as well as drop items into. I want items that are dropped to maintain their properties (durability, level, ect). I would also need several methods for things like getting an item from a room by the item's display name or id. For instance, if there is a room with a Broken Iron Sword, and a Stone Dagger, I'd need to be able to get the Broken Iron Sword object with the string "Broken Iron Sword".
So I'm Half way done with this assignment and all I need to is edit and sort my directory. What I've been trying to do as of now is edit my directory. I've tried to use the set function in the Array List, Iterator List etc but I just don't know how implement them mainly because I keep thinking "how can check which variable in the directory the user wants to change(Name, cost etc etc)?".
Main class
Java Code:
package plantnursery; import java.util.Scanner; import java.util.ArrayList; public class PlantNursery { private ArrayList<Plant> plantDirectory = new ArrayList<>(); private static Scanner read = new Scanner(System.in);
package home; import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class Box{ int x=70; int y=70;
[Code] ....
L a = new L(); causing the error. It will be great to know why it is showing error.
I just want to ask about a kind inheritance.Let say I have an interface MachineCode.I also have different classes, Binary, Hex and Octal that implements MachineCode
Question: How can I prevent java to create an Object like this:
Binary bin = new Binary(); Hex hex = new Hex(); Octal octal = new Octal();
those declaration above must be compile error,
I want to create Objects of Binary, Hex, and Octal this way: MachineCode bin = new Binary(); MachineCode hex = new Hex(); MachineCode octal = new Octal();
Using JDK7 is there a way to get an object that has got a specific property from the collection? For instance, I might want to seek if there is an Address containg "Tim Carlton" in the ArrayList.
I'm almost finished my Bank Account exercise and I found myself stuck at the last part. Its asking me to add a method that asks the user to input the name of the account into which they want to deposit money, then search the ArrayList for that account. If it is found, the user is asked how much money they wish to deposit.
I already have my deposit method sorted so basically what I need is just searching through the ArrayList by the name variable. I assume its don't by iterating through with some form of for loop.Heres what I have:
import java.util.Scanner; public class BankAccount { private double balance; private String name; public BankAccount(double balance, String name){ this.balance = balance;
[Code]......
And the driver class
import java.util.ArrayList; import java.util.Scanner; public class BankDriver { Scanner scan = new Scanner(System.in); ArrayList<BankAccount> list; public BankDriver(){