File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Displaying things...need help badly!! Please! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Displaying things...need help badly!! Please!" Watch "Displaying things...need help badly!! Please!" New topic
Author

Displaying things...need help badly!! Please!

Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
You guys, I'm at it again, need help badly!! I've got to use this class and write a program to read in data for five pets. And I need to display the following stuff. Here's my code so far:

/**
Class for basic pet records: name, age, and weight.
*/
public class Pet1
{
private String name;
private int age; //in years
private double weight; //in pounds

/**
This main is just a demonstration program.
*/
public static void main(String[] args)
{
Pet myDog = new Pet( );
myDog.set("Fido", 2, 5.5);
myDog.writeOutput( );
System.out.println("Changing name.");
myDog.set("Rex");
myDog.writeOutput( );
System.out.println("Changing weight.");
myDog.set(6.5);
myDog.writeOutput( );
System.out.println("Changing age.");
myDog.set(3);
myDog.writeOutput( );
}

public void writeOutput( )
{
System.out.println("Name: " + name);
System.out.println("Age: " + age + " years");
System.out.println("Weight: " + weight + " pounds");
}

public void set(String newName)
{
name = newName;
//age and weight are unchanged.
}

public void set(int newAge)
{
if (newAge <= 0)
{
System.out.println("Error: illegal age.");
System.exit(0);
}
else
age = newAge;
//name and weight are unchanged.
}

public void set(double newWeight)
{
if (newWeight <= 0)
{
System.out.println("Error: illegal weight.");
System.exit(0);
}
else
weight = newWeight;
//name and age are unchanged.
}

public void set(String newName, int newAge, double newWeight)
{
name = newName;
if ((newAge <= 0) || (newWeight <= 0))
{
System.out.println("Error: illegal age or weight.");
System.exit(0);
}
else
{
age = newAge;
weight = newWeight;
}
}

public String getName( )
{
return name;
}


public int getAge( )
{
return age;
}
public double getWeight( )
{
return weight;
}
}
I've got to display the name of the smallest pet, name of the largest pet, name of oldest pet, name of youngest pet, average weight of the five pets, and average age of the five pets.
How in the world would I go about doing this? Any suggestions would be greatly appreciate.


) ) ) )
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
You will need to store your Pets in some form of Collection or Array. At the moment you are only creating a single instance of Pet and then manipulating it.
You have made a good start and are working along the right lines. Keep on coding.
Barry Higgins
Ranch Hand

Joined: Jun 05, 2003
Posts: 89
You'll obviously have to create a number of Pets and populate them.
The thing is then that you have to store them in an Array (or an ArrayList if you are going to add them dynamically).
You should hold this Array in another class and it is in here that you will have your getBiggest, getOldest etc methods.
These are very straightforward and will probably contain a for loop (to go through each element in the array).
eg
public Pet getOldest() {
oldestPet=null;
for (int i=0; i<PetArray.length; i++)
if (PetArray[i].getAge()>oldestPet.getAge())
oldestPet=PetArray[i];

return oldestPet;
}

where PetArray is the array of Pets

Hope this helps
Barry
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Thanks Barry and Nigel, I'll give it a whirl and see what I can come up with.
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Barry,

Am I anywhere close to getting somewhere...take a peek at my code...please.

/**
Class for basic pet records: name, age, and weight.
*/
public class PetRecord1
{
private String name;
private int age;//in years
private double weight;//in pounds
private int Smallest;
private int Largest;
private int Oldest;
private int Youngest;
private double AverageWeight;
private double AverageAge;

public void writeOutput( )
{
System.out.println("* Name: " + name);
System.out.println("* Age: " + age + " years");
System.out.println("* Weight: " + weight + " pounds");
}

public PetRecord1(String initialName, int initialAge,
double initialWeight)
{
name = initialName;
if ((initialAge < 0) || (initialWeight < 0))
{
System.out.println("Error: Negative age or weight.");
System.exit(0);
}
else
{
age = initialAge;
weight = initialWeight;
}
}

public void set(String newName, int newAge, double newWeight)
{
name = newName;
if ((newAge < 0) || (newWeight < 0))
{
System.out.println("Error: Negative age or weight.");
System.exit(0);
}
else
{
age = newAge;
weight = newWeight;
}
}

public PetRecord1(String initialName)
{
name = initialName;
age = 0;
weight = 0;
}

public void set(String newName)
{
name = newName; //age and weight are unchanged.
}

public PetRecord1(int initialAge)
{
name = "No name yet.";
weight = 0;
if (initialAge < 0)
{
System.out.println("Error: Negative age.");
System.exit(0);
}
else
age = initialAge;
}

public void set(int newAge)
{
if (newAge < 0)
{
System.out.println("Error: Negative age.");
System.exit(0);
}
else
age = newAge;
//name and weight are unchanged.
}

public PetRecord1(double initialWeight)
{
name = "No name yet";
age = 0;
if (initialWeight < 0)
{
System.out.println("Error: Negative weight.");
System.exit(0);
}
else
weight = initialWeight;
}

public void set(double newWeight)
{
if (newWeight < 0)
{
System.out.println("Error: Negative weight.");
System.exit(0);
}
else
weight = newWeight; //name and age are unchanged.
}

public PetRecord1( )
{
name = "No name yet.";
age = 0;
weight = 0;
}

public String getName( )
{
return name;
}

public int getAge( )
{
return age;
}

public double getWeight( )
{
return weight;
}

//all new stuff added

public int getSmallest()
{
return Smallest;
}

public int getLargest()
{
return Largest;
}

public int getOldest()
{
return Oldest;
}

public int getYoungest()
{
return Youngest;
}

public double getAverageWeight()
{
return AverageWeight;
}

public double getAverageAge()
{
return AverageAge;
}

}

What did you mean by creatiing a number of Pets and populate them?
I think I put my methods in the wrong place didn't I? I'm so lost...
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
To help you on your way with this
Barry Higgins
Ranch Hand

Joined: Jun 05, 2003
Posts: 89
As you can see from Nigel's code there is an array of length 2

This is being populated by the two pets he's created

Now you have to understand how arrays work and you might need to read up a bit on this but basically it a list of Pets help in one place that are refernced by index eg. myPet[0] is the Pet that sits in position 0 of the array.
You have to traverse through this array looking at each to see if this pet is larger than the largest one you have found already. At the end you return the largest Pet found in that search.

Notice also there are 2 classes in Nigel's code Pet and Pets you should put all your getLargest, getSmallest etc function into Pets and call them as needed from you main function.

Hope this helps,
Barry
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Whew, that all sounds so complicated! I'll see what I can do. Thanks.
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Gosh you guys, I must be dumber than dirt. I still have no idea what I am doing with this program...Please if any of you can help me to understand I would greatly appreciate it!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Everything takes practice. It typically takes 6 months to a year before you become comfortable with programming the first time. After that learning a new language is typically a little easier.

Enough pep talk

So what exactly don't you understand. If you don't understand the code posted by Nigel or Barry, tell us exactly which lines of code are confusing you. Someone here will be happy to clarify.

If you are having problems with the code you are writing yourself, it might be helpful to just post a few lines of code. (I for one and somewhat reluctant to go through 100+ lines of code just to look for errors for you.) If you get compiler errors, please copy and paste them here. If the code compiles but doesn't do what you expect, try to describe both the expected behavior and the actual behavior. The more specific you can be with your questions, the more we will be able to help you. (At least that's my own philosophy.)

Keep Coding! (TM)

Layen


Java API Documentation
The Java Tutorial
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Layne,

I've only been doing the java thing for a few weeks. (And just two days two hours, a week at that, plus trying to code some on my own.)

Some of the programs just seem so way over my head to try to code.
As of right now I'm working with three programs, Pet1, PetRecord1, and PetRecordDemo1. First of all, I do not understand 'where' (which part of the program I am supposed to start coding on. Pet1, PetRecord1, or PetRecordDemo1.) I do believe it is the PetRecord1 that I posted earlier that I am supposed to manipulate the code to make it do what I want it to do.
I just do not understand 'how' to do any of it. And if I make changes to PetRecord, I suppose I'll have to invoke or 'call' them from the PetRecordDemo1??? I just don't know.

This part that Nigel so kindly tried to help me with..


public class Pets{ public static void main(String [] args){ Pet myPets[] = new Pet[2]; myPets[0] = new Pet("Fido", 2, 1); myPets[1] = new Pet("Biff", 3, 2); System.out.println("I have a pet called " + myPets[0].name); System.out.println("I have another pet who is " + myPets[1].age + " years old"); System.out.println("The size of my array is " + myPets.length); }} class Pet{ String name; int age; int weight; Pet(String name, int age, int weight){ this.name = name; this.age = age; this.weight = weight; }}




Well I guess I should code something like this in that petrecord1. I am just so totally lost. Or do I create a whole new program and use that somehow. As you can tell, I am so lost I can't even find my own head. The program just seems so hard to me.

Any suggestions?


[ November 22, 2004: Message edited by: Rose Evans ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Perhaps you should go over the code that Nigel gave. Are there any particular parts of it that you don't understand? If you don't know where to start with it, perhaps you should try to think like the computer. What is the first line that will be executed? (Hint: That's what main() is for right?) Follow the execution one line at a time and figure out what each line does. It might also help to make a table with the variable names along the top and fill in the values they have at each line in the exection. When I was starting to program (and even now), I found it helpful to manually step through some code to understand what it does.

Keep working at it and don't give up. I'm sure you will start grasping it eventually.

Layne
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Thanks Layne. I'll see what I can do.
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673

In the above code myPet is the instance created of the type(interchange with class) Pet.
Now when we initialise instances of classes we are able to set their class variables on creation of the instance. To achieve this we need to write constructors . A constructor has the same name as the class, so for the Pet example we can create a class as follows
Note that the Pet class doesn't have a main method therefore it can't be thought of in terms of a program. In java one public class with a main method is always needed to create an application.
Now I can also create more than one instance at a time by creating an Array of type Pet
As arrays are zero based (they are indexed from 0) the above code created an array of type Pet with three spaces to place instances of type Pet. To populate the first position in the array (0) I use the following : Notice that on the left hand side of the = I state the position in my array where to add a new instance and on the right hand side of the = I create that instance using my constructor.
Please reread my earlier posted code and see if things become clearer now
[ November 23, 2004: Message edited by: Nigel Browne ]
r phipps
Ranch Hand

Joined: Sep 14, 2004
Posts: 60
Lane that is some of the Best advice i have heard in years. When i was in my Cobol Programming training course almost 20 years ago we were talked to play computer one line at a time. I know it has really helped me here in Java Programming.
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Nigel, you are one smart man!! I'll see what I can do with my code!
Thank you for your help!!! It is greatly appreciated!
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
I still haven't gotten 'anywhere' with this program. Just totally lost.
Ahhhhhhhhhhhh thinking I need to switch majors. Ha.
This should be simple, but it's sure not clicking in my brain.
Barry Higgins
Ranch Hand

Joined: Jun 05, 2003
Posts: 89
Well what have you got at the moment .. and where is it falling down?
Don't let it get you down .. otherwise if you switch majors and stop using programming you mightn't get to come in to work on Sundays
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
/**
Class for basic pet records: name, age, and weight.
*/
public class PetRecord1
{
private String name;
private int age;//in years
private double weight;//in pounds

public void writeOutput( )
{
System.out.println("* Name: " + name);
System.out.println("* Age: " + age + " years");
System.out.println("* Weight: " + weight + " pounds");
}

public PetRecord1(String initialName, int initialAge,
double initialWeight)
{
name = initialName;
if ((initialAge < 0) || (initialWeight < 0))
{
System.out.println("Error: Negative age or weight.");
System.exit(0);
}
else
{
age = initialAge;
weight = initialWeight;
}
}
********************************************
Apparently, I am supposed to
use this part of the code(the code below)for
the five new pets. I just don't
know how to do any of it. It just
seems overwhelming to me. I've gotten
some great advice from people but
I don't know where or how to use it.
I've got to add a system out statement
that ask the user to enter the new five
pets names, weight, and age.
Then I had to figure out the other stuff,
smallest pet, largest pet, oldest, youngest, and
the average weight of them all and the average
age of them all. I don't know where to begin.
Ha ha, I don't mind working on Sunday's as long
as I'm paid well. ~smile~

********************************************
public void set(String newName, int newAge, double newWeight)
{
name = newName;
if ((newAge < 0) || (newWeight < 0))
{
System.out.println("Error: Negative age or weight.");
System.exit(0);
}
else
{
age = newAge;
weight = newWeight;
}
}

public PetRecord1(String initialName)
{
name = initialName;
age = 0;
weight = 0;
}

public void set(String newName)
{
name = newName; //age and weight are unchanged.
}

public PetRecord1(int initialAge)
{
name = "No name yet.";
weight = 0;
if (initialAge < 0)
{
System.out.println("Error: Negative age.");
System.exit(0);
}
else
age = initialAge;
}

public void set(int newAge)
{
if (newAge < 0)
{
System.out.println("Error: Negative age.");
System.exit(0);
}
else
age = newAge;
//name and weight are unchanged.
}

public PetRecord1(double initialWeight)
{
name = "No name yet";
age = 0;
if (initialWeight < 0)
{
System.out.println("Error: Negative weight.");
System.exit(0);
}
else
weight = initialWeight;
}

public void set(double newWeight)
{
if (newWeight < 0)
{
System.out.println("Error: Negative weight.");
System.exit(0);
}
else
weight = newWeight; //name and age are unchanged.
}

public PetRecord1( )
{
name = "No name yet.";
age = 0;
weight = 0;
}

public String getName( )
{
return name;
}

public int getAge( )
{
return age;
}

public double getWeight( )
{
return weight;
}
}
[ November 28, 2004: Message edited by: Rose Evans ]
Barry Higgins
Ranch Hand

Joined: Jun 05, 2003
Posts: 89
Right you have all the ingrediants if you look through the previous posts. I've just put everything you have there together for you. All you are doing initially is creating as many Pets as you want and putting them in an array. You can then put all your operations on them. As far as I can see you shouldn't have to touch you Pet class again. All the work is going to take place in PetRecord1. Have a look at getOldest() and if you follow what's going on there you should be able to write your other functions quite easily.



Also when you submit code in future will surround them with code tags it'll preserve the code indentation.

Hope this help.
Barry
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Displaying things...need help badly!! Please!