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

In Java: Cars: Implement a class Car, which contains the fields: make, e.g. Ford

ID: 3719682 • Letter: I

Question

In Java:

Cars:

Implement a class Car, which contains the fields:

make, e.g. Ford, Subaru, Toyota ...

model, e.g., Escape, Outback, Camry ...

year

MPG miles per gallon

milesDriven, the total number of miles ever driven in this car.

fuelCapacity in gallons, i.e., the size in gallons of the fuel tank.

fuelRemaining, which represents the amount of fuel remaining in the gas tank.

Implement at least the following methods within the Car class:

a constructor, which initializes each of the fields

fillTank(double g), which adds up to g gallons of gas to the fuel tank, but not more than the car's fuel capacity.

drive(double m), which simulates driving m miles in the car, adding to the total number of miles driven, and reducing the amount of gas in the car according to this car's average MPG.

toString( ), which returns a String representation of the car.

getFuelRemaining( ), which returns the amount of fuel left in the tank.

For example, we should be able to do something like the following:

Write a short driver program to test your Car class with a short array of Cars.

Fractions

Write a class used to represent fractions. It should contain fields for numerator and denominator, and it should be able to perform some simple arithmetic operations.

The fields are:

numerator

denominator

Implement at least the following methods:

public Fraction(int n, int d)

Constructor that creates a Fraction with numerator and denominator d. If d is 0, throw an ArithmeticException. This can be done with the statement throw new ArithmeticException();

public int getNum()

Returns the value of the numerator field

public int getDenom()

Returns the value of the denominator field

public void setNum(int n)

Sets the numerator field to the value given in n

public void setDenum(int d)

Sets the denominator field to the value given in d. If d is 0, throw an ArithmeticException.

public Fraction add(Fraction a)

Returns the fraction that is the sum of the subject of the method and a. For example (new Fraction(3,4)).add(new Fraction(1,4)) is 16/16 i.e 1/1 We sum the fractions a/b and c/d as (a*d+b*c)/b*d then reduce.

public boolean equals(Fraction a)

Returns true if the subject of method and argument of call are equal. Fractions a/b and c/d are equal if a*d and b*c are equal or since the fractions are normalized if a==c and b==d.

public String toString()

Returns a String representation of the fraction. For example, if the numerator is 1 and the denominator is 2, the String "1/2" is returned.

Fractions should be stored in reduced form. For example, 2/4, 3/6, 4/8, etc. should all be stored as 1/2. 4/10 should be stored as 2/5. You may use the Euclidean Algorithm (Links to an external site.)Links to an external site. to determine the greatest common divisor so that you can store fractions in reduced form.

Write a simple main driver method to test your fraction class and each of its methods.

Explanation / Answer

Please find my implementation.

Car.java

class Car{
   String make,model;
   int year;
   double MPG,milesDriven;
   private double fuelCapacity,fuelRemaining;
   //constructer
   public Car(String make,String model,int year,double MPG,double fuelCapacity,double fuelRemaining){
       this.make=make;
       this.model=model;
       this.year=year;
       this.MPG=MPG;
       this.fuelCapacity=fuelCapacity;
       this.fuelRemaining=fuelRemaining;
   }
   //fillTank
   public void fillTank(double m){
       //if tank is not full
       if(m+this.fuelRemaining<=this.fuelCapacity){
           this.fuelRemaining=this.fuelRemaining+m;
       }
       //tank is full
       else{
           this.fuelRemaining=this.fuelCapacity;
       }
   }
   //drive function
   public void drive(double m){
       //adding it to milesDriven
       this.milesDriven=this.milesDriven+m;
       //reducting the fuel
       this.fuelRemaining=this.fuelRemaining-(m/this.MPG);
   }
   //getFuelRemaining()
   public double getFuelRemaining(){
       return this.fuelRemaining;
   }
   //toString() method as printCar
   public String toString(){
       String result="";
       result+=(this.make+" "+this.model+" "+this.year);
       result+=(" MPG : "+this.MPG);
       result+=(" Total Capacity: "+this.fuelCapacity);
       result+=(" FuelRemaining: "+this.fuelRemaining);
       result+=(" milesDriven: "+this.milesDriven);
      
       return result;
   }
   public static void main(String args[]){
          Car oldJunker = new Car("Ford", "Pinto", 1972, 17.5, 12, 8); // creates a new Car object
          oldJunker.drive(5); // drives the Car 5 miles
          oldJunker.fillTank(1); // put in a gallon of gas
       System.out.println(oldJunker.getFuelRemaining()); // prints the amount of fuel left
       System.out.println(oldJunker); // prints the attributes of the car to the screen
   }
}

Fraction.java

class Fraction{
   private int numerator,denominator;
   //constructer
   public Fraction(int n,int d){
       check(d);
       this.numerator=n;
       this.denominator=d;
       reduce();
   }
  
   //If d is 0, throw an ArithmeticException
   public void check(int d){
       if(d==0){
           throw new ArithmeticException("Denominator shouldn't be zero");
       }
   }
   //Returns the value of the numerator field
   public int getNum(){
       return this.numerator;
   }
   //   Returns the value of the denominator field

   public int getDenom(){
       return this.denominator;
   }
   //Sets the numerator field to the value given in n

   public void setNum(int n){
       this.numerator=numerator;
   }
   //Sets the denominator field to the value given in d. If d is 0, throw an ArithmeticException.
   public void setDenum(int d)   {
       check(d);
       this.denominator=d;
   }
   //Returns the fraction that is the sum of the subject of the method and a.
   public Fraction add(Fraction a){
       if(a==null){return null;}
      
       Fraction result=new Fraction(1,1);
       result.numerator=this.numerator*a.denominator+this.denominator*a.numerator;
       result.denominator=this.denominator*a.denominator;
       result.reduce();
       System.out.println(result);
       return result;
      
   }
   //Returns true if subject of method and argument of call are equal.
   // Fractions a/b and c/d are equal if a*d and b*c are equal or since the fractions are normalized, if a==c and b==d.
   public boolean equals(Fraction a){
          
       return this.numerator*a.denominator==this.denominator*a.numerator;
   }
   //reducting fraction
   public void reduce(){
       int r=gcd(this.numerator,this.denominator);
       this.numerator/=r;
       this.denominator/=r;
   }
   //gcd
   int gcd(int x, int y)
    {
        int r=0, a, b;
        a = (x > y) ? x : y; // a is greater number
        b = (x < y) ? x : y; // b is smaller number
        r = b;
        while(a % b != 0)
        {
            r = a % b;
            a = b;
            b = r;
        }
        return r;
    }
   //toString()
   public String toString(){
       return this.numerator+"/"+this.denominator;
   }
   public static void main(String args[]){
       Fraction f1=new Fraction(8,4);
       Fraction f2=new Fraction(4,16);
       System.out.println("F1="+f1);
       System.out.println("F2="+f2);
       System.out.println(f1.equals(f2));
       System.out.println(f1+"+"+f2+"="+f1.add(f2));
   }
  
}

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