how to make a scanner to get speed from the user and add the speed with the last value of the object for example Honda (speed+0.2)

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter1: Creating Java Programs
Section: Chapter Questions
Problem 14RQ
icon
Related questions
Question

question for  Java  a class demo has three objects 

Car car = new Car("make:Honda","color:Black",0.2);
Car car2 = new Car("make:Ford","color:blue", 0.5);
Car car3 = new Car("make:Tesla","color:silver",1.2);

how to make a scanner to get speed from the user and add the speed with the last value of the object for example Honda (speed+0.2)

 

Expert Solution
Step 1

To add the speed with last value of the object, we need to use the statement this.Speed += maxSpeed; in the setter method of speed and to check the updated value, we need to use the getter method for speed.

import java.util.Scanner;
public class Car
{
   private String model;
   private String color;
   private double Speed;
   public Car(String model, String color, double Speed)
   {
       this.model = model;
       this.color = color;
       this.Speed = Speed;
   }
   public String getModel() {
       return model;
   }

   public void setModel(String model) {
       this.model = model;
   }

   public String getColor() {
       return color;
   }

   public void setColor(String color) {
       this.color = color;
   }

   public double getSpeed() {
       return Speed;
   }

   public void setSpeed(double maxSpeed) {
       this.Speed += maxSpeed;
   }

 public static void main(String[] args) {
  Scanner Keyboard=new Scanner(System.in);
  Car car1 = new Car("make:Honda","color:Black",0.2);
  System.out.print("Enter the speed to upgrade:");
  double newspeed=Keyboard.nextDouble();
  car1.setSpeed(newspeed);
  System.out.println(" New speed "+car1.getSpeed());                                  
 } 
}

  

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Files and Directory
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
CMPTR
CMPTR
Computer Science
ISBN:
9781337681872
Author:
PINARD
Publisher:
Cengage