Help with java homework

0 Zack Shea · September 27, 2015
I have watched bucky's java tutorials and programmed all of his examples up to video 39 and I am about to lose my mind trying to complete this Comp Sci homework.  I emailed the teacher and he basically said figure it out.  Thanks Professor, I only pay about $3000 to take this class...

He said we need to write a class that will make the following code run and output the following exactly.  I never posted on this forum and I am grateful for any help anyone provides...


In this homework, you will write a Car.java file in order to make HW2tester.java (see the attachment) file work. And the output is

required to be exactly 

This car is Chevy, year 2005, price 3000
This car is Ford, year 2011, price 22000
This car is Audi, year 2012, price 25000
This car is Cadillac, year 2005, price 3000
The total car number is: 4
The total car number is: 4
The total car number is: 4
The total car number is: 4
Total car number 4 must be counted.
You are not allowed to make any modifications on HW2Tester.java.

Here is the code he provided.


public class HW2tester
{
public static void main(String[] args)
{

Car car2 = new Car("Ford", 2013, 20000);
Car car3 = new Car("Audi", 2012, 25000);
Car car4 = new Car();

car2.setPrice(22000);
car2.setYear(2011);

car4.setBrand("Cadillac");

System.out.println("This car is " + car1.getBrand() + ", year " + car1.getYear() + ", price " + car1.getPrice());
System.out.println("This car is " + car2.getBrand() + ", year " + car2.getYear() + ", price " + car2.getPrice());
System.out.println("This car is " + car3.getBrand() + ", year " + car3.getYear() + ", price " + car3.getPrice());
System.out.println("This car is " + car4.getBrand() + ", year " + car4.getYear() + ", price " + car4.getPrice());

System.out.println("The total car number is: " + car1.getNumber());
System.out.println("The total car number is: " + car2.getNumber());
System.out.println("The total car number is: " + car3.getNumber());
System.out.println("The total car number is: " + car4.getNumber());

}
}

Post a Reply

Replies

Oldest  Newest  Rating
0 tony ding · September 28, 2015
OK HERE! I couldn't get the output "Cadillac, year 2005, price 3000" yet, but I did the rest. I'll leave it for someone else to do, but if I find a solution, I'll post it.
public class Car {
String brand;
int year;
int price;
static int number;
static Car car1 = new Car("Chevy", 2005, 3000);
public Car(String brand, int year, int price) {
this.brand = brand;
this.year = year;
this.price = price;
number++;
}

public Car() {
number++;
}

public static void main(String[] args){


}
public void setPrice(int p) {
this.price = p;
}

public void setYear(int i) {
// TODO Auto-generated method stub

this.year = i;
}

public void setBrand(String brand) {
// TODO Auto-generated method stub

this.brand = brand;
}

public String getBrand() {
return brand;
}

public int getYear() {
return year;
}

public int getPrice() {
return price;
}
public int getNumber() {

return number;

}

}
0 Zack Shea · September 28, 2015
Thank you so much. I am struggling mightily with this college course in Java. My professor has a thick accent and really only talks about theory and doesn't really get into actual examples in class. I was able to figure out the first few homework assignments with Bucky's tutorials, but this one had me cursing at the screen. I really appreciate the help, sir. 
0 Bob Kako · September 28, 2015
You could set the year and price in the default constructor like this:

public Car(){
this.year = 2005;
this.price = 3000;
number++;
}

... it's a bit of a hack but since you know the other calls to create a Car() have values passed in they will overwrite the 2005 & 3000.  
I used and Enumerated type for car1 (static Car car1 = new Car("Chevy", 2005, 3000); gave me a bunch of errors).  I am using Eclipse so I am not sure if I have a setting wrong that would allow me to see car1 in the car.java file?  
0 Zack Shea · September 28, 2015
Thank you so much, it really means a lot that you took the time to help out.  I have been struggling mightily with this assignment.  My professor really only talks about programming theory and really doesn't get into the actual coding examples.  It is hard for me to apply what he is teaching me.  I have watched Bucky's videos on java and he has gotten me through a test and 2 homework assignments.

Thanks again,
0 Gary Whitney · September 28, 2015
It was really nice that someone took the time to write code for you.
I am going to suggest you get the book Java all in one for Dummies in a DRM PDF format. The PDF will let you search the entire book. I got mine from O'Reilly Media.
0 Zack Shea · September 29, 2015
there is a line of code that didn't paste over in HW2tester.java in the code box above. (in red)

Car car1 = new Car();
Car car2 = new Car("Ford", 2013, 20000);
Car car3 = new Car("Audi", 2012, 25000);
Car car4 = new Car();

I keep getting the following when I run the HW2tester.java

This car is null, year 0, price 0
This car is Ford, year 2011, price 22000
This car is Audi, year 2012, price 25000
This car is Cadillac, year 0, price 0
The total car number is: 4
The total car number is: 4
The total car number is: 4
The total car number is: 4
0 Zack Shea · September 29, 2015
Thanks Gary, I will check that out. 
  • 1

Java / Android Development

107,058 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator