aspose file tools*
The moose likes Beginning Java and the fly likes please help with this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "please help with this" Watch "please help with this" New topic
Author

please help with this

rina francis
Greenhorn

Joined: Oct 11, 2003
Posts: 3
Hi, I'm a newbie to java. I need help on some programs. I've done most of it but pls correct me if I've done it wrong.

1) Write a java application by following the below instructions:
a) Create a class named car that has the data members model, plate, and color.
Ans: public class Car {
char model, plate, color;
}
b)Write a default constructor for car class to intialise the data members of car class.
- model data member of car class will be intialise to "Nissan".
- plate data member of car class will be initialise to "06 BU 482".
- color data member of car class will be initialise to "Red".
Ans: public class car {
public string model=Nissan;
public string plate=06 BU 482;
public string color=Red;
public car{
}
}
c) Write a non-default constructor for car class. The arguments of the non-default constructor will be (char *model, char *plate, char *color) and it will intialise the data members model, plate, and color of class to the given parameters.
Ans: public class Car{
char model, plate, color;
public car(char *model, char *plate, char *color) }
Model=Nissan;
Plate=06 BU 482;
Color=Red;
}
}
d)Write a member function displayCarInfo() that has no argument list. The function will display the members of car class.
Ans: public void displayCarInfo()
}
System.out.printIn("First Car Information:");
System.out.printIn("nModel:Nissan");
System.out.printIn("nPlate:06 BU 482");
System.out.printIn("nColor:Red");
}
e) In main function, create two object by calling one of the constructors of car class and call displayCarInfo() member function for each car object.
Ans: public class MyCar
{
public static void main(string args[]){
Car c1=new car
Car C2=new car(Alfa Romeo, 06 Fun 85, Blue)
}
}
public class Car{
public Car(char model, char plate, char color)
{
public string model=Alfa Romeo;
public string plate=06 Fun 85;
public string color=Blue;
}
}
public void displayCarInfo()
{
System.out.printIn("Second Car Information:");
System.out.printIn("nModel:Alfa Romeo");
System.out.printIn("nPlate:06 Fun 85");
System.out.printIn("nColor:Blue");
}
Example Output:
First Car Information
Model:Nissan
Plate:06 Bu 482
Color:Red
Second Car Information
Model:Alfa Romeo
Plate:06 FUN 85
Color:Blue

2)Create a class named Dog that has the data members name, birth year, weight, height and boolean hungry that define the dog is hungry or not.
Ans: public class Dog {
string name;
int birth year;
int weight;
int height;
boolean hungry;
}
a) Write a member function getName() that return the name of the dog.
Ans: public string getName() {
return name;
}
b) Write a member function display() that displays the dog information.
Ans: public void display()
{
System.out.printIn("Dog Information:");
System.out.printIn("nName:Lucky");
System.out.printIn("nB.Year:1993");
System.out.printIn("nWeight:21");
System.out.printIn("nHeight:50");
System.out.printIn("nHunger:True");
System.out.printIn("nLucky eats");
}
c) Write a member function food() that makes hungry to false to indicate the dog is not hungry.
Ans: public class Food()
{
boolean hungry==false;
}
Example Output:
Dog Information:
Name:Lucky
B.Year:1993
Weight:21
Height:50
Hunger:True
Lucky eats.

Please help me.
don cline
Ranch Hand

Joined: Oct 10, 2003
Posts: 35
Rina,
A couple tips:
b)Write a default constructor for car class to intialise the data members of car class.
- model data member of car class will be intialise to "Nissan".
- plate data member of car class will be initialise to "06 BU 482".
- color data member of car class will be initialise to "Red".
Ans: public class car {
public string model=Nissan;
public string plate=06 BU 482;
public string color=Red;
public car{
}
}
Assuming they want member variables set in the constructor try:
public class car
{
public String model;
public String plate;
public String color;
public car()
{
model="Nissan";
plate="06 BU 482";
color="Red";
}
}
c) Write a non-default constructor for car class. The arguments of the non-default constructor will be (char *model, char *plate, char *color) and it will intialise the data members model, plate, and color of class to the given parameters.
Ans: public class Car{
char model, plate, color;
public car(char *model, char *plate, char *color) }
Model=Nissan;
Plate=06 BU 482;
Color=Red;
}
}
This looks more like C++ than Java. Are you trying to convert to Java from C++? Java does not have pointers, it has references.
If you really mean String rather than char *, you have something like:
public class car
{
public String model;
public String plate;
public String color;
public car(String model, String plate, String color)
{
this.model=model;
this.plate=plate;
this.color=color;
}
}
Good Luck,
Don
rina francis
Greenhorn

Joined: Oct 11, 2003
Posts: 3
Thank you Don for the tips. It really helped. Well no I'm not trying to convert to Java from C++. I have actually been doing C++ and now I've just started Java so theres a lil mix up there. If you don't mind can you help me with the calling display and the boolean thing or can you give me some similar examples.

Regards Rina
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
1) Write a java application by following the below instructions:
a) Create a class named car that has the data members model, plate, and color.
Ans: public class Car {
char model, plate, color;
}

Just to nitpick a bit...
If the instructions are really to name the class car, then name the class car, not Car.
Here, the instructions are to have "data members", but no instructions are provided as to exactly what data types to use. I don't think that char is the best choice to represent model, plate, and color. Perhaps Strings for all three would suffice, as you've used in later answers.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
c) Write a member function food() that makes hungry to false to indicate the dog is not hungry.
Ans: public class Food()
{
boolean hungry==false;
}

food is not Food.
Also, the == operator compares for equality. The = operator is used for assignments.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
b) Write a member function display() that displays the dog information.
Example Output:
Dog Information:
Name:Lucky
B.Year:1993
Weight:21
Height:50
Hunger:True
Lucky eats.
Ans: public void display()
{
System.out.printIn("Dog Information:");
System.out.printIn("nName:Lucky");
System.out.printIn("nB.Year:1993");
System.out.printIn("nWeight:21");
System.out.printIn("nHeight:50");
System.out.printIn("nHunger:True");
System.out.printIn("nLucky eats");
}

This answer is not likely what the assignment writer is looking for. What are all those extra n characters doing? Also, you're supposed to use the variables in the class to display the values, not just type in the values as parts of String literals. So, do System.out.println("Hunger: " + hungry); not System.out.printIn("nHunger:True");
rina francis
Greenhorn

Joined: Oct 11, 2003
Posts: 3
Thanks Dirk for the tips.
 
jQuery in Action, 2nd edition
 
subject: please help with this