Question

In: Computer Science

It's Java; the code should be the same as the output sample below; please, thank you....

It's Java; the code should be the same as the output sample below; please, thank you.

Note: create a single-file solution that has multiple class definitions. Also, note that the static main() method should be a member of the Vehicle class.

Create a class called Vehicle that has the manufacturers name (type String), number of cylinders in the engine (type int), and owner (type Person given next). Then, create a class called Truck that is derived from Vehicle and has the following additional properties: the load capacity in tons (type double since it may contain a fractional part) and towing capacity in pounds (type int). Be sure your class has a reasonable complement of constructors, accessor and mutator methods, and suitably defined equals and toString methods. Write a program to test all your methods (see sample run).

The definition of the class Person follows. Completing the definitions of the methods is part of this programming project.

class Person{
private String name;
public Person()
{...}
public Person(String theName)
{...}
public Person(Person theObject)
{...}
public String getName()
{...}
public void setName(String theName)
{...}
public String toString()
{...}
public boolean equals(Object other)
{...}
}

Output sample: interactive session

Enter·manufacturar·name:Chevrolet↵ Enter·number·of·cylinders:8↵ Enter·owner·name:David↵ Enter·load:2800.0↵ Enter·towing·capacity:1000↵ Enter·1·to·change·vehicle·properties,·2·to·print·and·10·to·quit:2↵ main↵ Manufacturer:·Chevrolet·Cylinders:·8·Name:·David·Load·Capacity:·2800.0·lbs·Tow·Capacity:·1000·pounds↵ Enter·1·to·change·vehicle·properties,·2·to·print·and·10·to·quit:1↵ Enter·1·to·change·manufacturar,·2·to·change·cylinders,·3·to·change·owner,·4·to·change·load,·5·to·change·towing·capacity,·or·any·other·number·to·go·back·to·main·menu:4↵ Enter·load:50000↵ Enter·1·to·change·vehicle·properties,·2·to·print·and·10·to·quit:1↵ Enter·1·to·change·manufacturar,·2·to·change·cylinders,·3·to·change·owner,·4·to·change·load,·5·to·change·towing·capacity,·or·any·other·number·to·go·back·to·main·menu:5↵ Enter·towing·capacity:1000↵ Enter·1·to·change·vehicle·properties,·2·to·print·and·10·to·quit:2↵ main↵ Manufacturer:·Chevrolet·Cylinders:·8·Name:·David·Load·Capacity:·50000.0·lbs·Tow·Capacity:·1000·pounds↵ Enter·1·to·change·vehicle·properties,·2·to·print·and·10·to·quit:10↵ ↵

Solutions

Expert Solution

Note: Here whole code and class is done as per your request, but if you have any doubt or issue regarding this you can ask me or post the issue, i will change code according to your need.

Alos include try{...} catch() {...} exception for better run the code.

person.java


public class Person {

        private String name;
        private Person obj;
        
        
        public Person() {
        }
        
        public Person(String theName) {
                this.name = theName;
        }
        
        public Person(Person theObject) {
                this.obj = theObject;
        }
        
        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }
        
        @Override
        public boolean equals(Object obj) {
                return super.equals(obj);
        }

        @Override
        public String toString() {
                return "Owner = " + name + "\n";
        }
}

Truck.java


public class Truck extends Vehicle {

        private double loadCapacity;
        private int towingCapacity;

        public Truck() {
        }

        public Truck(String manufactureName, int numOfCylinder, Person owner, double loadCapacity, int towingCapacity) {
                super(manufactureName, numOfCylinder, owner);
                this.loadCapacity = loadCapacity;
                this.towingCapacity = towingCapacity;
        }
        
        @Override
        public String toString() {
                return super.toString() + "loadCapacity = " + loadCapacity + "\ntowingCapacity = " + towingCapacity;
        }

        public double getLoadCapacity() {
                return loadCapacity;
        }

        public void setLoadCapacity(double loadCapacity) {
                this.loadCapacity = loadCapacity;
        }

        public int getTowingCapacity() {
                return towingCapacity;
        }

        public void setTowingCapacity(int towingCapacity) {
                this.towingCapacity = towingCapacity;
        }
        
        @Override
        public boolean equals(Object obj) {
                return super.equals(obj);
        }
}

Vehicle.java

import java.util.Scanner;

public class Vehicle {

        @Override
        public String toString() {
                return "manufactureName = " + manufactureName + "\nnumOfCylinder = " + numOfCylinder + "\n" + owner.toString();
        }

        private String manufactureName;
        private int numOfCylinder;
        private Person owner;

        public Vehicle() {
        }

        public Vehicle(String manufactureName, int numOfCylinder, Person owner) {
                this.manufactureName = manufactureName;
                this.numOfCylinder = numOfCylinder;
                this.owner = owner;
        }

        public String getManufactureName() {
                return manufactureName;
        }

        public void setManufactureName(String manufactureName) {
                this.manufactureName = manufactureName;
        }

        public int getNumOfCylinder() {
                return numOfCylinder;
        }

        public void setNumOfCylinder(int numOfCylinder) {
                this.numOfCylinder = numOfCylinder;
        }

        public Person getOwner() {
                return owner;
        }

        public void setOwner(Person owner) {
                this.owner = owner;
        }

        public static void main(String[] args) {
                Truck tr;
                Scanner scan = new Scanner(System.in);

                try {

                        System.out.print("Enter manufacturar Name : ");
                        String manufactureName = scan.next();

                        System.out.print("Enter number of cylinders : ");
                        int numOfCylinder = scan.nextInt();

                        System.out.print("Enter owner Name : ");
                        String owner = scan.next();

                        System.out.print("Enter load : ");
                        double loadCapacity = scan.nextDouble();

                        System.out.print("Enter towing capacity : ");
                        int towingCapacity = scan.nextInt();

                        tr = new Truck(manufactureName, numOfCylinder, new Person(owner), loadCapacity, towingCapacity);

                        int ch;
                        do {
                                System.out.println("\nEnter 1 to change vehicle properties, 2 to print and 10 to quit: ");
                                ch = scan.nextInt();

                                if (ch == 1) {
                                        System.out.println(
                                                        "\nEnter 1 to change manufacturar, 2 to change cylinders, 3 to change owner, 4 to change load,"
                                                                        + " 5 to change towing capacity, or any other number to go back to main menu: ");
                                        int editCh = scan.nextInt();

                                        switch (editCh) {

                                        case 1:
                                                System.out.print("Enter manufacturar name: ");
                                                manufactureName = scan.next();
                                                tr.setManufactureName(manufactureName);
                                                break;
                                        case 2:
                                                System.out.print("Enter number of cylinders: ");
                                                numOfCylinder = scan.nextInt();
                                                tr.setNumOfCylinder(numOfCylinder);
                                                break;
                                        case 3:
                                                System.out.print("Enter owner name: ");
                                                owner = scan.next();
                                                tr.setOwner(new Person(owner));
                                                break;
                                        case 4:
                                                System.out.print("Enter load: ");
                                                loadCapacity = scan.nextDouble();
                                                tr.setLoadCapacity(loadCapacity);
                                                break;
                                        case 5:
                                                System.out.print("Enter towing capacity: ");
                                                towingCapacity = scan.nextInt();
                                                tr.setTowingCapacity(towingCapacity);
                                                break;
                                        }

                                } else if (ch == 2)
                                        System.out.println(tr.toString());
                        } while (ch != 10);
                } catch (Exception e) {
                        System.out.println("Exception occures, again run code");
                }
                scan.close();
        }
}

Related Solutions

Please I can get a flowchart and a pseudocode for this java code. Thank you //import...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import the required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BirthdayReminder {       public static void main(String[] args) throws IOException {        // declare the required variables String sName = null; String names[] = new String[10]; String birthDates[] = new String[10]; int count = 0; boolean flag = false; // to read values from the console BufferedReader dataIn = new BufferedReader(new...
Question: Can I get the code in Java for this assignment to compare? Please and thank you....
Question: Can I get the code in Java for this assignment to compare? Please and thank you. Can I get the code in Java for this assignment to compare? Please and thank you. Description Write a Java program to read data from a text file (file name given on command line), process the text file by performing the following: Print the total number of words in the file. Print the total number of unique words (case sensitive) in the file. Print...
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import...
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import java.util.Scanner; ;//import Scanner to take input from user public class TestScore {    @SuppressWarnings("resource")    public static void main(String[] args) throws ScoreException {//main method may throw Score exception        int [] arr = new int [5]; //creating an integer array for student id        arr[0] = 20025; //assigning id for each student        arr[1] = 20026;        arr[2] = 20027;...
Please can I kindly get a flowchart for this java code. Thank you. //import the required...
Please can I kindly get a flowchart for this java code. Thank you. //import the required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BirthdayReminder {       public static void main(String[] args) throws IOException {        // declare the required variables String sName = null; String names[] = new String[10]; String birthDates[] = new String[10]; int count = 0; boolean flag = false; // to read values from the console BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in));...
please right make it so that it can run on jGRASP and java code please thank...
please right make it so that it can run on jGRASP and java code please thank you Debug Problem 1: As an intern for NASA, you have been instructed to debug a java program that calculates the speed that sound travels in water. Details about the formulas and correct results appear in the comments area at the top of the program Here is the code to debug: importjava.util.Scanner; /**    This program demonstrates a solution to the    The Speed of Sound...
----- Please solve the questions with the code below. Thank you. ----- Exercise Overview Refactor your...
----- Please solve the questions with the code below. Thank you. ----- Exercise Overview Refactor your code to enhance the user experience and to use objects and classes. All functional requirements in Project 1 remain, except where enhancing the system replaces specific functions. Functional Requirements The console entry point for the user inputs is on the same line as the prompt. (new) User enters name at the beginning of a session. System covers four math operations – addition, subtraction, multiplication,...
This question is about java program. Please show the output and the detail code and comment...
This question is about java program. Please show the output and the detail code and comment of the each question and each Class and interface. And the output (the test mthod )must be all the true! Thank you! Question1 Create a class Animal with the following UML specification: +-----------------------+ | Animal | +-----------------------+ | - name: String | +-----------------------+ | + Animal(String name) | | + getName(): String | | + getLegs(): int | | + canFly(): boolean | |...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove,...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (After deleting an element, the number of elements in the array is reduced by 1.) Assume that...
can someone please check the code below? Thank you Hunterville College Tuition Design a program for...
can someone please check the code below? Thank you Hunterville College Tuition Design a program for Hunterville College. The current tuition is $20,000 per year. Allow the user to enter the rate the tuition increases each year. Display the tuition each year for the next 10 years. For the programming problem, create the pseudocode and enter it below. Enter pseudocode here start     Declarations             num tuition             num year                         num TOTAL_YEARS = 10             num INCREASE_RATE...
C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT