Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Methods in Java: Okay so I am just very confused and I am frustrating myself mor

ID: 3817840 • Letter: M

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 (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

   }
   }

Explanation / Answer

open class Car {
private int yearModel;
private String make;
private int speed;
/The constructor acknowledge the auto's year model and make as contention
/The constuctor ought to relegate 0 to the speed field
open Car (int yrModel, String carMake)
{
yearModel = yrModel;
make = carMake;
speed = 0;
}
open void setyearModel(int yrModel)
{
yearModel = yrModel;
}
open void setMake (String carMake)
{
make = carMake;
}
open void setSpeed(int carSpeed)
{
speed = carSpeed;
}
open int getYearModel()
{
return yearModel;
}
open String getMake ()
{
return make;
}
open int getSpeed ()
{
return speed;
}
open void AccelerateSpeed (int speed)
{
speed = speed + 5;
}
open void BrakeSpeed (int speed)
{
speed = speed - 5;
}
}
]
[/import javax.swing.JOptionPane;
open class MyNewCar {
open static void main(String[] args) {
Auto myCar = new Car (2010, "Honda");
int speed = myCar.getSpeed();
speed = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Speed" ));
for (int i = 0; i < 5; i++)
{
System.out.println("The" + " + myCar.getYearModel() + " + myCar.getMake() +
" + "is going " );
myCar.AccelerateSpeed(speed);
System.out.println("Your Speed now is: " + speed);
}
speed = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Speed" ));
for (int i = 0; i < 5; i++)
{
System.out.println("The" + " + myCar.getYearModel() + " + myCar.getMake() +
" + "is going " );
myCar.BrakeSpeed(speed);
System.out.println("Your Speed now is: " + speed);
}
}
}