Question

In: Computer Science

We will make some changes to our first program. If you recall we began with, A...

We will make some changes to our first program. If you recall we began with, A car's gas mileage or miles-per-gallon (MPG) can be calculated with the following Formula: MPG = Miles Driven / Gallons of gas used. Write a class called Mileage. The Mileage class should have two private member variables called miles and gallons of type double. The class should have four public methods: setMiles and setGallons should use void return types; getMiles and getGallons should use double return types. It should have one more method called getMPG that performs the math calculation and returns the double MPG Write a program called MPGMain that asks the user for the number of miles driven and the gallons of gas used. It should call the Mileage class to calculate the car's MPG. The class should return the MPG to the MPGMain where it was called and display the value on the screen. (Format the display and limit the miles-per-gallon to 2 decimal places) in JAVA programming language

Solutions

Expert Solution

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Mileage.java :

//java class
public class Mileage {
   //member variables
   private double miles;
   private double gallons;
   //public methods
   //setter methods
   public void setMiles(double m)
   {
       this.miles=m;//set miles
   }
   public void setGallons(double g)
   {
       this.gallons=g;//set gallons
   }
   //getter methods
   public double getMiles()
   {
       return this.miles;//return miles
   }
   public double getGallons()
   {
       return this.gallons;//return gallons
   }
   //method to calculate MPG
   public double getMPG()
   {
       return getMiles()/getGallons();//return MPG
   }
  

}
*******************************

MPGMain.java :

import java.util.*;//import package
//Java class
public class MPGMain {
//entry point , main method
   public static void main(String[] args) {
       //create object of scanner class
       Scanner sc=new Scanner(System.in);
       //creating object of Mileage class
       Mileage mileage=new Mileage();
       //asking number of miles drive
       System.out.print("Enter number of miles drive : ");
       mileage.setMiles(sc.nextDouble());//read and set mileage
       //asking number gallons of gas used
       System.out.print("Enter number of gallons of gas used : ");
       mileage.setGallons(sc.nextDouble());//read and set gallons
       //call method getMPG() and display MPG
       System.out.printf("miles-per-gallon (MPG) : %.2f",mileage.getMPG());
   }

}
======================================================

Output : Compile and Run MPGMain.java to get the screen as shown below

Screen 1 :MPGMain.java

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.


Related Solutions

Please make the following changes to the following code: #This program takes in the first and...
Please make the following changes to the following code: #This program takes in the first and last name and creates a userID printing first letter of first name and first 7 characters of last name #by Abi Santo #9/20/20 def main(): print("This Program takes your first and last name and makes a User ID") f_name = str(input("Please enter your first name ")).lower() l_name = str(input("Enter your last name: ")).lower() userID = str(f_name[:1]+str(l_name[:7])) print(userID) main() Call the function createUserName(). This function...
: Recall the airplane cargo problem we have discussed in our first lecture. An air-freight company...
: Recall the airplane cargo problem we have discussed in our first lecture. An air-freight company has 8 adjacent positions on its Boeing-727 aircraft for freight containers. The weights of this containers depend on what they are carrying. and company statistics indicate that %7 of the containers are classified as ”heavy”. While heavy containers are not inherently dangerous, having two such containers next to each other is considered dangerous should the plane encounter a wind gust. Understandably, company wants to...
Recall the airplane cargo problem we have discussed in our first lecture. An air-freight company has...
Recall the airplane cargo problem we have discussed in our first lecture. An air-freight company has 8 adjacent positions on its Boeing-727 aircraft for freight containers. The weights of this containers depend on what they are carrying. and company statistics indicate that %7 of the containers are classified as ”heavy”. While heavy containers are not inherently dangerous, having two such containers next to each other is considered dangerous should the plane encounter a wind gust. Understandably, company wants to know...
First make the changes in P82.cpp and call the new program ex82.cpp. Compile and run the...
First make the changes in P82.cpp and call the new program ex82.cpp. Compile and run the program and make sure it produces the correct results. Here is what you need to do for the exercise:     Overload the % operator such that every time you use it, it takes two objects of type AltMoney as its arguments and returns:             a) 5% of the difference between the income and expenditure, if income is larger than the expenditure             b) -2%...
Part (b): Reversing the order of bits in a word Recall that in our course we...
Part (b): Reversing the order of bits in a word Recall that in our course we define a word to be a 32-bit sequence (i.e., four consecutive bytes). For some algorithms it is useful to have a reversed version of that 32-bit sequence. (The deeply curious can read a brief description about such use in Fast Fourier Transform algorithm implementations by visiting Wikipedia at this link: http://bit.ly/2rnvwz6 ). Your task for part (b) is to complete the code in reverse.asm...
In this exercise, the first of a series, we will make connections between the physics you...
In this exercise, the first of a series, we will make connections between the physics you have been learning (in this case, kinematics) and how it is used by people in their work and research. Today we consider an example from kinesiology research, based on a 2000 paper from the Journal of Measurement in Physical Education and Exercise Science (nota bene: you do not have to read this paper, or any of the hyperlinks to follow the exercise – it...
Give me some examples what changes impact are you haping to make in the field of...
Give me some examples what changes impact are you haping to make in the field of health care
Write a program in C++ that will make changes in the list of strings by modifying...
Write a program in C++ that will make changes in the list of strings by modifying its last element. Your program should have two functions: 1. To change the last element in the list in place. That means, without taking the last element from the list and inserting a new element with the new value. 2. To compare, you need also to write a second function that will change the last element in the list by removing it first, and...
When we first began discussion sociology in this course, we talked about the idea of the...
When we first began discussion sociology in this course, we talked about the idea of the "sociological imagination." This is when we can see beyond our personal opinions and experiences in order to get a fuller perspective on how society works. Describe how the sociological imagination has impacted your life, OR provide an example of how the study of human behavior relates to your future professional field.
In this assignment we are not allowed to make changes to Graph.h it cannot be changed!...
In this assignment we are not allowed to make changes to Graph.h it cannot be changed! I need help with the Graph.cpp and the Driver.cpp. Your assignment is to implement a sparse adjacency matrix data structure Graph that is defined in the header file Graph.h. The Graph class provides two iterators. One iterator produces the neighbors for a given vertex. The second iterator produces each edge of the graph once. Additionally, you must implement a test program that fully exercises...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT