Due Date: Nov 10, 2014 Purpose: To exercise on classes and objects, use construc
ID: 3567111 • Letter: D
Question
Due Date: Nov 10, 2014 Purpose: To exercise on classes and objects, use constructors, accessor and mutator methods. Note that there are two problems in this assignment to give you more practice. Problem 1: In this program, you will write a Java class to represent a Car type and test the class using the main method in another class Race Track java 1. Create a new Java class named Car that has the following fields year The year field is an int that holds a car's yearmodel (e.g. 2010 make The make field is a String object that holds the make of the car (e.g."Porsche") speed -The speed field is an double that holds a car's current speed (e.g.25.0) 2. In addition, the Car class should have the following methods. Constructor The constructor should accept the cars year, make, and beginning speed as arguments o These values should be used to initialize the Cars year, make, and speed fields Getter Methods Write three accessor (getter methods to get the values stored in an object's fields o getYearo, getMakc0, getSpccd0 accelerate Write an accelerate method that has no arguments parameters) passed to it and adds l to the spccd ficld cach time it is called o For example: if the car was going 3 mph, accclcrate would set the speed to 4 mph 3. Write a separate Java class Race Track in file RaceTrackjava with a main method that uses your Car class Create a new Car object (using the Car constructor method), passing in the year, make, and speed Display the current status of the car object using the getter methods getYear0. getMake0. and getSpeed() Call the cars accelerate method and then re-display the car's speed using getSpeed0 Sample output: Year: 2010 Make: Porsche Speed: 5.0 Accelerate, my speed is now 6.0 Submit both Car java and RaccTrack.java filesExplanation / Answer
a.
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
class Car
{
public int year;
public String make;
public double speed;
public Car(int y, String m, double s)
{
year = y;
make = m;
speed = s;
}
public int getYear()
{
return year;
}
public String getMake()
{
return make;
}
public double getSpeed()
{
return speed;
}
public void accelerate()
{
speed += 1;
}
}
/* Name of the class has to be "Main" only if the class is public. */
class RaceTrack
{
public static void main (String[] args) throws java.lang.Exception
{
Car c = new Car(2010, "Porsche", 5.0);
System.out.println(" Year: "+ c.year);
System.out.println(" Make: "+ c.make);
System.out.println(" Speed: "+ c.speed);
c.accelerate();
System.out.println(" Accelerate, my speed is now "+ c.speed);
}
}
----------------------------
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Book
{
String title;
boolean borrowed;
public Book(String booktitle)
{
title = booktitle;
borrowed = false;
}
public void borrowed()
{
borrowed = true;
}
public void returned()
{
borrowed = false;
}
public boolean isBorrowed()
{
return borrowed;
}
public String getTitle()
{
return title;
}
public static void main (String[] args) throws java.lang.Exception
{
Book example = new Book("The Da Vinci Code");
System.out.println("Title: "+example.title);
System.out.println("Borrowed?: "+example.isBorrowed());
example.borrowed();
System.out.println("Borrowed?: "+example.isBorrowed());
example.returned();
System.out.println("Borrowed?: "+example.isBorrowed());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.