Final challenge: model a thing as a class
After this: Predict, complete and explain programs built from classes and objects.
This challenge has five steps, and each step is scored on its own. You will predict what two accounts print after a deposit and a refused withdrawal, complete a constructor line, tell a class from an object, add up a list of records, and say why a method has no static. Every step shows the code in Python and in Java; the answers are the same in both. You can try as many times as you like, and hints work as they do in the lessons.
Step 1 of 5Predict it
Predict it
Two accounts, a deposit on one and a withdrawal from the other. What does this print?
class Account: def __init__(self, name, balance): self.name = name self.balance = balance
def deposit(self, amount): self.balance = self.balance + amount
def withdraw(self, amount): if amount > self.balance: return False self.balance = self.balance - amount return True
a = Account("Ana", 50)b = Account("Ben", 20)a.deposit(10)b.withdraw(30)print(a.balance, b.balance)class Account { String name; int balance;
Account(String name, int balance) { this.name = name; this.balance = balance; }
void deposit(int amount) { balance = balance + amount; }
boolean withdraw(int amount) { if (amount > balance) { return false; } balance = balance - amount; return true; }}
public class Main { public static void main(String[] args) { Account a = new Account("Ana", 50); Account b = new Account("Ben", 20); a.deposit(10); b.withdraw(30); System.out.println(a.balance + " " + b.balance); }}Complete the constructor
Complete the first line of the constructor, so that the new pet's field name holds the value given in the call, and the program prints Rex.
class Pet: def __init__(self, name, age): ____________________ # ← put name in the new pet's own box self.age = age
p = Pet("Rex", 3)print(p.name)class Pet { String name; int age;
Pet(String name, int age) { ____________________ // ← put name in the new pet's own box this.age = age; }}
public class Main { public static void main(String[] args) { Pet p = new Pet("Rex", 3); System.out.println(p.name); }}Class or object
A file defines Account, and the program runs a = Account("Ana", 100)Account a = new Account("Ana", 100); and then b = Account("Ben", 10)Account b = new Account("Ben", 10);. How many classes and how many objects are there?
Add up the records
A list of items, each with a price and a count. What does this print?
class Item: def __init__(self, name, price, count): self.name = name self.price = price self.count = count
def cost(self): return self.price * self.count
cart = [Item("pen", 2, 3), Item("pad", 5, 1)]total = 0for item in cart: total = total + item.cost()print(total)import java.util.ArrayList;import java.util.List;
class Item { String name; int price; int count;
Item(String name, int price, int count) { this.name = name; this.price = price; this.count = count; }
int cost() { return price * count; }}
public class Main { public static void main(String[] args) { ArrayList<Item> cart = new ArrayList<>(List.of(new Item("pen", 2, 3), new Item("pad", 5, 1))); int total = 0; for (Item item : cart) { total = total + item.cost(); } System.out.println(total); }}Why no static
In the Java file, main is written public static void main, and deposit is written void deposit(int amount). Why does deposit have no static?
- Predict what a program of objects prints, complete a constructor line, tell a class from an object, add up a list of records, and say why a method has no static.