Question
Methods in Java: Okay so I am just very confused and I am frustrating myself more and more trying to figure out how methods work
Methods in Java:
Okay so I am just very confused and I am frustrating myself more and more trying to figure out how methods work (I'm sure its really easy).
For this collection assignment I have to create an object (cars) and give them atttributes (brand, year, color, type, etc.). I modified a template slightly and I'm stuck on a method "part".
It says In Cars.java, there is a method called createNextInSeries(). Replace this method (located in other methods) with a method that is appropriate for your chosen collection type. Create some members through hardcoded programming, then display the results.
The original method was to create a new comic witht the same entries, but with the issue number being incremented.
How can I do something similar with my collection type? There are two different classes... one is Cars.java and the other is CarsTester.java.
public class Cars {
/******************************************************************** * ATTRIBUTES * ******************************************************************/ private String brand; private int year; private String color; private String model; private String type; private String trans; private String drive; /******************************************************************** * CONSTRUCTORS * ******************************************************************/ //all necessary parameters for attributes public Cars(String brand_, int year_, String color_, String model_, String type_, String trans_, String drive_) { this.brand = brand_; this.year = year_; this.color = color_; this.model = model_; this.type = type_; this.trans = trans_; this.drive = drive_; } //transmission, drivetrain, and top speed omitted public Cars(String brand_, int year_, String color_, String model_) { this.brand = brand_; this.year = year_; this.color = color_; this.model = model_; this.type = null; //type omitted this.trans = null; // transmission omitted this.drive = null; // drivetrain omitted } //only brand name and year public Cars(String brand_, int year_) { this.brand = brand_; this.year = year_; this.color = null; // color omitted this.model = null; // model omitted this.type = null; // type omitted this.trans = null; // transmission omitted this.drive = null; // drivetrain omitted }
/******************************************************************** * GET and SET METHODS * ******************************************************************/ public void setBrand(String brand_) { this.brand = brand_; } public String getBrand() { return this.brand; } public void setYear(int year_) { this.year = year_; } public int getYear() { return this.year; }
public void setColor(String color_) { this.color = color_; } public String getColor() { return this.color; } public void setModel(String model_) { this.model = model_; } public String getModel() { return this.model; } public void setType(String type_) { this.type = type_; } public String getType() { return this.type; }
public void setTrans(String trans_) { this.trans = trans_; } public String getTrans() { return this.trans; }
public void setDrive(String drive_) { this.drive = drive_; } public String getDrive() { return this.drive; }
/******************************************************************** * OUTPUT METHODS * ******************************************************************/
//Return a string with only the brand and the year public String description() { return this.brand + " -- " + "Year: " + this.year; }
//Return the entire object as a single String public String toString() { String result = this.brand+" -- Year: " + this.year; if (this.color!=null) { result += " Color: "+color; result += " Model: "+this.model; } if (this.type!=null) { result += " Type: "+this.type; } if (this.trans!=null) { result += " Transmission: "+this.trans; } if (this.drive!=null) { result += " Drivetrain: "+this.drive; } result += " "; return result; } /******************************************************************** * OTHER METHODS * ******************************************************************/
/******************************************************************** * createNextInSeries * * Creates a new car with the same entries, but with the year * * incremented. * ******************************************************************/ public Cars createNextInSeries(){ return new Cars(this.brand,this.year+1,this.color,this.model,this.type, this.trans,this.drive); }
===============================================================================
public class CarsTester {
public static void main(String[] args) { // Load initial collection (hardcoded for now) Cars car1 = new Cars("Chevrolet", 2005, "Silver", "Trailblazer"); Cars car2 = new Cars("Ford", 2010, "Black", "Focus"); Cars car3 = new Cars("Dodge", 2007, "Red", "Viper", "Sportscar", "2WD", "Manual");
// Summary info System.out.println("The car collection:"); System.out.println(); System.out.println(car1.description()); System.out.println(car2.description()); System.out.println(car3.description()); System.out.println(); System.out.println("--------------------------------------"); // Full info System.out.println(" Detailed information: "); System.out.println(car1.toString()); System.out.println(car2.toString()); System.out.println(car3.toString()); // Change info car1.setColor("Blue"); car1.setBrand("Ford"); car3.setColor("Green"); car2.setModel("Corvette"); car2.setTrans("2WD"); car2.setType("Sportcar"); car3.setTrans(car2.getTrans()); // Full info, reflecting changes System.out.println(" Detailed information, with changes: "); System.out.println(car1.toString()); System.out.println(car2.toString()); System.out.println(car3.toString()); // Using a new method
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started