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

JAVA QUESTIONS 1. Implement (define) a class named carType.java. A carType has a

ID: 3829083 • Letter: J

Question

JAVA QUESTIONS

1. Implement (define) a class named carType.java. A carType has a model(a String) and a price (a double). It provides the methods to set the price (give the price an initial value), change the price (increase or decrease the price by some value), to return the current price, to set the model value and to return the model value. objects that are declared to be of a carType are given an inital pricep of 0 and an inital name value of the empty string, there is a secong constructor that allows for setting the model and price on instanitation

2. Provide the values for the following java expression (show steps)

a.18 / 5 == 18%5

b. (char)((int)('M')-3)

c. (4 - 3 % 5) * 5 / (5 - 4) / 5

d. 4 - 3 % ( 5 * 5 / 5 - 4) / 5 - 5

e. 4 - 3 % ( 5 * 5 / 5 - 4 / 5 - 5)

Explanation / Answer

carType.java

public class carType {
  
   //Declaring instance variables
   private String model;
   private double price;
  
   //Zero argumented constructor
   public carType() {
       super();
       this.model ="";
       this.price = 0;
   }
  
   //Parameterized constructor
   public carType(String model, double price) {
       super();
       this.model = model;
       this.price = price;
   }
  

   //getters and setters
   public String getModel() {
       return model;
   }
   public void setModel(String model) {
       this.model = model;
   }
   public double getPrice() {
       return price;
   }
   public void setPrice(double price) {
       this.price = price;
   }
  
   /* This method will modify the price by increasing
   * or decreasing based on supplied amount
   */
   public void changePrice(double amount)
   {
       price=price+amount;
   }
  
   //toString method is used to display the contents of an object inside it
   @Override
   public String toString() {
       return "model=" + model + ", price=$" + price;
   }

  
}

_____________________

TestCar.java

import java.util.Scanner;

public class TestCar {

   public static void main(String[] args) {
      
       //Declaring variables
       String model;
       double price,amt;
      
       //Scanner object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);
      
       //Getting the inputs entered by the user
       System.out.print("Enter the Model name :");
       model=sc.next();
      
       System.out.print("Enter the Price :$");
       price=sc.nextDouble();
      
       //Creating the Cartype class object by passing the user entered inputs
       carType ct=new carType(model,price);
      
       //Displaying the Car Info
       System.out.println("Displaying Car Info :"+ct.toString());
      
       //getting the Change amount entered by the user
       System.out.print("Enter Increase or decrease Amount :$");
       amt=sc.nextDouble();
      
       //calling the changePrice method
       ct.changePrice(amt);
      
       //Displaying the Car Info
       System.out.println("Displaying Car Info :"+ct.toString());

   }

}

_______________________

Output:

Enter the Model name :Veyron
Enter the Price :$900000
Displaying Car Info :model=Veyron, price=$900000.0
Enter Increase or decrease Amount :$-12000
Displaying Car Info :model=Veyron, price=$888000.0

______________

2)

a.18 / 5 == 18%5

18/5 = we have to take the coefficient value 3

so,18/5 = 3

18%5 = we have to take the remainder value which is 3

so 18%5=3

Now we are comparing whether those two are equal or not by using double equal to operator.

3==3 which is true

So Ans is True

_____________

b) (char) ((int)('M')-3)

= (char) (77-3) (Ascii velua of 'M' is 77)

= (char) (74) (ASCII value of J is 74)

= J

___________________

c) (4 - 3 % 5) * 5 / (5 - 4) / 5

First we have to computer the values inside the braces

order of precedence is
multiplication (*)
division (/)
modulas (%)
addition (+)
subtraction (-)

(4 - 3 % 5) * 5 / (5 - 4) / 5

= (4-3) * 5 / (1) / 5

= 1 * 5 / 1 / 5

= 5 / 1 / 5

= 5/5

=1

_________________

d) 4 - 3 % ( 5 * 5 / 5 - 4) / 5 - 5

= 4 - 3 % ( 25 / 5 - 4 ) / 5 - 5

= 4 - 3 % (5 - 4 ) / 5 - 5

= 4 - 3 % 1 / 5 - 5

= 4 - 0 - 5

= 4 - 5

=-1

_____________

e. 4 - 3 % ( 5 * 5 / 5 - 4 / 5 - 5)

= 4 - 3 % ( 25 /5 - 4 / 5 -5)

= 4 - 3 % ( 5 - 4 / 5 - 5)

= 4 - 3 % ( 5 - 0 - 5)

= 4 - 3 % (0) ( Here we will get Arithmatic exception divided by zero)

_________________Thank You