Question

please help ASAP me with this JAVA program! and please do correct INDENTATION with NO COMMENTS!...

please help ASAP me with this JAVA program! and please do correct INDENTATION with NO COMMENTS! thank you!

Using enum and array of objects

Use the enumerated type of: enum CarType { PORCHE, FERRARI, JAGUAR, UNKNOWN }

And place it in its own file. Then make a Car class that has the following:

            Make of CarType

            Year, string or integer

            Mileage of integer

Place in its own file like so:

            porche

            2010

            51119

            ferrari

            2014

            9182

            porche

            2015

            3987

            jaguar

            2011

            65123

Create a program that will have an array of four cars of Car class. Have it use a while loop to read in the information per car (use the enhanced switch to convert the string of the make into CarType – don’t forget about the phantom newline), allocate each Car object and put the information into that object. Then have a for loop go through the array, print the Car objects, and total the mileage. After the for loop ends, print the average mileage.

Sample run:

            Honest George’s Used Sports Cars

            Car 1 is a 2010 FERRARI with 51119 miles on it

            Car 2 is a 2014 JAGUAR with 9182 miles on it

            …

            Average mileage is 32352.8

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

/*
* The java program that demonstrates
* the Car class creating an array of Car
* type of size,4 . Then set create four Car objects
* and print the four-car objects and then average mileage
* */

//CarTester.java
public class CarTester
{
   public static void main(String[] args)
   {
       final int size=4;
       double totalmileage=0;
       //create an array of Car
       Car[] cars=new Car[size];
      
       //create four Car objects
       cars[0]=new Car("porche", 2010, 51119);
       cars[1]=new Car("ferrari", 2014, 9182);
       cars[2]=new Car("porche", 2015, 3987);
       cars[3]=new Car("jaguar", 2011, 65123);
       System.out.println("\tHonest George’s Used Sports Cars");
       //print the cars
       for (int idx = 0; idx < cars.length; idx++) {
           System.out.println("Car "+(idx+1)+" is a "+cars[idx].toString());
           totalmileage+=cars[idx].getMileage();
       }
       //display average mileage
       System.out.printf("Average mileage is %.1f",totalmileage/cars.length);
   }
} //end of class

----------------------------------------------------------------------------------------------------------------------------------------


//Car.java
public class Car
{
   //declare instance variable
   private CarType make; //CarType
   private int year;
   int mileage;
   /*constructor*/
   public Car(String type, int year, int mileage)
   {
       this.year=year;
       this.mileage=mileage;
       //set make using type
       switch (type) {
       case "porche":
           make=CarType.PORCHE;
           break;
       case "ferrari":
           make=CarType.FERRARI;
           break;
       case "jaguar":
           make=CarType.FERRARI;
           break;
       }
   }
   //Override
   public String toString() {  
       return String.format("%d %s with %d miles on it",
               year,make,mileage);
   }
   //Get mileage value
   public int getMileage() {
       return mileage;
   }
   public void setMileage(int mileage) {
       this.mileage = mileage;
   }
} //end of the class Car
----------------------------------------------------------------------------------------------------------------------------------------

//CarType.java
enum CarType { PORCHE, FERRARI, JAGUAR, UNKNOWN }

----------------------------------------------------------------------------------------------------------------------------------------

Sample Output:

Add a comment
Know the answer?
Add Answer to:
please help ASAP me with this JAVA program! and please do correct INDENTATION with NO COMMENTS!...
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
  • Please help me do these two programs in JAVA Program 1 Create a program that will...

    Please help me do these two programs in JAVA Program 1 Create a program that will create an ArrayList of Strings. Then input names until “quit” and use the enhanced for loop to print out the names. Program 2 Using enum and array of objects Use the enumerated type of: enum CarType { PORCHE, FERRARI, JAGUAR, UNKNOWN } And place it in its own file. Then make a Car class that has the following:             Make of CarType             Year,...

  • please need help with the java program A micro car rental company. package yourLastName.cis4110.assignment5; Please create...

    please need help with the java program A micro car rental company. package yourLastName.cis4110.assignment5; Please create an enum type, say Cartype, featuring all rental car types -- SUBCOMPACT, COMPACT, MIDSIZE, FULLSIZE (You may put the enum definition in Car class) class Car Instance variables: - private String plateNum - private Cartype carType - private boolean availability Member methods:             - Please define all setters and getters class RentACar - This is a five car system for prototyping instance variables:...

  • Need help doing program In bold is problem E2 #include<iostream> #include<string> #include<vector> using namespace std; //Car...

    Need help doing program In bold is problem E2 #include<iostream> #include<string> #include<vector> using namespace std; //Car class //Defined enum here enum Kind{business,maintenance,other,box,tank,flat,otherFreight,chair,seater,otherPassenger}; //Defined array of strings string KIND_ARRAY[]={"business","maintenance","other,box","tank,flat","otherFreight","chair","seater","otherPassenger"}; class Car { private: string reportingMark; int carNumber; Kind kind; //changed to Kind bool loaded; string destination; public: //Defined setKind function Kind setKind(string knd) { for(int i=0;i<8;i++) //repeat upto 8 kinds { if(knd.compare(KIND_ARRAY[i])==0) //if matched in Array kind=(Kind)i; //setup that kind } return kind; } //Default constructor Car() { } //Parameterized constructor...

  • I need an OUTLINE ONLY (pseudocode/comments). DO NOT DO THE PROGRAMMING ASSIGNMENT. Part I: PA3 Outline...

    I need an OUTLINE ONLY (pseudocode/comments). DO NOT DO THE PROGRAMMING ASSIGNMENT. Part I: PA3 Outline (10 points). Create an outline in comments/psuedocode for the programming assignment below. Place your comments in the appropriate files: main.cpp, functions.h, dealer.cpp, dealer.h, dealer.cpp (as noted below). Place into a file folder named LastnamePA3, the zip the content and hand in a zip file to Canvas. Part II: PA3: Car Dealership (40 points) For Programming Assignment 3 you will be creating a program to...

  • In Java and make sure the extra credit is in the program aswell

    in Java and make sure the extra credit is in the program aswell Submission Instructions Submit by midnight on the due date and include your submission (a zip file) as an attachment. Assignment Design a class named Animal. An Animal object has the following data members: name (String), age (integer), amount-food-eaten-per-day (integer, representing ounces) and a preferred food (String). Create a toStringO method in the Animal class. Create a no-arg constructor method that sets default values to the 4 data...

  • Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class...

    Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class Car {    private int year; private String model; private String make; int speed; public Car(int year, String model, String make, int speed) { this.year = year; this.model = model; this.make = make; this.speed = speed; } public Car() { } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getModel() { return model; }...

  • PLease, I need help with this Code. PLease create correctly documentations comments for better understanding. This...

    PLease, I need help with this Code. PLease create correctly documentations comments for better understanding. This is the input: JAV001 ARRIVE JAV002 ARRIVE JAV003 ARRIVE JAV004 ARRIVE JAV005 ARRIVE JAV001 DEPART JAV004 DEPART JAV006 ARRIVE JAV007 ARRIVE JAV008 ARRIVE JAV009 ARRIVE JAV010 ARRIVE JAV011 ARRIVE JAV012 ARRIVE JAV013 ARRIVE JAV014 ARRIVE JAV006 DEPART JAV014 DEPART JAV013 DEPART JAV005 DEPART JAV015 ARRIVE JAV010 DEPART JAV002 DEPART JAV015 DEPART JAV014 DEPART JAV009 DEPART JAV003 DEPART JAV008 DEPART JAV007 DEPART JAV012 DEPART JAV011...

  • This is a java homework for my java class. Write a program to perform statistical analysis...

    This is a java homework for my java class. Write a program to perform statistical analysis of scores for a class of students.The class may have up to 40 students.There are five quizzes during the term. Each student is identified by a four-digit student ID number. The program is to print the student scores and calculate and print the statistics for each quiz. The output is in the same order as the input; no sorting is needed. The input is...

  • *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J...

    *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J IN CLASS (IF IT DOESNT MATTER PLEASE COMMENT TELLING WHICH PROGRAM YOU USED TO WRITE THE CODE, PREFERRED IS BLUE J!!)*** ArrayList of Objects and Input File Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an ArrayList<Car> list . Program should work for input file containing info...

  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

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