Question

How to write a program that create a car class with: instance variables name and mpg...

How to write a program that create a car class with:

\bullet instance variables name and mpg (make public)

\bullet No need for a main() method

In a separate file, create a CompareCars class with a main()method

\bullet create 2 instances of car using new

\bulletset the name and mpg for each

\bulletinstance variables are public, so use the name an mpg to access the instance variables

\bullet compare the fuel efficiency of your 2 cars and print out which is more efficient to the screen.

finally change your variable from public to private

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE IN JAVA:

Car.java File:

public class Car {

//declaring instance variables

String name ;

double mpg ;

//getters and setters

String getName() {

return this.name ;

}

void setName(String name) {

this.name = name ;

}

double getMpg() {

return this.mpg ;

}

void setMpg(double mpg) {

this.mpg = mpg ;

}

}

CompareCars.java File:

public class CompareCars {

public static void main(String[] args) {

//creating instances of 2 cars

Car car1 = new Car();

Car car2 = new Car();

//setting name and mpg to the cars

car1.setName("BMW");

car1.setMpg(45.34);

car2.setName("Audi");

car2.setMpg(50.23);

//comparing the cars and displaying the result

if(car1.getMpg()>car2.getMpg())

System.out.println(car1.getName()+" is fuel efficient car");

else if(car1.getMpg()<car2.getMpg())

System.out.println(car2.getName()+" is fuel efficient car");

else

System.out.println("Both cars are equal in fuel efficient");

}

}

OUTPUT:

Problems @ Javadoc 다 Declaration console X <terminated> CompareCars [Java Application] /Library/Java/JavaVirtualMachines/jdk1

Add a comment
Know the answer?
Add Answer to:
How to write a program that create a car class with: instance variables name and mpg...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • Answer the following: 1.        Create a Class Car with the following instance variables: model (String), Brand...

    Answer the following: 1.        Create a Class Car with the following instance variables: model (String), Brand (String), maxspeed (double) Note: use encapsulation (all variables are private). (3 Marks) 2.        Create a non-default constructor for Class Car which takes 3 parameters. (2 Marks) 3.        Create a get and set methods for instance variable model variable only. (2 Marks) 4.        Create PrintInfo() method which prints all three instance variables. (2 Marks) 5.        Create a subclass GasCar with instance variable TankSize (double).. (2...

  • Create a class in Java with instance variables to hold the name, street address, city, and...

    Create a class in Java with instance variables to hold the name, street address, city, and state for a user. These instance variables should be private. You should enter the name and address in the nameAddress method. You should enter the city and state in the cityState method. In the main method, print the complete address using the method printAddress. You should not use the static keyword except for the main method. Your output should be similar to the output...

  • Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name...

    Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name Access modifier: private Data Type: String Name: currentWeight Access modifier: private Data Type: double Name: maximumWeight Access modifier: private Data Type: double Constructors Name: Bag Access modifier: public Parameters: none (default constructor) Task: sets the value of the instance variable name to the empty string sets the value of the instance variable currentWeight to 0.0 sets the value of the instance variable maximumWeight to...

  • In one file create an Employee class as per the following specifications: three private instance variables:...

    In one file create an Employee class as per the following specifications: three private instance variables: name (String), startingSalary (int), yearlyIncrement (int) a single constructor with three arguments: the name, the starting salary, and the yearly increment. In the constructor, initialize the instance variables with the provided values. get and set methods for each of the instance variables. A computation method, computeCurrentSalary, that takes the number of years in service as its argument. The method returns the current salary using...

  • You may not add any instance variables to any class, though you may create local variables...

    You may not add any instance variables to any class, though you may create local variables inside of a method to accomplish its task. No other methods should be created other than the ones listed here.    Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String [ ] Name: courseCreditArray Access...

  • You may not add any instance variables to any class, though you may create local variables...

    You may not add any instance variables to any class, though you may create local variables inside of a method to accomplish its task. No other methods should be created other than the ones listed here. (I need step 2 please.) Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String [...

  • Use an accessor and mutator as follows: Remove the addFuel method from class Car Replace the...

    Use an accessor and mutator as follows: Remove the addFuel method from class Car Replace the call to addFuel in the main program with a call to the accessor and mutator for the fuel amount to achieve the same effect (add 5 gallons of fuel to the existing fuel in the Toyota Camry). Test your program again to make sure it works and produces the same output. Part 2: Add functions to remove duplicate code * In the main program,...

  • public class Car {    /* four private instance variables*/        private String make;   ...

    public class Car {    /* four private instance variables*/        private String make;        private String model;        private int mileage ;        private int year;        //        /* four argument constructor for the instance variables.*/        public Car(String make) {            super();        }        public Car(String make, String model, int year, int mileage) {        super();        this.make = make;        this.model...

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT