Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Assignment 04 Solved

The document contains code for a Pizza ordering program that defines a Pizza class with attributes for size, cheese, pepperoni, and ham toppings and methods to calculate cost and get descriptions, and a PizzaOrder class to store multiple Pizza objects and calculate a total order cost. Main method tests creating Pizza objects, getting descriptions and costs, adding Pizzas to an order, and calculating the total order cost.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Assignment 04 Solved

The document contains code for a Pizza ordering program that defines a Pizza class with attributes for size, cheese, pepperoni, and ham toppings and methods to calculate cost and get descriptions, and a PizzaOrder class to store multiple Pizza objects and calculate a total order cost. Main method tests creating Pizza objects, getting descriptions and costs, adding Pizzas to an order, and calculating the total order cost.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

COMSATS University Islamabad, Wah

Campus
Department of Computer Science

ASSIGNMENT NO:04

SUBMITTED TO:
 MS SAMIA ZAFAR

SUBMITTED BY:
 ASHHAR ZAWAR SYED

REGISTRATION NO:
 (SP22-BCS-052)
Activity 1:

package com.company.line;
public class Point {
private int xCord;
private int yCord;

public Point(int x, int y) {


this.xCord = x;
this.yCord = y;
}

public void setXCord(int x) {


this.xCord = x;
}

public int getXCord() {


return this.xCord;
}

public void setYCord(int y) {


this.yCord = y;
}

public int getYCord() {


return this.yCord;
}

public void display() {


System.out.println( this.xCord + " " + this.yCord + ");
}
}

public class Line {


private Point startPoint;
private Point endPoint;

public Line(Point start, Point end) {


this.startPoint = start;
this.endPoint = end;
}

public double getLength() {


int xDiff = this.endPoint.getXCord() - this.startPoint.getXCord();
int yDiff = this.endPoint.getYCord() - this.startPoint.getYCord();
return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
}
}
public class Runner {
public static void main(String[] args) {
Point p1 = new Point(1, 2);
Point p2 = new Point(4, 6);
Line l1 = new Line(p1, p2);
System.out.println("Length of line 1: " + l1.getLength());

Point p3 = new Point(2, 3);


Point p4 = new Point(5, 9);
Line l2 = new Line(p3, p4);
System.out.println("Length of line 2: " + l2.getLength());
}
}

ACTIVITY 2:

PROGRAM:
package com.company.pizza;
public class Pizza {
private String size;
private int cheeseToppings;
private int pepperoniToppings;
private int hamToppings;

public Pizza(String size, int cheese, int pepperoni, int ham) {


this.size = size;
this.cheeseToppings = cheese;
this.pepperoniToppings = pepperoni;
this.hamToppings = ham;
}

public String getSize() {


return this.size;
}

public void setSize(String size) {


this.size = size;
}

public int getCheeseToppings() {


return this.cheeseToppings;
}
public void setCheeseToppings(int cheese) {
this.cheeseToppings = cheese;
}

public int getPepperoniToppings() {


return this.pepperoniToppings;
}

public void setPepperoniToppings(int pepperoni) {


this.pepperoniToppings = pepperoni;
}

public int getHamToppings() {


return this.hamToppings;
}

public void setHamToppings(int ham) {


this.hamToppings = ham;
}

public double calcCost() {


double cost = 0.0;
if (this.size.equals("small")) {
cost = 10.0;
} else if (this.size.equals("medium")) {
cost = 12.0;
} else if (this.size.equals("large")) {
cost = 14.0;
}
cost += 2.0 * (this.cheeseToppings + this.pepperoniToppings + this.hamToppings);
return cost;
}

public String getDescription() {


return "Size: " + this.size + ", Cheese Toppings: " + this.cheeseToppings +
", Pepperoni Toppings: " + this.pepperoniToppings + ", Ham Toppings: " + this.hamToppings;
}
}

public class PizzaOrder {


private Pizza[] pizzas;
private int numPizzas;

public PizzaOrder() {
this.pizzas = new Pizza[3];
this.numPizzas = 0;
}

public void addPizza(Pizza pizza) {


if (this.numPizzas < 3) {
this.pizzas[this.numPizzas] = pizza;
this.numPizzas++;
} else {
System.out.println("Cannot add more than 3 pizzas to the order.");
}
}

public double calcTotal() {


double total = 0.0;
for (int i = 0; i < this.numPizzas; i++) {
total += this.pizzas[i].calcCost();
}
return total;
}
}

public class Runner {


public static void main(String[] args) {
Pizza p1 = new Pizza("large", 1, 1, 1);
Pizza p2 = new Pizza("medium", 0, 0, 1);
System.out.println(p1.getDescription());
System.out.println("Cost of pizza 1: pkr" + p1.calcCost());
System.out.println(p2.getDescription());
System.out.println("Cost of pizza 2: pkr" + p2.calcCost());

PizzaOrder order = new PizzaOrder();


order.addPizza(p1);
order.addPizza(p2);
System.out.println("Total cost of order: pkr" + order.calcTotal());
}
}

You might also like