1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Nuetrik [128]
4 years ago
6

Write a class named Car that has the following fields: • yearModel: The yearModel field is an int that holds the car's year mode

l. • make: The make field is a String object that holds the make of the car. • speed: The speed field is an int that holds the car's current speed. In addition, the class should have the following methods: • Constructor: The constructor should accept the car's year model and make as arguments. These values should be assigned to the object's yearModel and make fields. The constructor should also assign 0 to the speed field. • Accessor: The appropriate accessor methods should be implemented to access the values stored in the object's yearModel, make, and speed fields. • accelerate: The accelerate method should add 5 to the speed field when it is called. • brake: The brake method should subtract 5 from the speed field each time it is called. Demonstrate the class in a program that contains a Car object, and then calls the accelerate method five times. After each call to the accelerate method, get the current speed of the car and print it on a separate line. Then, call the brake method five times, each time printing the current speed of the car on a separate line. Sample Run java Car 5↵ 10↵ 15↵ 20↵ 25↵ 20↵ 15↵ 10↵ 5↵ 0↵

Computers and Technology
1 answer:
dolphi86 [110]4 years ago
4 0

Answer:

Here is Car.java

public class Car  { //class definition

  //define private data members of class Car

   private int yearModel;  // this field is an int that holds the car's year model

   private String make;  // this field is a String object that holds the make of the car

private int speed;  //an int field that holds the car's current speed.

 

public Car (int yearModel, String make)  { //constructor that accept the car's year model and make as arguments

 this.make = make;  /*assigns make of Car to make of Constructor argument. Here this is used to refer to Car member variable and to avoid confusion between make variable of class and Constructor argument */

 this.yearModel = yearModel;  //assigns yearModel of Car to yearModel of Constructor argument

 speed = 0;  }  //assign 0 to the speed field

 

public int getyearModel ()  //accessor method to access the value stored in object's yearModel

          {   return yearModel;  }  //return the value of yearModel

 

public String getMake ()  //accessor method to access the value stored in object's make

{     return make;  }  //return the value of make

 

public int getSpeed ()  //accessor method to access the value stored in object's speed

{       return speed;        }  //return the value of speed

 

public void accelerate ()  //method that adds 5 to the speed field

{       speed = speed +5;    } // adds 5 to the value of speed

 

public void brake ()  //subtracts 5 from the speed field

{        speed = speed -5;     }  //subtracts 5 from the value speed

}

Explanation:

 Here is the Main.java

public class Main { //class definition

public static void main(String[] args) { //start of main function

Car car1 = new Car(2000, "Civic"); //creates a Car object and assigns values to make and yearModel fields by calling constructor of Car using car1 object

 for (int i = 1; i <= 5; i++) //iterates the loop 5 times to call accelerate method 5 times to add 5 to current speed

 {   System.out.println("Accelerate!"); //prints Accelerate! on output screen

  car1.accelerate(); //calls accelerate method to add 5 to the speed field

  System.out.println("Current speed: " + car1.getSpeed()); }   //displays the current speed by calling getSpeed() method using object car1

 System.out.println("\n");//prints a new line

 for (int j = 1; j <= 5; j++)  {   // iterates the loop 5 times to call brake method 5 times to subtract 5 from current speed

     System.out.println("Brake!");// //prints Brake! on output screen

  car1.brake(); ////calls brake method to subtract 5 from the speed field

  System.out.println("Current speed: " + car1.getSpeed()); } } } //displays the current speed by calling getSpeed() method using object car1

If you want to take input from user then make the following changes to Constructor of Car:

public Car (int yearModel, String make, int speed)  {  

 this.make = make;

 this.yearModel = yearModel;  

    speed = 0;  }

Now use this Main.java instead to take input values from user:

import java.util.Scanner; //class used to take input from user

public class Main {

public static void main(String[] args) {

 Scanner keyboard = new Scanner(System.in); //creates Scanner object

//below statement are used to take input for yearModel, make and speed from user

       System.out.println("Enter car's year model: ");

       int yearModel = keyboard.nextInt();

       keyboard.nextLine();

       System.out.println("Enter car's make: ");

       String make = keyboard.nextLine();

       System.out.println("Enter the speed: ");

       int speed = keyboard.nextInt();

//creates Car object named car1 and calls constructor of Car to pass values for yearModel, make and speed using car1 object

   Car car1 = new Car(yearModel,make,speed);

//this works the same as above Main.java

    for (int i = 1; i <= 5; i++)

 {   System.out.println("Accelerate!");

  car1.accelerate();

  System.out.println("Current speed: " + car1.getSpeed()); }  

   System.out.println("\n");

 for (int j = 1; j <= 5; j++)  {  

     System.out.println("Brake!");

  car1.brake();

  System.out.println("Current speed: " + car1.getSpeed()); }}}

You might be interested in
Vendors who use open source as part of their product offerings can expect to bring new products to the market faster because: Gr
tamaranim1 [39]

Answer:

A.  they can skip whole segments of the software development process.

Explanation:

A software development process refers to the process of dividing software development work into various phases for product management, project management and to improve the design.

Vendors who use open source as part of their product offerings can expect to bring new products to the market faster because they can skip whole segments of the software development process.

They don't have to go through different phases of software development.

6 0
3 years ago
What kind of software is Microsoft Outlook??
NNADVOKAT [17]

Answer:

Email software

Explanation:

8 0
3 years ago
Read 2 more answers
FTP is commonly used to __________ and __________ files to a server.
Aleks [24]

Answer:

upload; download

Explanation:

FTP (File Transfer Protocol) is an internet protocol used to upload and download a file to a server, there are some programs help us to transfer this data to a server, in some cases, we're going to need these programs to upload website files to the server like images or videos, or some websites where do you need a user and passwords to upload file by FTP

8 0
4 years ago
What does Technology mean to you?​
brilliants [131]
Technology is the collection of techniques, skills, methods and processes used in the production of goods or services or in the accomplishment of objectives, such as scientific investigation. Technology can be the knowledge of techniques, processes, etc. or it can be embedded in machines, computers, devices and factories, which can be operated by individuals without detailed knowledge of the workings of such things.
8 0
3 years ago
Read 2 more answers
for java ?(Business: check ISBN-13)ISBN-13 is a new standard for identifying books. It uses 13 digits d1d2d3d4d5d6d7d8d9d10d11d1
ludmilkaskok [199]

Answer:

In Java:

import java.util.*;

public class Main{

public static void main(String[] args) {

 Scanner input = new Scanner(System.in);

 String isbn;

 System.out.print("First 1:2 digits: ");

 isbn = input.nextLine();

 if(isbn.length()==12){

 int chksum = 0;

 for(int i = 0; i<12;i++){

     if((i+1)%2==0){      chksum+= 3 * Character.getNumericValue(isbn.charAt(i));          }

     else{          chksum+=Character.getNumericValue(isbn.charAt(i));          }  }

 chksum%=10;

 chksum=10-chksum;

 if(chksum==10){

 System.out.print("The ISBN-13 number is "+isbn+"0");}

 else{

 System.out.print("The ISBN-13 number is "+isbn+""+chksum);          }   }

 else{

     System.out.print("Invalid Input");

 }     }}

Explanation:

See attachment for explanation where comments are used to explain each line

Download txt
6 0
3 years ago
Other questions:
  • Race conditions are possible in many computer systems. Consider a banking system with two methods: deposit(amount) and withdraw(
    14·1 answer
  • The _____ command icon looks like a small clipboard with a page attached.
    6·1 answer
  • . _______ are the components that allow a posi-traction differential to transfer power from one drive wheel to another.
    10·1 answer
  • Can someone help me with...A table can help? PLEASE
    8·1 answer
  • Choose a developing country and give 3 reasons why it is a developing country ​
    15·1 answer
  • Which of these are forms of data? Check all that apply.
    8·2 answers
  • ListenListen with ReadSpeakerAn administrator working on a Windows Server 2016 Server Core installation needs to disable DHCP on
    9·1 answer
  • Hyperlink is a term used to refer to the specific manner of specifying the address of a website. Write reason for true or false.
    10·1 answer
  • Use a while loop to output the even number from 100 to 147? This is python
    10·1 answer
  • Guidewords for the word “serpent” may be
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!