I am currently working on a project where I need to return data from a database over RMI to a client who requests it. Some of the fields in the Data Object can not be seen by the client so I need to create another object to send over the network instead. The method I use is this...
public static SerializableObject createSerializableObjectFromDataObject(DataObject dataObject){
SerializableObject serializableObject = new SerializableObject();
serializableObject.setField(dataObject.getField());
serializableObject.setAnotherField(dataObject.getAnotherField());
return serializableObject;
}
Is there a better way of doing this? I am creating many subclasses DataObject which all require this static method to be implemented and I can't push it into the superclass because each one needs custom behaviour.
This program is basically complete. It compiles and runs. It is a college course assignment that I pretty much completed but for the last part in which I'm suppose to change the values of all fields and display the modified values using a toString method. Modifying the values of the fields is where I am stuck. I don't think I need to create a new text data file to do this. The instructor only asked that all the values of fields be changed and this was the last part of the assignment so I don't think it involves creating additional ObjectOutputStream and ObjectInputStream objects. I'm getting a NullPointerException error on line 161.Here is the code. I'm also including the input data file.
//create program so that objects of class can be serialized, implements interface Serialiable //create constructor with 4 parameters with accompanying get and set methods, Override toString method //create text file with 5 records, create Scanner object,ObjectOutputStream, and ObjectInputStream //create new ItemRecord object, change values of all fields in ItemRecord object using object's set methods //modify ItemRecord object using toString method
[hightlight =Java]import java.io.Serializable; public class ItemRecord implements Serializable
----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete.
Here is the data file: A100 99.99 10 Canon PowerShot-135 A200 149.99 50 Panasonic-Lumix T55 A300 349.99 20 Nikon- D3200 DSRL A400 280.99 30 Sony- DSC-W800 A500 97.99 20 Samsung- WB35F
Here is the data file for the modified field values. B100 98.00 10 ABC1010 B200 97.00 15 DEF1020 B300 96.00 10 GHI1030 B400 95.00 05 JKL1040 B500 94.00 01 MNO1050
public class Cash { //Quanity Field and RetailItem Object field private int total_units; private Retail myRetail;
//Create first constructors public Cash() { this(0,null);
[Code] ....
I'm seriously need to understand the concept of making shallow copies and deep copies. My book has shown me that its better to perform deep copies and I have followed that method. if you guys look in my constructor of the cash class
//Create a a constructor public Cash(int total_units,Retail myRetail) { this.total_units=total_units; this.myRetail=new Retail(myRetail);
Apparently I have field in Cash Class named
Retail myRetail
When I pass in an argument from my demo, I'm making a copy of that object from the demo class. In my retail class, I have the following copy constructor .
//Make a copy constructor public Retail(Retail Object1) { Item_Name=Object1.Item_Name; Item_Number=Object1.Item_Number; // if I use this then my program would work//this.cost=Object1.cost;
//if I use this part of code below, my program won't work at all and I would get an error saying Exception in thread "main" java.lang.NullPointerException this.cost.Item_Cost=Object1.cost.Item_Cost; this.cost.Wholesale_Cost=Object1.cost.Wholesale_Cost; }
My question is why can't I perform a deep copy there. I know if I do
this.myRetail=myRetail
in my cash constructor it would work, but then the book says its not a good method;
I've been unable to figure out how to access an objects data from another class. I ended up missing a lesson in java and haven't been able to catch up on this topic on my own through my textbook.
Error: has private access
Code:
public class TestCoffeeDrinker { public static void main(String[] args) { Coffee latte = new Coffee("Starbucks Tall Latte", 2.85); Coffee mocha = new Coffee("Starbucks Grande Mocha", 3.95); Coffee mcdonalds = new Coffee("McDonalds McCafe", 0.99); System.out.println(mcdonalds.toString());
I want to keep count of how many students are in my array. the array i made up of objects from other classes. like the class Student how do i do this. i have tried contains but better way it to to a loop to go through the array and determine if each object is a particular type but i don't know how to do this. here is the code
import java.util.*; import java.util.ArrayList; public class Driver { public static void main(String[] args){ /* Comments required PersonFileReader pfr = new PersonFileReader("person.dat"); ArrayList<Person> testData = pfr.readData(); Database db = new Database(testData);
My problem is that in my program, I have the user input data for one of three product objects, however when I read the data for all three objects, the same data is stored in all of them.
Anyway this is the method from the Interface class:
private void readInput() // the only method in the program that accepts product data from the user { Store matesStore = new Store(); String name; int demandRate, productChoice; double setupCost, unitCost, inventoryCost, sellingPrice; Scanner console = new Scanner(System.in);
[Code] ....
And here is the method from the Store class:
public static void addData(int option, String newName, int newDemand, double newSetup, double newUnit, double newInventory, double newPrice) //sets the product data for a particular product { if (option==1) setData(product1, newName, newDemand, newSetup, newUnit, newInventory, newPrice); else if (option==2) setData(product2, newName, newDemand, newSetup, newUnit, newInventory, newPrice); else /*(option==3)*/ setData(product3, newName, newDemand, newSetup, newUnit, newInventory, newPrice);
[Code] ....
The problem I had was with static variables and methods.
I have seen many ways of describing what objects are, one being that objects are a user-defined datatype. However, if objects are datatypes, then what does that make classes? To me, it seems as though classes should be the "types" of data defined by the programmer, and objects should be the specific "values" of that user defined data type. As an example, an integer would be a class, while 1 would be a "value" of that class, i.e. an object. From this point of view, I don't see why a specific number would be a data type... Therefore, why do we say that objects are user defined data types rather than classes?
how String objects are different from other objects
part 1:
// creating two objects Dog mydog1 = new Dog(); Dog mydog2 = new Dog(); // comparing the reference variables if( mydog1 == mydog2){ System.out.println(" The reference variables refer the same object "); } else { System.out.println(" They refer to different objects "); }
The above code works as I understand objects , it prints "They refer to different objects " to the screen.
Part - 2
// creating two objects ( I beleive, pls correct me if i am wrong ) String a = "haai"; String b = "haai";
if( a == b){ System.out.println(" Reference variables refer to same object");
When i run the above code it prints that a and b refer same object , I don't understand how they refer to same object when i didn't assign " String b = a; ". My question is did java just create one object and stored the same reference values to a and b .
The thing is if SITA is the name then I have to insert component to the particular envi tabs , if DEVB is the component I have to insert the component there. How can I do this I have done some code its inserting the data at the bottom og the XML .
I'm quite new to Java. I have some trouble with understanding how to get two classes to get objects from each other (if that is the correct term).
Lets say I have a login class, in which I have a method checking what the user has entered into the console (I have not displayed the whole code, but this class works as it should and give the user an option to enter username and password and return it true if entered correct).
public static boolean validateUserPass(String userName, String password) { String[] user = {"admin"}; String[] passwords = {"firkanten"}; boolean check = false; for (int i = 0; i < user.length; i++) { if (userName.equals(user[i])) { if (password.equals(passwords[i])) { check = true;
Now, in another class I want a display box to appear on the screen and give the user three options: Click yes, no or cancel. I get this to run perfectly alone, this is not the hard part for me. I want the display box only to appear when the correct username and password is given, but I can't seem to figure out how to do this probably.
In my project I had to create 2 classes, Room and Animal.
Room had to have an array (NOT arrayList-I know theyre better and easier but I need to use an array, for now) This array must be able to be populated by 10 "Animals", and the Room class needs two methods, a method to add animal a to the room (rooms array) and a toString() that returns the name of the room and the names of the animals in the room. The teacher added a hint saying that this toString() should reference the animal classes toString()
Here is my code thus far:
public class Room { private String name; Animal[] inRoom = new Animal[10]; public Room(String roomName){ name = roomName; } public void addAnimal(Animal a){ for(int i = 0; i < 10; i++){
The thing is if SITA is the name then I have to insert component to the particular envi tabs , if DEVB is the component I have to insert the component there. How can I do this I have done some code its inserting the data at the bottom og the XML.
I am making a game in java and for the game board i want to fill the screen with blocks. to do this i stored objects of a class that displays squares into an array list and displayed the array list. however, when i do this all of the squares are drawn on top of anther at the final squares coordinates and i dont know why.
here is the code
// code for adding the squares into the array nt x = 0; int y = 0; int size = 10; static ArrayList<Map> map = new ArrayList<Map>(); //static ArrayList<Items> items = new ArrayList<Items>();
I'm having trouble creating two new die objects for the PairofDie class. I'm trying to run two separate die and print the face value and then add both numbers up and print those values as well.
public class Die { private final int MAX = 6; private int faceValue; //private int faceValue2; public Die(){ faceValue = 1;
[Code] .....
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method Die() is undefined for the type PairofDice The method Die() is undefined for the type PairofDice at PairofDice.main(PairofDice.java:6)
I would like to know how I can iterate through objects . I have a manually created linked list (without using the built-in method one). So in the memory my object looks like this(attachment).I would like to do a for loop or while loop to get each element under the test3.head.
Exercise the Coin class using a driver class in which 5 coins are flipped, as a group, 1024 times. Display the number of times exactly 4 tails are obtained. Display the number of times exactly 5 tails are obtained. Use an array of Coin.
how to put an object into an array?I know how to make a coin class with a Heads and Tails, but I dont know how to make 5 coins all at once.
public class Item{ private int myId; private int myInv; public Item(int id, int inv){ myId = id; myInv = inv;
[Code] .....
I do not understand how to complete the compareTo() method and the equals() method. I understand that the compareTo() to should return 1 if the argument is smaller -1 if its bigger and 0 if they are the same, but I do not understand how it's possible to compare the current object with the object passed in as the parameter.
This is what I tried for the compareTo():
public int compareTo(Item other){ return this.compareTo(other); }
But my compiler tells me its a recursive call so I am not sure if I am doing this correctly.
Basically I am supposed to compare an object to another but I am not sure how to access the object that is not passed in as a parameter and compare it. Same goes for the equals(). This is what I tried for that (compiler also says its a recursive call):
public boolean equals(Item other){ return this.equals(other); }
I can't come up with a simple scenario so I'll use my actual code to explain the problem, and it's going to be related to java.awt.Color, but my problem is general, not awt dependent. Although I will shorten my code to show only what I think is relevant to the problem.
Java Code:
public class GameColor { public abstract class Effect { public int strength; public Effect(int strength) { this.strength = strength; } public abstract Color get(Color color);
[Code] ....
I have my reasons for having a SetHue inner class and a setHue method that practically do the same thing. The inner abstract class Effect and inner class SetHue was static but I changed it when I testing what could cause this problem, later I will need them to be static, but since they don't affect the problem I don't think it matters if they're static or not.
So down to the problem, I create three new GameColor instances which all are 255, 0, 0, basically red. Then I do setHue(0) method on the first one, setHue(20) on the second one, and setHue(40) on the last one. And for some reason all of them have a hue of 40, whereas the last one should've had this, so basically the last time I call setHue on the instances of GameColor, changes the hue of all of these colours to that hue. I want it to be individually affected, but all of them are affected. This is where I apply these changes:
I am currently trying to implement a hashmap from a class i currently have that is called 'Paper'
My paper class consists of:
String Title; String[] author;
What I am wondering, is if there is anyway I can call these seperate attributes to be both the key and the value of the hashmap. Currently I cannot see a way to call individual values, only the class itself.
I have a ArrayList of objects of class called HockeyPlayer (ArrayList<HockeyPlayer>). A HockeyPlayer has a String name and int number of goals.
This is my current work for comparing each object in the list to every other and printing them to screen:
Iterator<HockeyPlayer> it = hockeyPlayersList.iterator(); while (it.hasNext()) { HockeyPlayer singleHockeyPlayer = it.next(); //the first one encountered // HockeyPlayer nextHockeyPlayer = it.next(); //the next one encountered
[Code]...
This of course produces some duplication in the print-out: These are the equal hockey players: Who Ever wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.
Jason Harrison wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.
These are the equal hockey players: Jason Harrison wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.
Who Ever wears jersey. This type of employee is paid: true. This employee has post-secondary education: false. This employee's work is to play. This hockey player has scored 0 goals.