Week 21
Source Code
import java.util.*;
public class Week21 {
public static void main(String[] args) {
String myName = ""; // add your name
System.out.println("Week 21" + " - " + myName);
// Menu
System.out.println("Menu");
System.out.println("E1 - Example 1");
System.out.println("Q - Quit");
// setup Scanner
Scanner in = new Scanner(System.in);
System.out.print("Choice: ");
String choice = in.nextLine();
// switch choices
switch (choice) {
case "E1":
System.out.println("Example 1");
example1();
break;
case "E2":
System.out.println("Example 2");
example2();
break;
case "E3":
System.out.println("Example 3");
example3();
break;
case "Q":
System.out.println("Quitting..!");
break;
}
}
// example1 method
public static void example1() {
// Create a Greeter
// Create a MeanGreeter
}
// example2 method
public static void example2() {
// Create a Greeter
// Create a MeanGreeter
}
// example3 method
public static void example3() {
}
}
Greeter.java
public class Greeter {
// greet method no parameters
// greet method with one parameter
}
MeanGeeter.java
public class MeanGreeter extends Greeter {
@Override // Compiler will verify this actually overrides a method
// greet method with no parameters
// greet method with one parameter
}
SocialMediaDemo.java
class SocialMediaUser {
private String username;
private String platform;
// constructor
// introduce method
public void introduce() {
}
// Getter methods
public String getUsername() {
return username;
}
public String getPlatform() {
return platform;
}
}
class TeenInfluencer extends SocialMediaUser {
private String contentNiche;
private int followerCount;
// constructor
@Override
public void introduce() {
}
// Helper method to format follower count (e.g., 1500 -> 1.5K)
private String formatFollowerCount(int count) {
if (count >= 1000000) {
return String.format("%.1fM", count / 1000000.0);
} else if (count >= 1000) {
return String.format("%.1fK", count / 1000.0);
} else {
return String.valueOf(count);
}
}
// Helper method to add a catchy tagline based on content niche
private void addTagline() {
switch (contentNiche.toLowerCase()) {
case "gaming":
System.out.println("Let's get this win! #GamerLife");
break;
case "fashion":
System.out.println("Serving looks 24/7! #StyleIcon");
break;
case "music":
System.out.println("Dropping beats and making waves! #MusicLife");
break;
case "fitness":
System.out.println("No pain, no gain! #FitCheck");
break;
default:
System.out.println("Don't forget to like and subscribe! #ContentCreator");
}
}
}
public class SocialMediaDemo {
public static void main(String[] args) {
// Create a regular social media user
// Create some teen influencers with different niches
// Test the introduce methods
}
}
Customer.java
public class Customer
{
private String name;
private String address;
public Customer(String n, String a)
{
name = n;
address = a;
}
public String toString()
{
return "Name: " + name + "\nAddress: " + address;
}
public static void main(String[] args)
{
Customer c = new Customer("Fran Santiago", "123 Main St., Anytown, USA");
System.out.println(c);
// Uncomment these to test OnlineCustomer
// OnlineCustomer c2 = new OnlineCustomer("Jasper Smith",
// "456 High St., Anytown, USA", "jsmith456@gmail.com");
// System.out.println(c2);
}
}
// Complete the OnlineCustomer class to inherit from Customer
// It should have an email attribute,
// a constructor with 3 arguments (name, address, email) that uses the super
// constructor,
// and an overridden toString() method that calls the super toString() method
// and then prints "\nEmail:" and the email variable.
class OnlineCustomer
{
}
MoreShapesDemo.java
// Shape class
class Shape {
// Method to describe the relationship between two shapes
// Overriding toString() method
}
// Square class
class Square extends Shape {
// Overriding toString() method
}
// Rectangle class
class Rectangle extends Shape {
// Overriding toString() method
}
// Main class to demonstrate the relationship method
public class MoreShapesDemo {
public static void main(String[] args) {
// Creating Shapes
// Array of Shapes
// Demonstrating the relationships
}
}
ShoppingCartDemo.java
import java.util.*;
/**
* This code is based upon code in
* https://practiceit.cs.washington.edu/problem/view/bjp4/chapter9/e10-DiscountBill
*/
// Class representing an item with a name and price
class Item {
private String name; // Name of the item
private double price; // Price of the item
// Default constructor initializing item with empty name and price 0.0
public Item() {
this.name = "";
this.price = 0.0;
}
// Constructor that initializes item with given name and price
public Item(String name, double price) {
this.name = name;
this.price = price;
}
// Method to get the price of the item
public double getPrice() {
return price;
}
// Method to format a double value to a currency string
public String valueToString(double value) {
String result = "" + Math.abs(value);
if (result.indexOf(".") == result.length() - 2) {
result += "0";
}
result = "$" + result;
return result;
}
// Overriding toString() to return the item name and price as a string
public String toString() {
return name + " " + valueToString(price);
}
}
// DiscountedItem class inheriting from Item
class DiscountedItem extends Item {
// Instance variable for the discount amount
// Default constructor initializing discount to 0.0
// Constructor that initializes name, price, and discount
// Call the superclass constructor
// Getter method for discount amount
// Setter method for discount amount
// Overriding toString() to include discount amount formatted as currency
}
// ShoppingCart class to manage a collection of items
class ShoppingCart {
private ArrayList<Item> order; // List to store items
private double total; // Total price of items
private double internalDiscount; // Total discount amount
// Constructor initializing the cart with an empty order list and zero totals
public ShoppingCart() {
order = new ArrayList<Item>();
total = 0.0;
internalDiscount = 0.0;
}
// Method to add an item to the cart and update totals
public void add(Item i) {
order.add(i);
total += i.getPrice();
// Check if the item is a discounted item and update discount
// if (i instanceof DiscountedItem)
// internalDiscount += ((DiscountedItem) i).getDiscount();
}
// Method to print the order details
public void printOrder() {
System.out.println(this);
}
// Overriding toString() to return a formatted order summary
public String toString() {
return discountToString();
}
// Method to format the entire order, including subtotal, discount, and total
public String discountToString() {
return orderToString() + "\nSub-total: " + valueToString(total) + "\nDiscount: " + valueToString(internalDiscount) + "\nTotal: " + valueToString(total - internalDiscount);
}
// Method to format a double value to a currency string
private String valueToString(double value) {
value = Math.rint(value * 100) / 100.0;
String result = "" + Math.abs(value);
if (result.indexOf(".") == result.length() - 2) {
result += "0";
}
result = "$" + result;
return result;
}
// Method to build a string representation of all order items
public String orderToString() {
String build = "\nOrder Items:\n";
for (int i = 0; i < order.size(); i++) {
build += " " + order.get(i);
if (i != order.size() - 1) {
build += "\n";
}
}
return build;
}
}
// Main class to demonstrate the ShoppingCart functionality
public class ShoppingCartDemo {
public static void main(String[] args) {
ShoppingCart cart = new ShoppingCart();
// Adding regular items to the cart
cart.add(new Item("bread", 3.25));
cart.add(new Item("milk", 2.50));
// Adding discounted items to the cart
// cart.add(new DiscountedItem("ice cream", 4.50, 1.50));
// cart.add(new DiscountedItem("apples", 1.35, 0.25));
// Print the contents of the cart
cart.printOrder();
}
}
Last updated
Was this helpful?