JAVA - first semester intro to java To help you get started I included a zip fil
ID: 669745 • Letter: J
Question
JAVA - first semester intro to java
To help you get started I included a zip file with a starter project. It is called A03.zip. Make sure to un-zip it after downloading. SEE BOTTOM for zip files.
It includes two classes: Car and Country. Each defines a new type (blueprint). These two classes are already completed. Do not modify them. It also includes a project file: GetterSetter.gpj
What you need to do is to write a program that tests the classes Car and Country.
Start by adding a new file A03.java
To do that right-click the project GetterSetter . (look for the purple icon in the left column) A context menu opens.
Choose Add File. Navigate to the folder that includes the un-zipped starter project. For the file name choose A03.java Press Add and then Done.
At this point A03.java has been added to the project. You can see it in the left column. However, the file has not been created yet. Double click A03.java in the left column and select Create. Now you are ready to add some code to the new file.
In file A03.java create a public class called A03 that includes the main method.
In the main method test first class Car and then class Country.
Make all your output look like the output provided. Use empty lines to structure the output as shown above.
To test class Car do the following:
Print a label that tells the user that you are testing class car (see output)
Create a new car called myPrius
Set make to Toyota, model to Prius, and owner to Bob Smith
Print the labeled field values line by line (as shown in the output )
Create a second car called myRoadster.
Make should be Tesla, model Roadster, and the owner Lisa Ray
Print the labeled field values
The owner of the Prius buys the Roadster. Update the field owner of the Roadster to reflect that change. Print the new field value (see output)
Hint: Make sure to access the field of myPrius when changing the
owner of myRoadster. (do NOT user a hard-coded String)
To test class Country do the following:
Print a label that tells the user that you are testing class country
Create a new country and call it country1 The name is Macau, the population 453,000, and the area 6 square miles. Print the labeled field values line by line Calculate the population density using the method provided in class Country. Print the result (see output) Hint: The output includes commas to group large numbers by thousands. Adding a comma in the format specifier for integers like this: %,d will include the locale-specific grouping characters
Create a second country and call it country2
The name is Libya, the population 5,900,000, and the area 679,358 square miles. Print the labeled field values line by line, as well as the calculated population density
Create a third country and call it country3
The name is USA, the population 298,500,000, and the area 3,539,225 square miles. Print the labeled field values line by line, as well as the calculated population density
In 1960 the US population was 179,323,175. Update the population of the US to its 1960 value. Display the updated the feild as well as the 1960 population density.
It includes two classes: Car and Country. Each defines a new type (blueprint). These two classes are already completed. Do not modify them. It also includes a project file: GetterSetter.gpj
What you need to do is to write a program that tests the classes Car and Country.
Start by adding a new file A03.java
To do that right-click the project GetterSetter . (look for the purple icon in the left column) A context menu opens.
Choose Add File. Navigate to the folder that includes the un-zipped starter project. For the file name choose A03.java Press Add and then Done.
At this point A03.java has been added to the project. You can see it in the left column. However, the file has not been created yet. Double click A03.java in the left column and select Create. Now you are ready to add some code to the new file.
In file A03.java create a public class called A03 that includes the main method.
In the main method test first class Car and then class Country.
Make all your output look like the output provided. Use empty lines to structure the output as shown above.
To test class Car do the following:
Print a label that tells the user that you are testing class car (see output)
Create a new car called myPrius
Set make to Toyota, model to Prius, and owner to Bob Smith
Print the labeled field values line by line (as shown in the output )
Create a second car called myRoadster.
Make should be Tesla, model Roadster, and the owner Lisa Ray
Print the labeled field values
The owner of the Prius buys the Roadster. Update the field owner of the Roadster to reflect that change. Print the new field value (see output)
Hint: Make sure to access the field of myPrius when changing the
owner of myRoadster. (do NOT user a hard-coded String)
To test class Country do the following:
Print a label that tells the user that you are testing class country
Create a new country and call it country1 The name is Macau, the population 453,000, and the area 6 square miles. Print the labeled field values line by line Calculate the population density using the method provided in class Country. Print the result (see output) Hint: The output includes commas to group large numbers by thousands. Adding a comma in the format specifier for integers like this: %,d will include the locale-specific grouping characters
Create a second country and call it country2
The name is Libya, the population 5,900,000, and the area 679,358 square miles. Print the labeled field values line by line, as well as the calculated population density
Create a third country and call it country3
The name is USA, the population 298,500,000, and the area 3,539,225 square miles. Print the labeled field values line by line, as well as the calculated population density
In 1960 the US population was 179,323,175. Update the population of the US to its 1960 value. Display the updated the feild as well as the 1960 population density.
/*******************************************************
* Sample Code - do not modify
*A03
*******************************************************/
public class Car
{
// fields
private String make;
private String model;
private String owner;
// contructors
// methods
public String getMake()
{
return make;
}
public String getModel()
{
return model;
}
public String getOwner()
{
return owner;
}
public void setMake(String newMake)
{
make = newMake;
}
public void setModel(String newModel)
{
model = newModel;
}
public void setOwner(String newOwner)
{
owner = newOwner;
}
}
/*******************************************************
* Sample Code - do not modify
*A03
*******************************************************/
public class Country
{
// fields
private String name;
private int population;
private int area; // in square miles
// constructors
// methods
public String getName()
{
return name;
}
public int getPopulation()
{
return population;
}
public int getArea()
{
return area;
}
public void setName(String newName)
{
name = newName;
}
public void setPopulation(int newPopulation)
{
population = newPopulation;
}
public void setArea(int newArea)
{
area = newArea;
}
// population per square mile
public int populationDensity()
{
int density;
density = population / area; // integer division truncates
return density;
}
}
/*******************************************************
* Sample Code - do not modify
*A03
*******************************************************/
public class Car
{
// fields
private String make;
private String model;
private String owner;
// contructors
// methods
public String getMake()
{
return make;
}
public String getModel()
{
return model;
}
public String getOwner()
{
return owner;
}
public void setMake(String newMake)
{
make = newMake;
}
public void setModel(String newModel)
{
model = newModel;
}
public void setOwner(String newOwner)
{
owner = newOwner;
}
}
Explanation / Answer
package mani;
public class A03{
public static void main(String[] args){
System.out.println("*********TESTING CAR CLASS:***********");
Car myPrius=new Car();
myPrius.setMake("Toyota");
myPrius.setModel("Prius");
myPrius.setOwner("Bob Smith");
System.out.println("Make of the car: "+myPrius.getMake());
System.out.println("Model of the car: "+myPrius.getModel());
System.out.println("Owner of the car: "+myPrius.getOwner());
Car myRoadster=new Car();
myRoadster.setMake("Tesla");
myRoadster.setModel("Roadster");
myRoadster.setOwner("Lisa Ray");
System.out.println("Make of the car: "+myRoadster.getMake());
System.out.println("Model of the car: "+myRoadster.getModel());
System.out.println("Owner of the car: "+myRoadster.getOwner());
myRoadster.setOwner(myPrius.getOwner());
System.out.println("Owner of the car: "+myRoadster.getOwner());
System.out.println(" *********TESTING Country CLASS:***********");
Country country1=new Country();
country1.setName("Macau");
country1.setArea(6);
country1.setPopulation(453000);
String str = String.format("%,d", country1.getPopulation());
System.out.println("Name of the country: "+country1.getName());
System.out.println("Poulation of "+country1.getName()+" is "+str);
System.out.println("Area of the "+country1.getName()+" is "+country1.getArea());
str = String.format("%,d", country1.populationDensity());
System.out.println("Population density of the "+country1.getName()+" is "+str);
Country country2=new Country();
country2.setName("Libya");
country2.setArea(679358);
country2.setPopulation(5900000);
str = String.format("%,d", country2.getPopulation());
System.out.println("Name of the country: "+country2.getName());
System.out.println("Poulation of "+country2.getName()+" is "+str);
System.out.println("Area of the "+country2.getName()+" is "+country2.getArea());
str = String.format("%,d", country2.populationDensity());
System.out.println("Population density of the "+country2.getName()+" is "+str);
Country country3=new Country();
country3.setName("USA");
country3.setArea(3539225);
country3.setPopulation(298500000);
str = String.format("%,d", country3.getPopulation());
System.out.println("Name of the country: "+country3.getName());
System.out.println("Poulation of "+country3.getName()+" is "+str);
System.out.println("Area of the "+country3.getName()+" is "+country3.getArea());
str = String.format("%,d", country3.populationDensity());
System.out.println("Population density of the "+country3.getName()+" is "+str);
country3.setPopulation(179323175);
str = String.format("%,d", country3.getPopulation());
System.out.println("Poulation of "+country3.getName()+" is "+str);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.