Type In A Name And It Will Search Through Each Object And Print Back Corresponding Object Info
Nov 19, 2014
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") };
[code]....
View Replies
ADVERTISEMENT
Apr 15, 2014
For example I create an object like this:
BankAccount b = new SavingsAccount();
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'.
View Replies
View Related
Nov 6, 2014
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();
[code]....
View Replies
View Related
Oct 7, 2014
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");
[code]...
View Replies
View Related
Apr 9, 2014
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);
}
}
View Replies
View Related
Jul 6, 2014
I've been putting together an addressbook and try to work on some idea but I have hit a bit of a wal early on, and Im sure the problem is easy to resolve, I just can't get the old brain to do it.At present I have 5 classes
PersonDetailsAdressDetailsAddressBookAddressBookGUIEnterDetailsPanel
I have taken EnterDetailsPanel out of the main GUI code, as I plan, or had planned on using additional JPanels for the other options of the application until I got stuck. What I want the EnterDetails panel to do, which it does so far is to have the user enter all of their information and when the button is pressed it will then create a PersonDetails object and here is where my problem is. how to get the object from the panel back across to the GUI which initalises the AddressBook class in the constructor and add it to the List via an addDetails() method.
If I create a quick method in the AddressBookGUI class I can add the object to the List fine, I'm guessing this is because I have created a new instance of the AddressBook in here already via the constructor, however if I add an AddressBook reference in the EnterDetailsPanel I get a nullpointerexception for the opposite because it's not been created in there.These are what most of the classes do so far.
PersonDetails has a reference to the AddressDetails and holds all information for that person.AddressBook contains a List that accepts PersonDetails and has methods to add/remove the person.AddressBookGUI creates a new instance of the AddressBook class along with the general gui things. EnterDetailsPanel is added to JTabbedPane in the GUI class and allows the user to enter their details and submit those via a button which will then create a new PersonDetails object that I am trying to get added to the AddressBook class List
View Replies
View Related
Apr 12, 2014
Basically the class is supposed to have a static variable that keeps track of the number of companies (numberOfCompanies) that have been created and a static method that returns the value of this variable. I need to have a constructor and a method to addEmployee, a method to printCompany, and a toString method. The addEmployee method will check that there is only 1 salesperson, 2 designers, and 4 manufacturing persons at a time and will check that there are no more than 7 employees of the company.
The addEmployee method will return a String indicating what the error is (i.e. "There is already a sales person in this company") or a null if the employee was added. If attempting to name a third company, an error message will be shown. what i have so far but my print company mmethod returns null company class :in which i am obliged to use inputdialog because it says that it cannot return void when i use the showmeassage diaolog.
company class:
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class Company {
private ArrayList<Employees> list ;
private static int numberOfCompanies;
[Code] ....
error message when print button is clicked:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: JOptionPane: parentComponent does not have a valid parent
at javax.swing.JOptionPane.createInternalFrame(Unknown Source)
at javax.swing.JOptionPane.showInternalOptionDialog(Unknown Source)
at javax.swing.JOptionPane.showInternalMessageDialog(Unknown Source)
[Code] ......
View Replies
View Related
Apr 5, 2015
I'm trying to use LinkedBinarySearchTree but a lot of the variables are protected in BinaryTreeNode. I am creating a BinaryTreeNode object but it still isn't allowing me to use them. The variables I am trying to use are element, left, and right.
import ch11.exceptions.*;
import ch10.LinkedBinaryTree;
import ch10.BinaryTreeNode;
/**
* LinkedBinarySearchTree implements the BinarySearchTreeADT interface with links.
*/
public class LinkedBinarySearchTree<T> extends LinkedBinaryTree<T>
implements BinarySearchTreeADT<T>
[Code] ....
View Replies
View Related
May 27, 2014
I want to get the max volume from a file that I stored in an arraylist but it don't know if this is the way to do it. also I don't know how to print the method in the main method. here is the method that will get the max volume
public Shape3D maxVolume(ArrayList<Shape3D> shapes ){
Shape3D currentMax;
int currentMaxIndex = 0;
for ( int i = 1; i < shapes.size(); i++)
[Code] ....
This is my shape3D class
public abstract class Shape3D implements Comparable<Shape3D>
{
private String type;
public double radious;
public double height;
[Code] ....
View Replies
View Related
Mar 28, 2014
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?
View Replies
View Related
Dec 27, 2014
I want to create a program where I need to create an object of list type such as text file will contain nos like 1,2,3,4,5 and write into text file and delete the in FIFO order i.e 1,2,3,4,5...how i can achieve to write a program? I tried bt everytime got concurrent modification exception or Array out of bound exception.
View Replies
View Related
Aug 9, 2014
Essentially, the code I want is:
public void randomCreate(ParentObject obj){
int x = random(0-4); //pseudo
int y = random(0-4); //pseudo
create new ParentObj(x,y);
}
ParentObject is actually abstract, so you would only ever pass one of its children objects to it, and a child object of that type would be created. It seems like there should be a way to pass a type, rather than an object, and then create an instance later down, but I don't know if that is actually possible, or if it is poor programming style.
View Replies
View Related
Jul 26, 2014
I have the following code:
public class CollisionManager<T> {
private boolean collision = false;
private T mainEntity;
public <T extends Entities> void handleCollision(T mainEntity, T secondEntity){
this.mainEntity = mainEntity; // This is illegal.
}
}
Why "this.mainEntity = mainEntity" is incorrect and also show me the correct way to achieve this?
The error I am getting is "Type mismatch: cannot convert T to T"
View Replies
View Related
Nov 7, 2014
i have a class that creates an object made of 2 strings and an arraylist
class Users{
private String ip;
private String userName;
private ArrayList <String>cons=new ArrayList();
Users(String i_p,String un,ArrayList con){//construc
[Code] .....
This object then gets returned to an arraylist of these objects
static ArrayList<Users>users=new ArrayList();
My question is how can i then get the arraylist from this object to print its contents?
View Replies
View Related
Feb 28, 2014
I am getting "Type safety: Unchecked cast from Object to LinkedList<EventData>" in eclipse for a piece of code stated below
public LinkedList<EventData> loadFromFile(File file) {
queue=new LinkedList<EventData>();
//Some piece of code
return (LinkedList<EventData>)queue.clone(); //--->getting warning here
}
I know that because clone() method is returning Object, hence compiler doesn't have type information that's why showing warning. I don't want to suppress this warning instead i want to fix it.
View Replies
View Related
Oct 13, 2014
I am having a little trouble with a part of my Java assignment that needs to have linked lists in it.
I need to write a public method that takes an array of type object to load strings into a linked list.
View Replies
View Related
Oct 6, 2014
I have an Array of objects that contains strings. I am new to Java and have read books, the Java docs, and searched the Internet for my problem to no avail. How can I get my strings to print in a list so that the user can select an object to manipulate its attributes? 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");
[Code] ....
View Replies
View Related
Mar 27, 2015
can a keyevent in java make the space ship and the laser too ?
View Replies
View Related
Mar 23, 2015
If I set a Jlist to contain an array of objects, how can I display properties of each object instead of the object array list itself. Ex:
Instead of:
Person1
Person2
Person3
display values such as each person name within the Jlist:
Mike
Paul
Andrew
View Replies
View Related
Nov 19, 2014
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();
[Code] ....
View Replies
View Related
Dec 27, 2014
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);
[code]....
View Replies
View Related
Aug 18, 2014
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");
[code]....
View Replies
View Related
Apr 28, 2014
I am trying to invoke some methods in a for loop in order to print some info stored in a List. But for some reason, compiler pops a message saying "cannot find symbol - method getEmpID(). You are using a symbol here (a name for a variable, method or class) that has not been declared in any visible scope." But I am pretty sure that method getEmpID (as also getName(), getAfm(), and payment() ) have been declared as public.
Note: My List contains objects of different type (SalariedEmployee, HourlyEmployee). I hope this is not the factor causing this problem.
Java Code:
import java.util.*;
abstract class Employee{
private String name = "";
private String afm = "";
private long EmpID;
static long count=0;
[code]....
View Replies
View Related
Jan 15, 2015
What I want to do is have a label that is updated whenever an object gets some new, relevant data.The way you do it in Java looks different from the way we do it in Objective-C. In Objective-C, we have what's known as a protocol. An Objective-C protocol is almost exactly like a Java "implementation." In Obj-C, if I want the user to see the address of where he is, I can have an object that gets the information and invokes a view controller's method; at that point, the view controller would then take the data passed to it and display the data in a label. However, the view controller is an instance of a subclass of the bundled view controller class.
View Replies
View Related
Jun 11, 2014
I am trying to find either some references to point me on the right track with passing an object with all of it's properties still in tact after it's been created. Currently I am trying to do this through an interface but it seems to just create a new object everytime without the properties. Example below :
import java.util.ArrayList;
import java.util.List;
public interface TPerson{
//public Person p = null;
}
class Thrower {
Person p;
[code]....
When I implement the interface on the other objects as soon as I call the setP method shown above it seems to just create a new one even though I pass the object to the method I want to use.
View Replies
View Related
Apr 2, 2014
How do i take input values for TwoDPoint (which are objects) and return it back in numerical values also print them.
When i create an object in main method and pass values to my function of return type TwoDPoint,it gives error:- found int,int need TwoDPoiint,TwoDPoint.
// Here is what i tried to do:
Created class TwoDPoint that contains two fields x, y which are of type int. Defined another class TestTwoDPoint, where a main method is defined.In the main method created two TwoDPoint objects.
Then I modified the class TestTwoDPoint and add another function to it. This function takes two TwoDPoints as input and returns the TwoDPoint that is farthest from the point (0,0).
Then I added another function to TestTwoDPoint. This function takes two TwoDPoints as input and returns a new TwoDPoint whose x value is the sum of x values of the input TwoDPoint's and whose y value is the sum of the y values of the input TwoDPoint's.
class TwoDPoint {
int x = 2;
int y = 4;
}
class TestTwoDPoint {
public static void main(String args[]) {
TwoDPoint obj1 = new TwoDPoint();
System.out.println(obj1.x);
System.out.println(obj1.y);
[Code] ....
View Replies
View Related