Presentations HW
Homework for peer presentations.
- Unit 1 - Primitive Types FRQ 2006
- Unit 2 - Using Objects - Goblin Game
- Unit 3 - Boolean and If Statements
- Unit 5 - Writing Classes - FRQ 2021 1a and 3a
- Unit 6 - Array
public double purchasePrice() {
return (1 + taxRate) * getListPrice();
}
public int compareCustomer(Customer other) {
int nameCompare = getName().compareTo(other.getName());
if (nameCompare != 0) {
return nameCompare;
}
else {
return getID() - other.getID();
}
}
public class Goblin {
private String name;
private int HP;
private int DMG;
private double hitChance;
public String getName() {
return name;
}
public int getHP() {
return HP;
}
public int getDMG() {
return DMG;
}
public double getHitChance() {
return hitChance;
}
public boolean isAlive() {
if (this.HP > 0) {
return true;
} else {
return false;
}
}
public void setName(String newName) {
this.name = newName;
}
public void setHP(int newHP) {
this.HP = newHP;
}
public void takeDMG(int takenDamage) {
this.HP -= takenDamage;
}
public void setDMG(int newDMG) {
this.DMG = newDMG;
}
public void setHitChance(double newHitChance) {
this.hitChance = newHitChance;
}
}
import java.lang.Math;
public class Duel {
public static void attack(Goblin attackerGoblin, Goblin attackeeGoblin) {
System.out.println(attackerGoblin.getName() + " attacks " + attackeeGoblin.getName() + "!");
if (Math.random() < attackerGoblin.getHitChance()) {
attackeeGoblin.takeDMG(attackerGoblin.getDMG());
System.out.println(attackerGoblin.getName() + " hits!");
System.out.println(attackeeGoblin.getName() + " takes " + attackerGoblin.getDMG() + " damage");
} else {
System.out.println(attackerGoblin.getName() + " misses...");
}
System.out.println(attackeeGoblin.getName() + " HP: " + attackeeGoblin.getHP());
System.out.println();
}
public static void fight(Goblin goblin1, Goblin goblin2) {
while (goblin1.isAlive() && goblin2.isAlive()) {
attack(goblin1, goblin2);
if (!goblin1.isAlive()) {
System.out.println(goblin1.getName() + " has perished");
break;
}
attack(goblin2, goblin1);
if (!goblin2.isAlive()) {
System.out.println(goblin2.getName() + " has perished");
break;
}
}
}
public static void main(String[] args) {
Goblin goblin1 = new Goblin();
goblin1.setName("jeffrey");
goblin1.setHP(12);
goblin1.setDMG(2);
goblin1.setHitChance(0.50);
Goblin goblin2 = new Goblin();
goblin2.setName("Gunther the great");
goblin2.setHP(4);
goblin2.setDMG(1);
goblin2.setHitChance(1);
fight(goblin1, goblin2);
}
}
Duel.main(null);
import java.util.Scanner;
public class Exercise2 {
public static void main(String[] Strings) {
Scanner input = new Scanner(System.in);
System.out.print("Input a: ");
double a = input.nextDouble();
System.out.print("Input b: ");
double b = input.nextDouble();
System.out.print("Input c: ");
double c = input.nextDouble();
double result = b * b - 4.0 * a * c;
if (result > 0.0) {
double r1 = (-b + Math.pow(result, 0.5)) / (2.0 * a);
double r2 = (-b - Math.pow(result, 0.5)) / (2.0 * a);
System.out.println("The roots are " + r1 + " and " + r2);
} else if (result == 0.0) {
double r1 = -b / (2.0 * a);
System.out.println("The root is " + r1);
} else {
System.out.println("The equation has no real roots.");
}
}
}
Exercise2.main(null);
4) Write a Java program that reads a floating-point number and prints "zero" if the number is zero. Otherwise, print "positive" or "negative". Add "small" if the absolute value of the number is less than 1, or "large" if it exceeds 1,000,000.
import java.util.Scanner;
public class Exercise4 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input value: ");
double input = in.nextDouble();
if (input > 0)
{
if (input < 1)
{
System.out.println("Positive small number");
}
else if (input > 1000000)
{
System.out.println("Positive large number");
}
else
{
System.out.println("Positive number");
}
}
else if (input < 0)
{
if (Math.abs(input) < 1)
{
System.out.println("Negative small number");
}
else if (Math.abs(input) > 1000000)
{
System.out.println("Negative large number");
}
else
{
System.out.println("Negative number");
}
}
else
{
System.out.println("Zero");
}
}
}
Exercise4.main(null);
6) Write a Java program that reads in two floating-point numbers and tests whether they are the same up to three decimal places.
import java.util.Scanner;
public class Exercise6 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input floating-point number: ");
double x = in.nextDouble();
System.out.print("Input floating-point another number: ");
double y = in.nextDouble();
x = Math.round(x * 1000);
x = x / 1000;
y = Math.round(y * 1000);
y = y / 1000;
if (x == y)
{
System.out.println("They are the same up to three decimal places");
}
else
{
System.out.println("They are different");
}
}
}
Exercise6.main(null);
8) Write a Java program that takes the user to provide a single character from the alphabet. Print Vowel or Consonant, depending on the user input. If the user input is not a letter (between a and z or A and Z), or is a string of length > 1, print an error message
import java.util.Scanner;
public class Exercise8 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input an alphabet: ");
String input = in.next().toLowerCase();
boolean uppercase = input.charAt(0) >= 65 && input.charAt(0) <= 90;
boolean lowercase = input.charAt(0) >= 97 && input.charAt(0) <= 122;
boolean vowels = input.equals("a") || input.equals("e") || input.equals("i")
|| input.equals("o") || input.equals("u");
if (input.length() > 1)
{
System.out.println("Error. Not a single character.");
}
else if (!(uppercase || lowercase))
{
System.out.println("Error. Not a letter. Enter uppercase or lowercase letter.");
}
else if (vowels)
{
System.out.println("Input letter is Vowel");
}
else
{
System.out.println("Input letter is Consonant");
}
}
}
Exercise8.main(null);
10) Write a program in Java to display the first 10 natural numbers.
public class Exercise10 {
public static void main(String[] args)
{
int i;
System.out.println ("The first 10 natural numbers are:\n");
for (i=1;i<=10;i++)
{
System.out.println (i);
}
System.out.println ("\n");
}
}
Exercise10.main(null);
12) Write a program in Java to input 5 numbers from keyboard and find their sum and average
import java.util.Scanner;
public class Exercise12 {
public static void main(String[] args)
{
int i,n=0,s=0;
double avg;
{
System.out.println("Input the 5 numbers : ");
}
for (i=0;i<5;i++)
{
Scanner in = new Scanner(System.in);
n = in.nextInt();
s +=n;
}
avg=s/5;
System.out.println("The sum of 5 no is : " +s+"\nThe Average is : " +avg);
}
}
Exercise12.main(null);
14) Write a program in Java to display the multiplication table of a given integer.
import java.util.Scanner;
public class Exercise14 {
public static void main(String[] args)
{
int j,n;
System.out.print("Input the number(Table to be calculated): ");
{
System.out.print("Input number of terms : ");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println ("\n");
for(j=0;j<=n;j++)
System.out.println(n+" X "+j+" = " +n*j);
}
}
}
Exercise14.main(null);
16) Write a program in Java to display the pattern like right angle triangle with a number.
import java.util.Scanner;
public class Exercise16 {
public static void main(String[] args)
{
int i,j,n;
System.out.print("Input number of rows : ");
Scanner in = new Scanner(System.in);
n = in.nextInt();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
System.out.print(j);
System.out.println("");
}
}
}
Exercise16.main(null);
18) Write a program in Java to make such a pattern like right angle triangle with number increased by 1.
import java.util.Scanner;
public class Exercise18 {
public static void main(String[] args)
{
int i,j,n,k=1;
System.out.print("Input number of rows : ");
Scanner in = new Scanner(System.in);
n = in.nextInt();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
System.out.print(k++);
System.out.println("");
}
}
}
Exercise18.main(null);
20) Write a program in Java to print the Floyd's Triangle.
import java.util.Scanner;
public class Exercise20 {
public static void main(String[] args)
{
int numberOfRows;
System.out.print("Input number of rows : ");
Scanner in = new Scanner(System.in);
numberOfRows = in.nextInt();
int number = 1;
for (int row = 1; row <= numberOfRows; row++)
{
for (int column = 1; column <= row; column++)
{
System.out.print(number + " ");
number++;
}
System.out.println();
}
}
}
Exercise20.main(null);
public static int numberOfLeapYears(int year1, int year2) {
leapYears = 0
n = year2 - year1;
for (i = 0, i<n, ++i) {
if (isLeapYear(year1 + i)) {
++leapYears;
}
}
return leapYears;
}
public static int dayOfWeek(int month, int day, int year) {
int weekday = firstDayOfYear(year);
int additionalDays = dayOfYear(month, day, year) - 1;
for(int d = 1; d <= additionalDays; d++)
{
weekday++;
if(weekday == 7)
weekday = 0;
}
return weekday;
}
public int scoreGuess(String guess)
{
int count = 0;
for(int i = 0; i < secret.length(); i++)
{
int j = i + guess.length();
if(j <= secret.length() && secret.substring(i, j).equals(guess))
count++;
}
return count * (guess.length() * guess.length());
}
public void addMembers(String[] names, int gradYear)
{
for(String name : names)
memberList.add(new MemberInfo(name, gradYear, true));
}
public class ArrayMethods {
private int[] values;
int i = 2;
public void swapFirstLast()
{
int first = values.get(0);
int last = values.get(values.size() - 1);
values.set(0, last)
values.set(values.get(values.size() - 1), first)
}
public void replaceEvenZero()
{
while (i < values.get(values.size() - 1)); {
values.set(i, 0);
i = i + 2
}
}
}