Sustainability: Sustainability is an important consideration for any nation. Discuss what countries
do to sustain their economy and help its people.
Solution
Sustainable development is a crucial subject to affix downward since it beset so many
contrasting stuff.Due to the complication of this accountable it is big to attention at the big in a
holistic action that approaches affair intelligently.The authority for sustainab;e development at
the civil level that is for the difficult mosaic is a mixture of economic,political that precondition
be bring combine under a particular set of circumstances.Any changes in the people life style
makes a economic atmosphere and candid of championship and business will make a country
effort easy..
This is the assignmentOBJECTIVESAfter finishing this lab, stude.pdf
1. This is the assignment:
OBJECTIVES
After finishing this lab, students can have the following:
Understand how to declare an unsorted-optimized array data structure
Know how to insert nodes to the structure, fetch a node with a key field of node, update the
information of a node and delete a node from the structure
Verify that the nodes in the unsorted-optimized array data structure are fully encapsulated.
Students will be familiar with the ArrayList and apply all the basic operation to access nodes
Also, students recognize that ArrayList is a heterogeneous data structure that can store nodes of
any type of objects.
REQUIREMENT STATEMENT
A car dealer needs an application that can help the shop to do the following tasks:
1.Insert a car or a motocycle
2.Test for the encapsulation of the data structure
3.Remove a car or a motocycle
4.Update information of a car or a motocycle
5.Show All vehicles
6.Exit
Also, the owner of the shop asks for:
-Using the Unsorted Optimized Array data structure for the application and apply Inheritance
and polymorphism of Object Oriented Programming
-Requiement for output:
The car’s information is displayed as below (for example)
VIN: 123A456B Color: White Year: 2014
Car make: Toyota Model: Sienna Number of passengers: 8 Number of doors: 4
The motocycle’s information is displayed as below (for example
VIN: 321BC654
Color: Grey
Year: 2015
Motocycle make: Honda
Model: PCX150
Has side car: no
-After a task from the menu, the application should allow users to continue using the application
and only terminate when users select Exit
COSC2436 – Lab3
2. 1
TASK INSERT
-Display the message: “*** TASK: INSERT and FETCH A CAR OR A MOTOCYCLE ***”
-Allow users to enter the information of a car or a motocyle from the keyboard -Insert the
car/motocycle to the data structure
-Fetch the information of car/motocycle by its VIN from the data structure that have entered to
the structure to ensure that the car/motocycle is in the structure
TEST FOR ENCAPSULATION
-Display the message: “*** TASK: TEST FOR DATA ENCAPSULATION ***”
-Create a test car with the following information typed from the keyboard: -Insert this test car to
the data structure
-Modify the the color of the test car with different color
-Declare an object of car named “copy” and assign the node that is fetched from the data
structure with the target key as VIN of test car
-Display information of the object named copy
-Compare the color of test car with the color of object copy. If they are different then display the
message: “The Unsorted Optimized Array structure has the encapsulation”; otherwise display
“The Unsorted Optimized Array structure does not have the encapsulation”
TASK UPDATE
-Display: “***TASK: UPDATE INFORMATION OF A CAR OR A MOTOCYCLE *** “ -
Create an object of class Car named newNode
-Fetch a node with the VIN is the same VIN of test car then assign to newNode, therefore, the
newNode has the same VIN with the test car
-Change the color of the newNode to different color -Update the data structure with with the
newNode
-Fetch with the key as the key of test car. If the node resulted from fetch has different color from
the test car the display the message “Update test car successfully”; otherwise display “Update
test car failed”
TASK DELETE
-Display: “ *** TASK: DELETE A CAR OR A MOTOCYCLE ***”
-Delete the node with the VIN as SP2017 that was not inserted to the data structure, print out the
result of task delete (output: false)
-Delete the node with the VIN of the above test car, print the result (output: true) -Fetch with the
key as the VIN of the test car, print out the result (output: null)
TASK SHOW ALL
-Call showAll to display all nodes on the data structure
3. WHAT WE NEED TO KNOW TO DO THE PROGRAM
-Review how to declare the Inheritance relationship between 2 classes
-How to write the constructor, toString or other methods of child classes to inherit from the super
class -How to apply polymorphism to the main
-Understand what is Unsorted Optimized Array structure, how initialize the Unsorted Optimized
Array data structure
-How 4 operations of Unsorted Optimized Array structure work
UML and PSUEDO-CODE
You should provide the UML of super class, and 2 child classes
The pseudo-code is provided in the requirement, you do not need to rewrite; just follow step by
step to write the code
COSC2436 – Lab3
2
HOW TO DO THE LAB part1
-After provide UML and read the pseudo-code, start the Java editor -Create the project name
SP2017LAB3_PART1_yourLastname
-Add super class named SP2017LAB3_Vehicle_yourLastname that maintain Vin number, color,
make, model, year. It also has some methods, such as, mutator, accessor, and toString, method
forward (accept the amount of distance in miles, for example, 25, then display “move forward 25
miles”), method backward (accept the amount of distance in miles, for example 3 then display
the message “move backward 3 miles”), turn left (display word “Turn left”) and turn right
(display word “Turn Rright”)
-Add child class named SP2017LAB3_Car_yourLastName inherits from above class Vehicle. It
has more data members, such as, number of passengers, number of doors
-Add child class named SP2017LAB3_Motocycle_yourLastName inherits from above class
Vehicle. It has more data members, such as, has side car (boolean)
-Add a data structure class named
SP2017LAB3_VehicleUnsortedOptimizedArray_yourLastName that maintain size(int), a vehicle
array and next (int) to hold the location where the new node is inserted to the structure. Also, the
class has the methods for 4 operations: insert, fetch, delete, update and showAll that display all
the nodes in the structure. ( see the code on page 103 for your reference)
-Add a driver class name SP2017LAB3_VehicleDealerShop_yourLastname that do the
requirement asking for
PART2:
OBJECTIVES: Practice using Java ArrayList class.
REQUIREMENT and HOW TO DO THE LAB PART2
4. Add the following part to the previous project with the following step by strep (pseudo-code)
-Declare a data structure of ArrayList with the size 25
-Insert 3 cars/motocycles with information that are entered from the keyboard -Show all 3 nodes
that have inserted to the ArrayList data structure
-Use a for loop to insert number 1 to 100 to the above data structure of ArrayList
-Display the size of the above ArrayList.
-Remove the one above car/motocycle from the ArrayList data structure with one of the VIN
displyed at above showAll(). Then display the size of the ArrayList structure
-Get and display the information of nodes at the location 45. Change the value at location 45 to
2016. Display the information at location 45 again
-Use JOptionPane to display the message: “Congratulation! You are successful on ArrayList
with four operations: add, get, remove and set”
This is the code I have so far:
***Vehicle Class***
import java.util.Scanner;
public class SP2017LAB3_Vehicle{
//
private static Scanner keyboard = new Scanner(System.in);
protected String vin, model, sideCar;
protected String color;
protected String make;
protected int year, miles, passengers, doors;
// default constructor
public SP2017LAB3_Vehicle() {
vin = null;
model = null;
color = null;
make = null;
year = 0;
miles = 0;
}
//parameter constructor
public SP2017LAB3_Vehicle(String vin, String model, String color, String make,
5. int year, int miles){
this.vin = vin;
this.model = model;
this.color = color;
this.make = make;
this.year = year;
this.miles = miles;
}
// mutator method
public void setVin(String vin){
this.vin = vin;
}
// accessor method
public String getVin(){
return vin;
}
public void setColor(String color){
this.color = color;
}
public String getColor(){
return color;
}
public void setMake(String make){
this.make = make;
}
public String getMake(){
return make;
6. }
public void setYear(int year){
this.year = year;
}
public int getYear(){
return year;
}
public void setMiles(int miles){
this.miles = miles;
}
public int getMiles(){
return miles;
}
public void forward(){
System.out.println("How far forward would you like to go?");
miles = keyboard.nextInt();
System.out.println("Move forward " + miles + " mile(s)");
}
public void backward(){
System.out.println("How far backward would you like to go?");
miles = keyboard.nextInt();
System.out.println("Move backward " + miles + " mile(s)");
}
public void left(){
System.out.println("Turn left");
}
public void right(){
7. System.out.println("Turn right");
}
// Standard output list
public String toString() {
String infoOutput = "VIN:t" + vin + " Color:t" + color + " Year:t";
return infoOutput;
}
public SP2017LAB3_Vehicle deepCopy(){
SP2017LAB3_Vehicle clone = new SP2017LAB3_Vehicle(vin, model, color, make, year,
miles);
return clone;
}
public int compareTo(String targetKey){
return(vin.compareTo(targetKey));
}
}
***Car***
import java.util.Scanner;
public class SP2017LAB3_Car extends SP2017LAB3_Vehicle{
private static Scanner keyboard = new Scanner(System.in);
int doors;
int passengers;
public SP2017LAB3_Car() {
super();
doors = 0;
passengers = 0;
}
//
public SP2017LAB3_Car(String vin, String model, String color, String make,
8. int year, int miles, int door, int passenger){
super(vin, model, color, make, year, miles);
doors = door;
passengers = passenger;
}
public int getDoors(){
return doors;
}
public void setDoors(int door){
doors = door;
}
public int getPassengers(){
return passengers;
}
public void setPassengers(int passenger){
passengers = passenger;
}
public void printCar(){
super.toString();
System.out.println("Car make: t" + make + " Model: t" + model + " Number of
passengers: " + passengers +
" Number of doors: " + doors);
}
public void carInfo(){
System.out.println("How many passengers can legally ride in the car?");
passengers = keyboard.nextInt();
System.out.println("How many doors does the car have?");
doors = keyboard.nextInt();
}
}
10. }
System.out.println("Motorcycle make: t" + make + " Model: t" + model + " Has
side car: " + hasSideCar);
}
}
***VehicleDealerShop***
import java.util.Scanner;
//
public class SP2017LAB3_VehicleDealerShop {
static SP2017LAB3_Vehicle newNode = null;
static String option = null, choice = null;
//
private static Scanner keyboard = new Scanner(System.in);
//
public static void main(String[]args){
//
SP2017LAB3_Vehicle inform = new SP2017LAB3_Vehicle();
//
SP2017LAB3_VehicleUnsortedOptimizedArray array = new
SP2017LAB3_VehicleUnsortedOptimizedArray();
while(true){
//
System.out.println("1: Insert a car or motorcycle");
System.out.println("2. Test for the encapsulation of the data structure");
System.out.println("3. Remove a car or motorcycle");
System.out.println("4. Update information of a car or motorcycle");
System.out.println("5. Show all vehicles");
System.out.println("6. Exit");
11. //
int selection = Integer.parseInt(keyboard.nextLine());
//
switch(selection){
case 1:
//
System.out.println("*** TASK: INSERT and FETCH A CAR OR A MOTORCYCLE
***");
System.out.println("Do you wish to insert a car or motorcycle? Y/N");
option = keyboard.nextVehicleDealerShopLine();
if (option == "Y" || option == "y"){
System.out.println("What is the vin number for the car or motorcycle?");
System.out.println("What is the vin number for the car or motorcycle?");
inform.vin = keyboard.nextLine();
System.out.println("What is the color of the vehicle?");
inform.color = keyboard.nextLine();
System.out.println("What is the year of the vehicle?");
inform.year = keyboard.nextInt();
System.out.println("What is the make of the vehicle?");
inform.make = keyboard.nextLine();
System.out.println("What is the model of the vehicle?");
inform.model = keyboard.nextLine();
System.out.println("Is the vehicle a car? Y/N");
choice = keyboard.nextLine();
if (choice == "Y" || choice == "y"){
System.out.println("How many passengers can legally ride in the car?");
inform.passengers = keyboard.nextInt();
System.out.println("How many doors does the car have?");
inform.doors = keyboard.nextInt();
}
else{
12. System.out.println("Does the motorcycle have a sidecar? Y/N");
inform.sideCar = keyboard.nextLine();
}
array.insert(newNode);
}
else{
System.out.println("What is the vin number for the car or motorcycle?");
array.fetch(inform.vin);
}
//
case 2:
//
System.out.println("*** TASK: TEST FOR DATA ENCAPSULATION ***");
case 3:
//
case 4:
//
case 5:
//
case 6:
//
}
}
}
}
***VehicleUnsortedOptimizedArray***
public class SP2017LAB3_VehicleUnsortedOptimizedArray {
private int next;
private int size;
13. private SP2017LAB3_Vehicle[] data;
public SP2017LAB3_VehicleUnsortedOptimizedArray(){
next = 0;
size = 25;
data = new SP2017LAB3_Vehicle[size];
}
public boolean insert(SP2017LAB3_Vehicle newNode){
if (next >= size)
return false;
data[next] = newNode.deepCopy();
if(data[next] == null)
return false;
next = next +1;
return true;
}
public SP2017LAB3_Vehicle fetch(String targetKey){
SP2017LAB3_Vehicle node;
SP2017LAB3_Vehicle temp;
int i = 0;
while(i< next && !(data[i].compareTo(targetKey) == 0)){
i++;
}
if(i== next)
return null;
node = data[i].deepCopy();
if(i != 0){
temp = data[i - 1];
data[i - 1] = data[i];
data[i] = temp;
14. }
return node;
}
public boolean delete(String targetKey){
int i = 0;
while(i < next && !(data[i].compareTo(targetKey) == 0)){
i++;
}
if(i == next)
return false;
data[i] = data[next - 1];
data[next - 1] = null;
next = next - 1;
return true;
}
public boolean update(String targetKey, SP2017LAB3_Vehicle newNode){
if(delete(targetKey) == false)
return false;
else if(insert(newNode) == false)
return false;
else
return true;
}
}
COSC2436 – Lab3
1
Solution
A)
import java.util.Scanner;
public class SP2017LAB3_Vehicle{
private static Scanner keyboard = new Scanner(System.in);
15. protected String vin, model, sideCar;
protected String color;
protected String make;
protected int year, miles, passengers, doors;
public SP2017LAB3_Vehicle() {
vin = null;
model = null;
color = null;
make = null;
year = 0;
miles = 0;
}
//parameter constructor
public SP2017LAB3_Vehicle(String vin, String model, String color, String make,
int year, int miles){
this.vin = vin;
this.model = model;
this.color = color;
this.make = make;
this.year = year;
this.miles = miles;
}
public void setVin(String vin){
this.vin = vin;
}
public String getVin(){
return vin;
}
public void setColor(String color){
this.color = color;
16. }
public String getColor(){
return color;
}
public void setMake(String make){
this.make = make;
}
public String getMake(){
return make;
}
public void setYear(int year){
this.year = year;
}
public int getYear(){
return year;
}
public void setMiles(int miles){
this.miles = miles;
}
public int getMiles(){
return miles;
}
public void forward(){
System.out.println("How far forward would you like to go?");
miles = keyboard.nextInt();
System.out.println("Move forward " + miles + " mile(s)");
17. }
public void backward(){
System.out.println("How far backward would you like to go?");
miles = keyboard.nextInt();
System.out.println("Move backward " + miles + " mile(s)");
}
public void left(){
System.out.println("Turn left");
}
public void right(){
System.out.println("Turn right");
}
public String toString() {
String infoOutput = "VIN:t" + vin + " Color:t" + color + " Year:t";
return infoOutput;
}
public SP2017LAB3_Vehicle deepCopy(){
SP2017LAB3_Vehicle clone = new SP2017LAB3_Vehicle(vin, model, color, make, year,
miles);
return clone;
}
public int compareTo(String targetKey){
return(vin.compareTo(targetKey));
}
}
import java.util.Scanner;
public class SP2017LAB3_Car extends SP2017LAB3_Vehicle{
private static Scanner keyboard = new Scanner(System.in);
int doors;
int passengers;
18. public SP2017LAB3_Car() {
super();
doors = 0;
passengers = 0;
}
public SP2017LAB3_Car(String vin, String model, String color, String make,
int year, int miles, int door, int passenger){
super(vin, model, color, make, year, miles);
doors = door;
passengers = passenger;
}
public int getDoors(){
return doors;
}
public void setDoors(int door){
doors = door;
}
public int getPassengers(){
return passengers;
}
public void setPassengers(int passenger){
passengers = passenger;
}
public void printCar(){
super.toString();
System.out.println("Car make: t" + make + " Model: t" + model + " Number of
passengers: " + passengers +
" Number of doors: " + doors);
19. }
public void carInfo(){
System.out.println("How many passengers can legally ride in the car?");
passengers = keyboard.nextInt();
System.out.println("How many doors does the car have?");
doors = keyboard.nextInt();
}
}
import java.util.Scanner;
public class SP2017LAB3_Motorcycle extends SP2017LAB3_Vehicle {
private static Scanner keyboard = new Scanner(System.in);
boolean sideCars;
String hasSideCar;
public SP2017LAB3_Motorcycle (){
super();
sideCars = false;
}
public SP2017LAB3_Motorcycle (String vin, String model, String color, String make,
int year, int miles) {
super(vin, model, color, make, year, miles);
sideCars = false;
}
public void setSideCars(boolean sideCar){
sideCars = sideCar;
}
public boolean getSideCars(){
return sideCars;
20. }
public void printMotorcycle(){
super.toString();
if (sideCars == true){
hasSideCar = "Yes";
}
else{
hasSideCar = "No";
}
System.out.println("Motorcycle make: t" + make + " Model: t" + model + " Has side car:
" + hasSideCar);
}
}
import java.util.Scanner;
//
public class SP2017LAB3_VehicleDealerShop {
static SP2017LAB3_Vehicle newNode = null;
static String option = null, choice = null;
//
private static Scanner keyboard = new Scanner(System.in);
//
public static void main(String[]args){
//
SP2017LAB3_Vehicle inform = new SP2017LAB3_Vehicle();
//
SP2017LAB3_VehicleUnsortedOptimizedArray array = new
SP2017LAB3_VehicleUnsortedOptimizedArray();
while(true){
//
21. System.out.println("1: Insert a car or motorcycle");
System.out.println("2. Test for the encapsulation of the data structure");
System.out.println("3. Remove a car or motorcycle");
System.out.println("4. Update information of a car or motorcycle");
System.out.println("5. Show all vehicles");
System.out.println("6. Exit");
//
int selection = Integer.parseInt(keyboard.nextLine());
//
switch(selection){
case 1:
//
System.out.println("*** TASK: INSERT and FETCH A CAR OR A MOTORCYCLE ***");
System.out.println("Do you wish to insert a car or motorcycle? Y/N");
option = keyboard.nextVehicleDealerShopLine();
if (option == "Y" || option == "y"){
System.out.println("What is the vin number for the car or motorcycle?");
System.out.println("What is the vin number for the car or motorcycle?");
inform.vin = keyboard.nextLine();
System.out.println("What is the color of the vehicle?");
inform.color = keyboard.nextLine();
System.out.println("What is the year of the vehicle?");
inform.year = keyboard.nextInt();
System.out.println("What is the make of the vehicle?");
inform.make = keyboard.nextLine();
System.out.println("What is the model of the vehicle?");
inform.model = keyboard.nextLine();
System.out.println("Is the vehicle a car? Y/N");
choice = keyboard.nextLine();
if (choice == "Y" || choice == "y"){
System.out.println("How many passengers can legally ride in the car?");
inform.passengers = keyboard.nextInt();
22. System.out.println("How many doors does the car have?");
inform.doors = keyboard.nextInt();
}
else{
System.out.println("Does the motorcycle have a sidecar? Y/N");
inform.sideCar = keyboard.nextLine();
}
array.insert(newNode);
}
else{
System.out.println("What is the vin number for the car or motorcycle?");
array.fetch(inform.vin);
}
//
case 2:
System.out.println("*** TASK: TEST FOR DATA ENCAPSULATION ***");
case3:
//
}
}
}
}
public class SP2017LAB3_VehicleUnsortedOptimizedArray {
private int next;
private int size;
private SP2017LAB3_Vehicle[] data;
public SP2017LAB3_VehicleUnsortedOptimizedArray(){
next = 0;
size = 25;
23. data = new SP2017LAB3_Vehicle[size];
}
public boolean insert(SP2017LAB3_Vehicle newNode){
if (next >= size)
return false;
data[next] = newNode.deepCopy();
if(data[next] == null)
return false;
next = next +1;
return true;
}
public SP2017LAB3_Vehicle fetch(String targetKey){
SP2017LAB3_Vehicle node;
SP2017LAB3_Vehicle temp;
int i = 0;
while(i< next && !(data[i].compareTo(targetKey) == 0)){
i++;
}
if(i== next)
return null;
node = data[i].deepCopy();
if(i != 0){
temp = data[i - 1];
data[i - 1] = data[i];
data[i] = temp;
}
return node;
}
public boolean delete(String targetKey){
int i = 0;