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

Java help, I have posted the instruction and my code so far. I am have an except

ID: 3580282 • Letter: J

Question

Java help, I have posted the instruction and my code so far. I am have an exception error so my code with not run

Car Class
doors: int
// must be 2, 4, or 5 or throws IllegalArgumentException
model: String
// At least 1 character long
// No more than 60 characters long
// If only 1 character long, it must be a letter
// If 2 or 3 characters, must start with a letter
price: float
// numeric value with fractional part
// not less than zero

---Car----

public class Car implements Comparable
{
private int doors;
private String model;
private float price;

//The default constructor
public Car()
{
  
}

//@param doors
// @param model
//@param price
public Car(int doors, String model, float price)
{
this.doors = doors;
this.model = model;
this.price = price;
}

//@return
public int getDoors()
{
return doors;
}
public String getmodel()
{
return model;
}
public float getprice()
{
return price;
}

//Concatenate return string of Car: attributes and values
//@return
@Override
public String toString()
{
return "Car{" + "Doors: " + doors + ", Model: " + model + ", Price: " + price + "}";
}

//@param o
//@return
@Override
public int compareTo(Object o)
{
Car p = (Car) o;
return this.toString().compareTo(p.toString());
}

public Object getModel() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

public void setModel(String model) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}

---CarTest-----

public class CarTest
{
static Car[] Cars = new Car[4];
public static void main(String[] args)
{
CarCollection();
printCarArray();
printSortedCarArray();   
}

// car attributes int Doors, string Model, float Price and their values
//for loop to run car array of 4, 0-3
//Concatenate Car Doors, Model and Price
public static void CarCollection()
{
int [] Doors = {4,2,5,3};
String [] Model = {"Focus","Impala","Cavalier","Pinto"};
float [] Price = {5000,400,2600,50};
  
for (int i = 0; i < 4; i++)
{
Cars[i] = new Car(Doors[i],Model[i],Price[i]);
}
}
//printCarArray for loop to display unsorted array
public static void printCarArray()
{
System.out.println("Unsorted car array");
for (Car c: Cars) {
System.out.println(c);
}
}
//printCarArray for loop to display sorted array
public static void printSortedCarArray()
{
Arrays.sort(Cars);
System.out.println("Sorted car array");
for (Car c:Cars)
{
System.out.println(c);
}
}   
}

Explanation / Answer

package snippet;

public class Car implements Comparable
{
private int doors;
private String model;
private float price;

//The default constructor
public Car()
{
  
}

//@param doors
// @param model
//@param price
public Car(int doors, String model, float price) throws Exception
{
  
       if(doors==2||doors==4||doors==5)
   {
       this.doors = doors;
  
   }
   else
   {
       throw new IllegalArgumentException("Doors must be 2,4 or 5");
   }
      
       if(model.length()>60)
       {
       throw new Exception("Length cannot be greater than 60");

       }
       else
       {
           if(model.length()==1)
           {
               char ch=model.charAt(0);
               if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
               {
                   this.model = model;
               }
               else
               {
                   throw new Exception("First letter must be letter cannot be digit");
                  
               }
              
              
           }
           else if(model.length()==2||model.length()==3)
           {
               char ch=model.charAt(0);
               if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
               {
                   this.model = model;
               }
               else
               {
                   throw new Exception("First letter must be letter cannot be digit");
                  
               }
           }
           else
           {
               this.model = model;
           }
          
       }
      
       if(price<0)
       {
           throw new Exception("Price caanot be less than 0");
       }
       else
       {
this.price = price;

       }
  
  
}

//@return
public int getDoors()
{
return doors;
}
public String getmodel()
{
return model;
}
public float getprice()
{
return price;
}

//Concatenate return string of Car: attributes and values
//@return
@Override
public String toString()
{
return "Car{" + "Doors: " + doors + ", Model: " + model + ", Price: " + price + "}";
}

//@param o
//@return
@Override
public int compareTo(Object o)
{
Car p = (Car) o;
return this.toString().compareTo(p.toString());
}
public Object getModel() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void setModel(String model) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}

================================================================

CarTest.java

package snippet;

import java.util.Arrays;

public class CarTest
{
static Car[] Cars = new Car[4];
public static void main(String[] args)
{
   try
   {
CarCollection();
printCarArray();
printSortedCarArray();
   }
   catch (Exception e)
   {
       System.out.println(e.getMessage());
   }
  
}

// car attributes int Doors, string Model, float Price and their values
//for loop to run car array of 4, 0-3
//Concatenate Car Doors, Model and Price
public static void CarCollection() throws Exception
{
int [] Doors = {1,2,5,2};
String [] Model = {"1Focus","Impala","Cavalier","Pinto"};
float [] Price = {5000,400,2600,50};
  
  
  
for (int i = 0; i < 4; i++)
{
Cars[i] = new Car(Doors[i],Model[i],Price[i]);
}
}
//printCarArray for loop to display unsorted array
public static void printCarArray()
{
System.out.println("Unsorted car array");
for (Car c: Cars) {
System.out.println(c);
}
}
//printCarArray for loop to display sorted array
public static void printSortedCarArray()
{
Arrays.sort(Cars);
System.out.println("Sorted car array");
for (Car c:Cars)
{
System.out.println(c);
}
}   
}

==========================================================================

Output:

Doors must be 2,4 or 5

==========================================

I have added exception for all doors,length of model name and price

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote