-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Instances of classes and Object instantiation
Our main idea is to create a restaurant ordering system: Design a simple restaurant ordering system where students can instantiate objects representing menu items, customers, and orders. Students can then place orders by creating instances of order objects and adding menu items to them. This approach helps students grasp the concept of object instantiation in a real-world context.
here is some basic code provided by chatgpt to serve as an example:
import java.util.ArrayList;
// Define MenuItem class to represent items on the menu
class MenuItem {
private String name;
private double price;
public MenuItem(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
// Define Order class to represent an order placed by a customer
class Order {
private ArrayList<MenuItem> items;
public Order() {
items = new ArrayList<>();
}
public void addItem(MenuItem item) {
items.add(item);
}
public double getTotalPrice() {
double totalPrice = 0;
for (MenuItem item : items) {
totalPrice += item.getPrice();
}
return totalPrice;
}
public void displayOrder() {
System.out.println("Order:");
for (MenuItem item : items) {
System.out.println("- " + item.getName() + " $" + item.getPrice());
}
System.out.println("Total Price: $" + getTotalPrice());
}
}
public class RestaurantOrderingSystem {
public static void main(String[] args) {
// Create menu items
MenuItem pizza = new MenuItem("Pizza", 10.99);
MenuItem burger = new MenuItem("Burger", 8.99);
MenuItem salad = new MenuItem("Salad", 5.99);
// Create orders
Order order1 = new Order();
order1.addItem(pizza);
order1.addItem(salad);
Order order2 = new Order();
order2.addItem(burger);
order2.addItem(burger);
order2.addItem(burger);
// Display orders
System.out.println("Order 1:");
order1.displayOrder();
System.out.println("\nOrder 2:");
order2.displayOrder();
}
}2D arrays
An example that we can use of 2D arrays would be a farming simulator that allows you to plant and remove corn on a 2d grid
here is some basic code provided by chatgpt to serve as an example:
import java.util.Scanner;
public class FarmSimulator {
private char[][] farm;
private int numRows;
private int numCols;
public FarmSimulator(int numRows, int numCols) {
this.numRows = numRows;
this.numCols = numCols;
farm = new char[numRows][numCols];
initializeFarm();
}
private void initializeFarm() {
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
farm[i][j] = '~'; // '~' represents empty land
}
}
}
public void displayFarm() {
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
System.out.print(farm[i][j] + " ");
}
System.out.println();
}
}
public void plantCrop(int row, int col, char cropType) {
if (row >= 0 && row < numRows && col >= 0 && col < numCols) {
farm[row][col] = cropType;
} else {
System.out.println("Invalid coordinates!");
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Farm Simulator!");
System.out.print("Enter the number of rows: ");
int numRows = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int numCols = scanner.nextInt();
FarmSimulator farmSimulator = new FarmSimulator(numRows, numCols);
while (true) {
System.out.println("\nCurrent Farm Layout:");
farmSimulator.displayFarm();
System.out.print("\nEnter the row and column to plant a crop (row col cropType): ");
int row = scanner.nextInt();
int col = scanner.nextInt();
char cropType = scanner.next().charAt(0);
farmSimulator.plantCrop(row, col, cropType);
}
}
}Metadata
Metadata
Assignees
Labels
No labels