Question

In: Computer Science

Problem Statement Design a class named Car that has the following fields: year: The year field...

Problem Statement

Design a class named Car that has the following fields:

  • year: The year field is an integer that holds the car’s year.

  • make: the make field is a String that holds the make of the car.

  • speed: the speed field is an integer that holds the car’s current speed.

In addition, the class should have the following constructor and other methods:

  • Constructor: the constructor should accept the car’s year >model and make as parameters. These values should be assigned to the object’s yearand make fields. The constructor should also assign 0 to the speed field.

  • Accessors: design appropriate accessor methods to get the values stored in an object’s year, make, and speed fields.

  • accelerate: the accelerate method should add 5 to the speed field each time it is called.

  • brake: the brake method should subtract 5 from the speed field each time it is called.

Your solution must include these components:

  1. UML Class diagram

  2. Flowchart

  3. Pseudo-code (Gaddis Pseudo Code)

  4. Program Coded (Java Code)

Solutions

Expert Solution

// Screenshot of the code & output

// Code to copy

import java.util.Scanner;

public class Car {
   // instance variables declaration
   private int year;
private String make;
private int speed;
// constructor
public Car(int year, String make) {
this.year= year;
this.make = make;
this.speed=0;
}
   public int getYear() {
       return year;
   }
   public String getMake() {
       return make;
   }
public void setSpeed(int speed) {
   this.speed = speed;
}

   public int getSpeed() {
       return speed;
   }
   // accelerate method
   public void accelerate() {
   speed += 5;
   }
   // brake method
   public void brake () {
   speed-=5;
   }

   public static void main(String[] args) {

   int year=0;
   int speed=0;
   String make=null;   
       Scanner keyboard = new Scanner (System.in);       

System.out.print("What is the year of your car? ");
       year = keyboard.nextInt();
       System.out.print("What is the make of your car? ");
       make = keyboard.next();
       System.out.print("How fast is your car going? ");
       speed = keyboard.nextInt();
       Car car = new Car(year, make);
       car.setSpeed(speed);
       car.accelerate();
       System.out.println("\nYearModel:" + car.getYear() + " Make:" + car.getMake() +
       " Speed:" + car.getSpeed() + " MPH.");
       keyboard.close();
   }

}


Related Solutions

Create a class named “Car” which has the following fields. The fields correspond to the columns...
Create a class named “Car” which has the following fields. The fields correspond to the columns in the text file except the last one. i. Vehicle_Name : String ii. Engine_Number : String iii. Vehicle_Price : double iv. Profit : double v. Total_Price : double (Total_Price = Vehicle_Price + Vehicle_Price* Profit/100) 2. Write a Java program to read the content of the text file. Each row has the attributes of one Car Object (except Total_Price). 3. After reading the instances of...
Design a class named Pet, which should have the following fields: Name – The name field...
Design a class named Pet, which should have the following fields: Name – The name field holds the name of a pet. Type – The type field holds the type of animal that is the pet. Example values are “Dog”, “Cat”, and “Bird”. Age – The age field holds the pet’s age. The Pet class should also have the following methods: setName – The setName method stores a value in the name field. setType – The setType method stores a...
Complete the following C++ tasks: a. Design a class named BaseballGame that has fields for two...
Complete the following C++ tasks: a. Design a class named BaseballGame that has fields for two team names and a final score for each team. Include methods to set and get the values for each data field. Create the class diagram and write the pseudocode that defines the class. b. Design an application that declares three BaseballGame objects and sets and displays their values. c. Design an application that declares an array of 12 BaseballGame objects. Prompt the user for...
Design a class named Employee. The class should keep the following information in fields: ·         Employee...
Design a class named Employee. The class should keep the following information in fields: ·         Employee name ·         Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. ·         Hire date Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that inherits from the Employee class. The ProductionWorker class should have fields...
In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
[20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields:...
[20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields: position x, position y, and the direction (east, south, west, and north). Assume that positive x points to the east and positive y points to the south, and initially x = 0, y = 0, direction = "east". Create a no-arg constructor and another constructor which sets the three data fields. Create the accessors and mutators for the three data fields. Create a method...
[20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields:...
[20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields: position x, position y, and the direction (east, south, west, and north). Assume that positive x points to the east and positive y points to the south, and initially x = 0, y = 0, direction = "east". Create a no-arg constructor and another constructor which sets the three data fields. Create the accessors and mutators for the three data fields. Create a method...
Design a class named Robot. The Robot class has three private data fields: position x, position...
Design a class named Robot. The Robot class has three private data fields: position x, position y, and the direction (east, south, west, and north). Assume that positive x points to the east and positive y points to the south, and initially x = 0, y = 0, direction = "east". Create a no-arg constructor and another constructor which sets the three data fields. Create the accessors and mutators for the three data fields. Create a method named forward that...
[20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields:...
[20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields: position x, position y, and the direction (east, south, west, and north). Assume that positive x points to the east and positive y points to the south, and initially x = 0, y = 0, direction = "east". Create a no-arg constructor and another constructor which sets the three data fields. Create the accessors and mutators for the three data fields. Create a method...
In java, (include javadoc comments for each method) design a class named Contacts that has fields...
In java, (include javadoc comments for each method) design a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contacts objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT