Question

1. What is the role of constructors in the context of Java classes?

2. Define a meaningful Java class, with at least two instance variables and a constructor with parameters. Using the constructor, write one line of code to instantiate an object of the defined class (use your own data values for the instance variables).

3. Design and implement in Java a class Car and a class CarTest.

For the class Car consider the following elemensts: (i) the instance variables make (of type String), model (of type String), price (of type double) and year (of type int and greater than 1900), (ii) a constructor with arguments, (iii) the method toString (returns the string representation of a Car object) and the setters and getters methods.

The class CarTest contains only the main method. In the main method you should: (1) Instantiate a Car object car1 by invoking the argument constructor (use argument values of your own) and display to the console the string representation of the object car1 using the method toString; (2) Ask the user to enter values for each instance variable and use the setter type methods to assign the entered values to the corresponding instance variables of the object car1. Display the string representation of the object car1 by invoking the toString method.
Be sure your code compiles and runs as expected.

- Java source files Car.java and CarTest.java .
- A document file (.pdf or .doc/.docx) containing the following elements: (1) A short question discussion / analysis, (2) Design decisions and assumptions, (3) UML class diagram for the class Car (4) Relevant test cases and two relevant screenshots, (5) Error handling, (6) Lessons learned and (7) Possible improvements. The size of the document file should be of 2 pages, single spaced, font size 12.

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

public class Car {

private String make, model;
private double price;
private int year;

public Car(String make, String model, double price, int year)
{
this.make = make;
this.model = model;
this.price = price;
if(year > 1900)
this.year = year;
else
this.year = 1901;
}

public String getMake() {
return make;
}

public void setMake(String make) {
this.make = make;
}

public String getModel() {
return model;
}

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

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public int getYear() {
return year;
}

public void setYear(int year) {
if(year > 1900)
this.year = year;
else
this.year = 1901;
}

public String toString() {
return "The car is a " + make + " " + model + ", that costs: $" + price + ", from the year: " + year;
}

}...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Related Homework Solutions

Parking Problem in Java
Homework Solution
$30.00
Java
Programming
Codes
Classes
Computer Science
Parking
OOP
Car Model
License Number
Owner Name
Strings
Integers
Statements
Input
Output
Constructor
Public Methods
Functions
Java Programming: Stack & Queue
Homework Solution
$40.00
Programming
Java
Coding
Computer Science
Data Sets
Stack
Queue
Matrix
Circular Array
Algorithm
Duplicates
Sequences
Test Files
Random Functions
Mathematicians
Josephus Problem
Statements
Variables
Java Programming: Printing The Pattern
Homework Solution
$10.00
Java
Programming
Computer Science
Loops
Printing
Patterns
Rows
Loops
Statements
Variables
Integers
Strings
Input
Output
Accounting Problems Using Java
Homework Solution
$45.00
Java
Programming
Codes
Algorithms
Computer Science
Statements
Variables
Loops
Input
Output
Integers
Interest Rates
Account Balance
Display Messages
Compounding Interest
Strings
Dates
Analysis
Get help from a qualified tutor
Live Chats