Week 13
Source Code
import java.util.*;
public class Week13 {
public static void main(String[] args) {
String myName = ""; // add your name
System.out.println("Week 13" + " - " + myName);
// Menu
System.out.println("Menu");
System.out.println("E1 - Example 1");
// 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 "E4":
System.out.println("Example 4");
example4();
break;
case "E5":
System.out.println("Example 5");
example5();
break;
case "E6":
System.out.println("Example 6");
example6();
break;
case "E7":
System.out.println("Example 7");
example7();
break;
default:
System.out.println("Invalid choice");
}
}
// example1 method
public static void example1() {
// Create two points
// Display the points
}
// example2 method
public static void example2() {
// Create two points
// Display the points
// Swap the coordinates
// Display the points
}
// example3 method
public static void example3() {
}
// example4 method
public static void example4() {
}
// example5 method
public static void example5() {
// Create coins for two players
// Each player flips their coin
// Show the results of the flips
// Determine the winner
}
// example6 method
public static void example6() {
// Using the default constructor
}
// example7 method
public static void example7() {
// Using the parameterized constructor
}
}
Point.java
/**
* The Point class represents a point in 2D space
* with x and y coordinates.
*/
public class Point {
// instance variables
/**
* Constructs a Point with specified x and y coordinates.
*
* @param x The x-coordinate of the point.
* @param y The y-coordinate of the point.
*/
/**
* Sets the x-coordinate of the point.
*
* @param x The new x-coordinate.
*/
/**
* Sets the y-coordinate of the point.
*
* @param y The new y-coordinate.
*/
/**
* Gets the x-coordinate of the point.
*
* @return The x-coordinate.
*/
/**
* Gets the y-coordinate of the point.
*
* @return The y-coordinate.
*/
/**
* Returns a string representation of the point.
*
* @return A string in the format "(x, y)".
*/
@Override
}
Coin.java
/**
* The Coin class represents a coin with two sides: Heads and Tails.
* It provides methods to flip the coin, check if it's heads,
* and retrieve the current state as a string.
*/
public class Coin {
// instance variables
// 0 for Heads, 1 for Tails
/**
* Initializes the coin by flipping it to set a random initial state.
*/
/**
* Flips the coin to randomly set the state to Heads or Tails.
*/
/**
* Checks if the current state of the coin is Heads.
*
* @return true if the coin is heads, false otherwise.
*/
/**
* Returns a string representation of the current state of the coin.
*
* @return "Heads" if the coin is heads, "Tails" otherwise.
*/
@Override
}
Book.java
public class Book {
// Properties
// Constructor
// Getters
// Method to mark the book as read
// Method to provide a string representation of the book
@Override
// Main method to test the Book class
public static void main(String[] args) {
// Create three instances of your favorite books
// Print the title, author, and pages for each book
// Mark all books as read
// Print each book's string representation
}
}
Ingredient.java
/**
* The Ingredient class represents an ingredient in a recipe or inventory.
* Each ingredient has a name, an amount, a unit of measurement, and an
* indication of whether it is organic.
*/
public class Ingredient {
// Instance variables
private String name;
private double amount;
private String unit;
private boolean organic;
/**
* Constructs a new Ingredient with the specified properties.
*
* @param name The name of the ingredient.
* @param amount The quantity of the ingredient.
* @param unit The unit of measurement for the ingredient.
* @param organic True if the ingredient is organic; false otherwise.
*/
public Ingredient(String name, double amount, String unit, boolean organic) {
this.name = name;
this.amount = amount;
this.unit = unit;
this.organic = organic;
}
/**
* Returns the name of the ingredient.
*
* @return The name of the ingredient.
*/
public String getName() {
return name;
}
/**
* Returns the amount of the ingredient.
*
* @return The quantity of the ingredient.
*/
public double getAmount() {
return amount;
}
/**
* Returns the unit of measurement for the ingredient.
*
* @return The unit of measurement (e.g., "cups", "grams").
*/
public String getUnit() {
return unit;
}
/**
* Checks if the ingredient is organic.
*
* @return True if the ingredient is organic, false otherwise.
*/
public boolean isOrganic() {
return organic;
}
}
Recipe.java
/**
* The Recipe class represents a recipe with a name, a main ingredient,
* a secondary ingredient, and a cooking time. It provides methods to calculate
* the cost of the recipe, check if the recipe is fully organic, and determine
* the nutritional rating based on ingredient properties.
*/
public class Recipe {
// Instance variables
private Ingredient mainIngredient;
private Ingredient secondaryIngredient;
private String name;
private int cookingTime;
/**
* Constructs a new Recipe with the specified name, ingredients, and cooking
* time.
* If invalid parameters are provided, default values are used instead.
*
* @param name The name of the recipe.
* @param main The main ingredient of the recipe.
* @param secondary The secondary ingredient of the recipe.
* @param cookingTime The cooking time in minutes. If less than or equal to
* zero, defaults to Integer.MIN_VALUE.
*/
public Recipe(String name, Ingredient main, Ingredient secondary, int cookingTime) {
this.name = name.trim();
this.mainIngredient = main;
this.secondaryIngredient = secondary;
// Validate and assign cooking time
if (cookingTime <= 0) {
this.cookingTime = Integer.MIN_VALUE;
} else {
this.cookingTime = cookingTime;
}
}
/**
* Calculates the total cost of the recipe.
*
* @return The total cost of the recipe.
*/
public double getCost() {
double baseCost = mainIngredient.getAmount() * 2.5 +
secondaryIngredient.getAmount() * 1.5;
if (isFullyOrganic()) {
baseCost *= 1.5; // Organic premium
}
return baseCost;
}
/**
* Checks if the recipe is fully organic.
*
* @return True if both ingredients are organic, false otherwise.
*/
public boolean isFullyOrganic() {
return mainIngredient.isOrganic() &&
secondaryIngredient.isOrganic();
}
/**
* Determines the nutritional rating of the recipe.
*
* @return The nutritional rating ("A", "B", or "C").
*/
public String getNutritionalRating() {
if (isFullyOrganic()) {
return "A";
} else if (mainIngredient.isOrganic() || secondaryIngredient.isOrganic()) {
return "B";
} else {
return "C";
}
}
/**
* Returns the cooking time for the recipe.
*
* @return The cooking time in minutes.
*/
public int getCookingTime() {
return cookingTime;
}
/**
* Returns a string representation of the recipe.
*
* @return A string containing the recipe details.
*/
@Override
public String toString() {
String recipeString = "Recipe: " + name + "\n";
recipeString += "Main Ingredient: " + mainIngredient.getName() + "\n";
recipeString += "Secondary Ingredient: " + secondaryIngredient.getName() + "\n";
recipeString += "Rating: " + getNutritionalRating() + "\n";
recipeString += "Cooking Time: " + getCookingTime() + " minutes";
return recipeString;
}
}
MealPlan.java
/**
* The MealPlan class represents a weekly meal plan, including two recipes,
* the total number of servings, and methods to calculate cost and check if
* the plan is fully organic.
*/
public class MealPlan {
/**
* Constructs a MealPlan with the specified name, recipes, and servings.
* If invalid parameters are provided, default values are used.
*
* @param name The name of the meal plan.
* @param breakfastRecipe The breakfast recipe.
* @param lunchRecipe The breakfast recipe.
* @param dinnerRecipe The dinner recipe.
*/
/**
* Calculates the total cost of the meal plan for the week.
*
* @return The total cost of the meal plan.
*/
/**
* Checks if the meal plan is fully organic.
*
* @return True if both recipes are fully organic, false otherwise.
*/
/**
* Returns a summary of the meal plan, including its name, total cost,
* and organic status.
*
* @return A summary string of the meal plan.
*/
public static void main(String[] args) {
// breakfast recipe
// lunch recipe
// dinner recipe
// Valid meal plan
}
}
Calculator.java
import java.util.*;
/**
* The Calculator class provides basic arithmetic operations.
* @author CGA Student
*/
public class Calculator {
/**
* Adds two integers.
*
* @param a the first integer
* @param b the second integer
* @return the sum of a and b
*/
public int add(int a, int b) {
return a + b;
}
/**
* Main method.
*
* @param args command line arguments (not used)
*/
public static void main(String[] args) {
Calculator calculator = new Calculator();
// Example
System.out.println("Addition: " + calculator.add(10, 5));
// Ask the user for two numbers
}
}
Last updated
Was this helpful?