Question

In: Computer Science

Java language This is your first homework on objects and classes, and the basics of object-oriented...

Java language

This is your first homework on objects and classes, and the basics of object-oriented programming. For each exercise, your task is to first write the class that defines the given object. Compile it and check it for syntax errors. Then write the “demo class” with the main program that creates and uses the object.

a)You are planning to purchase a new motor boat for cool cruises on Porter’s Lake this summer, but you want to simulate it before you make the purchase. Write a class MotorBoat that represents motorboats. A motorboat has attributes for

  • The capacity of the fuel tank (liters)
  • The amount of fuel in the tank (liters)
  • The maximum speed of the boat (kmph)
  • The current speed of the boat (kmph)
  • The rate of fuel consumption (liters/km)
  • The distance traveled (km)

and the following methods:

  • Constructor that sets the capacity, fuel in the tank, max speed and fuel consumption to the given values and current speed to zero.
  • Method to set the current speed of the boat
  • Method to get the current speed of the boat
  • Method to change the speed of the boat (the method should accept the change which is a positive or negative number and change the current speed. If the change is greater than max speed, it should set the current speed to max speed).
  • Operate the boat for an amount of time in minutes at the current speed (this method should set the distance traveled and reduce the amount of fuel in the tank by the appropriate amount).
  • Refuel the boat with some amount of fuel (if the total amount of fuel is greater than the capacity, set it to the capacity).
  • Return the amount of fuel in the tank
  • Return the distance traveled so far

Note: If the rate of fuel consumption is r and the distance traveled is d, then the amount of fuel remaining in the tank = fuel in tank – r*d

b) Write a tester program that creates a motorboat with capacity 100 liters, fuel in tank 50 liters, max. speed 100 kmph, rate of fuel consumption 1. Set its speed to 25 kmph and operate it for 30 minutes. Print the current speed, distance driven and the fuel remaining in the tank. Then increase the speed by 25, and operate it for 30 minutes. Print the current speed, distance driven and fuel in tank. Your output should be:

Current speed: 25.0 kmph

Distance driven: 12.5 km

Fuel in tank: 37.5 liters

Current speed: 50.0 kmph

Distance driven: 25.0 km

Fuel in tank: 12.5 liters

Test your program to check for other cases as well.

Solutions

Expert Solution

JAVA CODE(MotorBoat.java)

class MotorBoat
{
   double fueltank_capacity;
   double fuel_in_tank;
   double max_speed;
   double current_speed;
   double fuel_consumption;
   double distance_travelled;
  
   //constructor to initialize the object
   public MotorBoat(double capacity,double fuel,double maxi_speed,double fuel_consumed)
   {
       fueltank_capacity=capacity;
       fuel_in_tank=fuel;
       max_speed=maxi_speed;
       current_speed=0;
       fuel_consumption=fuel_consumed;
      
   }
  
   // method to get current speed of boat
   public double getCurrent_speed()
   {
      
       return current_speed;
   }
  
  
   // method to set current speed of boat
   public void setCurrent_speed(double speed)
   {
       current_speed=speed;
   }

   // method to change speed of boat
   public void changeSpeed(double speed)
   {
      
       if(speed > max_speed)
       {
           current_speed=max_speed;
       }
       else
       {
           current_speed+=speed;
       }
      
   }
  
   // method to run boat for given minutes
   public void boatRunning(int minutes)
   {
       distance_travelled= current_speed * ((double)minutes/60);
       fuel_in_tank = fuel_in_tank - fuel_consumption * distance_travelled;
   }
  
   // method to refuel the tank
   public void reFuel(double fuel)
   {
       if(fuel > fueltank_capacity)
       {
           fuel_in_tank=fueltank_capacity;
       }
   }
  
   // method to get fuel in tank
   public double fuelInTank()
   {
       return fuel_in_tank;
   }
  
   // method to find distance travelled
   public double distanceTravelled()
   {
       return distance_travelled;
   }
  
}

JAVA CODE (Demo.java)

public class Demo
{
  
   public static void main(String args[])
   {
       // creating MotorBoat object
       MotorBoat ob=new MotorBoat(100,50,100,1);
      
       // change the boat speed
       ob.changeSpeed(25);
      
       // run the boat for 30 minutes
       ob.boatRunning(30);
      
       //displaying speed, distance travelled, fuel in tank
       System.out.printf("Current Speed: %.1f kmph",ob.getCurrent_speed());
       System.out.printf("\nDistance driven: %.1f km",ob.distanceTravelled());
       System.out.printf("\nFuel in tank: %.1f liters",ob.fuelInTank());
      
       // change the boat speed
       ob.changeSpeed(25);
      
       // run the boat for 30 minutes
       ob.boatRunning(30);
      
       //displaying speed, distance travelled, fuel in tank
       System.out.printf("\nCurrent Speed: %.1f kmph",ob.getCurrent_speed());
       System.out.printf("\nDistance driven: %.1f km",ob.distanceTravelled());
       System.out.printf("\nFuel in tank: %.1f liters\n",ob.fuelInTank());
   }
}

SCREENSHOT FOR OUTPUT:


Related Solutions

Classes and Objects are the central of Object Oriented Programming (O.O.P.) This is a style of...
Classes and Objects are the central of Object Oriented Programming (O.O.P.) This is a style of programming that focuses on using objects to design and build many great applications. What is the difference between a class an an instance of the class? Explain what is constructor? What do you call a constructor that accepts no arguments? Explain the "has-a" relationship can exist between classes. Explain what is the "this" keyword? Do the following: Answer the 4 points with only one...
How algorithms address object-oriented classes and objects. What is the File object? How are File objects...
How algorithms address object-oriented classes and objects. What is the File object? How are File objects used in algorithms? 175 words minumum please :)
Explain what classes and objects are in object - oriented programming. Give an example of each...
Explain what classes and objects are in object - oriented programming. Give an example of each and explain how they work together in a computer program.
Use JAVA BASICS  classes, aggregation and manipulating arrays of objects. I DO NOT WANT THE SAME SOLUTION...
Use JAVA BASICS  classes, aggregation and manipulating arrays of objects. I DO NOT WANT THE SAME SOLUTION WHICH ALREADY EXISTS ON CHEG. DO NO USE ARRAY LIST Scenario: A dog shelter would like a simple system to keep track of all the dogs that pass through the facility. The system must record for each dog: dogId (int) - must be unique name (string) age (double) - cannot be less than 0 or more than 25 breed (string) sex (char) – m...
 This homework will check your ability to declare and use classes and object, and how...
 This homework will check your ability to declare and use classes and object, and how to use both Maps and sets in Java. The assignment will also serve as an introduction to Junit Testing.  Here is the general layout. Some more detailed instructions are also included below. Pay attention to the details and the naming conventions, since we will be running your code through testing code the grading team will develop. That code will require your method signatures...
Homework 3 – Programming with C++ What This Assignment Is About: • Classes and Objects •...
Homework 3 – Programming with C++ What This Assignment Is About: • Classes and Objects • Methods • Arrays of Primitive Values • Arrays of Objects • Recursion • for and if Statements • Insertion Sort 2. Use the following Guidelines • Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc). • User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for...
Why is it more feasible to use Objects and object oriented programming as compared to using...
Why is it more feasible to use Objects and object oriented programming as compared to using method based programs? What are the disadvantages of using only methods in your programs.
How do objects enhance Java? How do objects relate to classes and methods?
How do objects enhance Java? How do objects relate to classes and methods?
When should you use and object-oriented language over a procedural language and vice versa?
When should you use and object-oriented language over a procedural language and vice versa?
in java language. There are three classes for the project, and two inner classes defined in...
in java language. There are three classes for the project, and two inner classes defined in the Cuboid class. Create the following classes: •   Shape. It’s an abstract class with two abstract methods, area and perimeter. •   Rectangle. It’s a concrete class that extends Shape. Implement area() and perimeter(). Implement the compareTo(object) method to sort rectangles by area in ascending order. •   Cuboid. It’s a concrete class that extends Rectangle. A cuboid is a 3D rectangle. The shape has a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT