Question: Please translate this code from Java to C++. //Filename TestAutomobiles.java //Additional Class Automobile import java.util.Scanner; public class TestAutomobiles { public static void main(String[] args) throws
Please translate this code from Java to C++.
//Filename TestAutomobiles.java //Additional Class Automobile import java.util.Scanner; public class TestAutomobiles { public static void main(String[] args) throws java.lang.Exception {
//Input Automobile information Scanner inputDevice = new Scanner(System.in); System.out.println("Vehicle Make: "); String make = inputDevice.nextLine(); System.out.println("Vehicle Model: "); String model = inputDevice.nextLine();
System.out.println("Vehicle VIN: "); String vin = inputDevice.nextLine();
System.out.println("Vehicle Color: "); String color = inputDevice.nextLine();
System.out.println("Vehicle ID: "); int id = inputDevice.nextInt();
System.out.println("Vehicle Year: "); int year = inputDevice.nextInt();
System.out.println("Vehicle MPG: "); int mpg = inputDevice.nextInt(); //Creating the automobile class object /*Test values: Automobile obj1=new Automobile (1,2013,42,0,"Volkswagen","Jetta TDI","1VW3L8AJXDC679023","Silver");*/ Automobile obj1=new Automobile(id,year,mpg,0,make,model,vin,color);
//Calling the method for the objects and displaying values for confirmation System.out.println("Vehicle 1"); System.out.println("Automobile Make: " +obj1.getMake()); System.out.println("Automobile Model: " +obj1.getModel()); System.out.println("Automobile Color: " +obj1.getColor()); System.out.println("Automobile ID Number: " +obj1.getID()); System.out.println("Automobile Year: " +obj1.getYear()); System.out.println("Automobile VIN Number: " +obj1.getVin_number()); System.out.println("Automobile Miles per Gallon: " +obj1.getMiles_p_gallon()); System.out.println("Automobile Starting Speed is: "+obj1.getSpeed() + " Mph"); inputDevice.nextLine(); //Used a boolean loop to assign the two methods for Accelerate and Brake boolean endLoop = false; while (!endLoop) { System.out.println("Type A to accelerate or" + " B to brake or" + " S to stop");
//if...else parameters for the loop char choice = inputDevice.nextLine().charAt(0); if(choice == 'A' || choice == 'a') obj1.Accelerate(); else if(choice == 'B' || choice == 'b') obj1.Brake(); if(obj1.getSpeed() <= 0) endLoop = true; else if(choice == 'S' || choice == 's') endLoop = true;
System.out.println("Automobile Current Speed: "+obj1.getSpeed() + " Mph"); } } } //Second Class class Automobile { //Declared the variables int ID, year, miles_p_gallon, speed; String make, model, vin_number, color;
//Added the default constructor Automobile() { ID = 0; year = 0; miles_p_gallon = 0; speed = 0; make = "NO MAKE"; model = "NO MAKE"; vin_number = "NO MAKE"; color = "NO MAKE"; }
//Identified the parameters for the constructor Automobile(int id,int y,int m_p_g,int s,String mak,String mod,String vin_num,String col) { ID = setID(id); year = setYear(y); miles_p_gallon = setMiles_p_gallon(m_p_g); speed = s; make = mak; model = mod; vin_number = vin_num; color = col; }
//Set values for the mutator methods int setID(int id) { if(id < 0 || id > 9999) id = 0; return id; }
int setYear(int y) { if(y < 2000 || y > 2017) y = 0; return y; }
int setMiles_p_gallon(int m_p_g) { if(m_p_g < 10 || m_p_g > 60) m_p_g = 0; return m_p_g; }
//Separated the accessor methods int getID(){return ID;} int getYear(){return year;} int getMiles_p_gallon(){return miles_p_gallon;} int getSpeed() {return speed;} String getMake(){return make;} String getModel(){return model;} String getVin_number(){return vin_number;} String getColor(){return color;} void Accelerate(){speed += 5;} void Brake(){speed -= 5;}
//Overriding the Accelerate and Brake methods void Brake(int deSpeed){speed -= deSpeed;} void Accelerate(int incSpeed){speed += incSpeed;} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
