logo

CarYardMIT162011 Class Implementation in Java

This assignment assesses the basic concepts of programming; students should be able to demonstrate their achievements in the following unit learning outcomes: a. describe the fundamental principles of object-oriented programming; b. interpret a user’s needs while dealing with simple program specifications; c. design a simple class using UML notation; d. create a simple application based on UML design and the incremental development process of coding, debugging, and testing; e. apply basic control structures – sequence, repetition, and selection – to program development; f. produce simple interactive applications.

6 Pages1107 Words393 Views
   

Added on  2023-06-11

About This Document

This article explains the implementation of CarYardMIT162011 class in Java with UML class diagram, Car class and Driver class. It covers the functionalities of buying, selling and displaying cars in a car yard. The Car class contains attributes like id, modelName, year, costPrice and sellingPrice. The Driver class contains methods like buy(), sell() and display().

CarYardMIT162011 Class Implementation in Java

This assignment assesses the basic concepts of programming; students should be able to demonstrate their achievements in the following unit learning outcomes: a. describe the fundamental principles of object-oriented programming; b. interpret a user’s needs while dealing with simple program specifications; c. design a simple class using UML notation; d. create a simple application based on UML design and the incremental development process of coding, debugging, and testing; e. apply basic control structures – sequence, repetition, and selection – to program development; f. produce simple interactive applications.

   Added on 2023-06-11

ShareRelated Documents
Task1: UML Class Diagram
Task 2: Car Class
package caryard;
public class Car {
int id;
String modelName;
int year;
double costPrice, sellingPrice;
Car(String name, int year, double costPrice){
setID();
this.modelName = name;
this.year = year;
this.costPrice = costPrice;
sellingPrice = calcSP();
}
public void setID(){
id = (int) ((Math.random() * ((1000 - 100) + 1)) + 100);
}
int getID(){
return id;
}
String getModel(){
return modelName;
}
int getYear(){
return year;
}
CarYardMIT162011 Class Implementation in Java_1
double getCP(){
return costPrice;
}
double getSP(){
return sellingPrice;
}
double calcSP(){
return costPrice*(1 + 0.3);
}
}
Task 3: Driver Class
package caryard;
import java.util.Scanner;
public class CarYardMIT162011 {
static Car[] cars = new Car[100];
static int carCount=0;
static Scanner sc = new Scanner(System.in);
private static void buy(){
System.out.println("\n~ Buy Car ~");
System.out.println("\nEnter model name:");
String name = sc.nextLine();
int year;
while(true){
System.out.println("Enter year:");
year = sc.nextInt();
if(year<2009)
System.out.println("\nError! The car yard does not
buy cars manufactured before 2009.");
else
break;
}
System.out.println("Enter price: $");
double price = sc.nextDouble();
cars[carCount]=new Car(name,year,price);
System.out.println("\nNew car added: "+name+" ("+year+")");
carCount++;
}
private static void display(){
System.out.println("\n~ Car List ~");
for(int i=0;i<carCount;i++){
System.out.println("\n"+(i+1)+":");
System.out.println("\n\tCar ID: "+cars[i].getID());
System.out.println("\n\tModel: "+cars[i].getModel());
System.out.println("\n\tYear: "+cars[i].getYear());
System.out.println("\n\tCost price: $"+cars[i].getCP());
System.out.println("\n\tSelling price:
$"+cars[i].getSP());
System.out.println();
}
}
CarYardMIT162011 Class Implementation in Java_2
private static int sell(){
System.out.println("\n~ Sell Car ~");
System.out.println("\nEnter car ID:");
int id = sc.nextInt();
for(int i=0;i<carCount;i++){
if(cars[i].getID()==id){
System.out.println("\n\tModel:
"+cars[i].getModel());
System.out.println("\n\tYear: "+cars[i].getYear());
System.out.println("\n\tCost price:
$"+cars[i].getCP());
System.out.println("\n\tSelling price:
$"+cars[i].getSP());
return 1;
}
}
System.out.println("No match found for car ID: "+id);
return 0;
}
public static void main(String[] args){
int choice=1;
cars[carCount]=new Car("Mazda",2009,15000);
carCount++;
cars[carCount]=new Car("Suzuki",2018,7000);
carCount++;
cars[carCount]=new Car("Royce",2015,12000);
carCount++;
while(choice!=4){
System.out.println("\n~ Main Menu ~");
System.out.print("1. Buy Car\n2. Sell Car\n3. Display all
cars\n4. Exit\n\nEnter menu choice... ");
choice = sc.nextInt();
sc.nextLine();
switch(choice){
case 1: buy();
break;
case 2: sell();
break;
case 3: display();
break;
case 4:
System.out.println("\nGoodbye! Have a nice day!");
break;
default: System.out.println("Wrong entry!");
}
}
}
}
CarYardMIT162011 Class Implementation in Java_3

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
Java Program to Draw Shapes - Desklib
|10
|1283
|284

Object Oriented Programming with Java
|9
|1142
|153

NIM Game Playing Interface in Java
|8
|2250
|180

The Java Programming Document
|19
|2557
|43

Design Documentation Pseudo Code BEGIN Display Welcome message BEGIN
|13
|1512
|68

Memory Sequence Game - Java Program for Console Based Game
|8
|998
|259