AP CSA CodeHS 2.4

¡Supera tus tareas y exámenes ahora con Quizwiz!

2.4.7 Chat Bot

import java.util.Scanner; public class BotTester { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Hello. What is your name? "); String name = input.nextLine(); Bot hi = new Bot(name); hi.greeting(); hi.help(); System.out.println("What's the weather like?"); hi.weather(); System.out.print("How many feet in a mile? "); hi.feetInMile(); hi.goodbye(); } } public class Bot { private String name; public Bot (String yourName){ name = yourName; } public void greeting(){ System.out.print("Hello "); System.out.print(name); System.out.println("! My name is Hal!"); System.out.println("How are you today!"); } public void help(){ System.out.println("You can ask me about the weather,"); System.out.println("or how many feet are in a mile."); System.out.println("I can even convert feet to meters!"); } public void weather(){ System.out.println("Its always warm and dry inside your computer!"); } public void feetInMile() { System.out.println("There are 5280 feet in a mile."); } public void goodbye(){ System.out.println("It was nice talking with you!"); System.out.println("Have a great day!"); } public void favoriteNumber(int yourNumber){ System.out.println("My favorite number is 8."); System.out.print("That is "); System.out.print(yourNumber - 8); System.out.println(" away from your number"); } public double feetToMeters(double feet){ double meters = feet * 0.3048; return meters; } }

2.4.5 Hello!

import java.util.Scanner; public class HelloTester { public static void main(String[] args) { // Create a Scanner object Scanner input = new Scanner(System.in); System.out.println("What is your name?"); String name = input.nextLine(); Hello hi = new Hello(name); hi.english(); hi.spanish(); hi.french(); } } public class Hello { private String name; public Hello (String yourName){ name = yourName; } public void english(){ System.out.print("Hello "); System.out.print(name); System.out.println("!"); } public void spanish(){ System.out.print("Hola "); System.out.print(name); System.out.println("!"); } public void french(){ System.out.print("Bonjour "); System.out.print(name); System.out.println("!"); } public void german(){ System.out.print("Hallo "); System.out.print(name); System.out.println("!"); } public void russian(){ System.out.print("Privet "); System.out.print(name); System.out.println("!"); } public void chinese(){ System.out.print("Ni hao "); System.out.print(name); System.out.println("!"); } }

2.4.6 Loose Change

public class Coins { private int quarters; private int dimes; private int nickels; private int pennies; public Coins(int numQuarters, int numDimes, int numNickels, int numPennies){ quarters = numQuarters; dimes = numDimes; nickels = numNickels; pennies = numPennies; } public void addQuarter(){ System.out.println("Adding a quarter ..."); quarters ++; } public void addDime(){ System.out.println("Adding a dime ..."); dimes ++; } public void addNickel(){ System.out.println("Adding a nickel ..."); nickels ++; } public void addPenny(){ System.out.println("Adding a penny ..."); pennies ++; } public void quartersCount(){ System.out.println(quarters); } public void quartersTotal(){ System.out.println(quarters * 0.25); } public void dimesCount(){ System.out.println(dimes); } public void dimesTotal(){ System.out.println(dimes * 0.10); } public void nickelsCount(){ System.out.println(nickels); } public void nickelsTotal(){ System.out.println(nickels * 0.05); } public void penniesCount(){ System.out.println(pennies); } public void penniesTotal(){ System.out.println(pennies * 0.01); } public void bankValue(){ System.out.println(quarters * 0.25 + dimes * 0.10 + nickels * 0.05 + pennies * 0.01); } public void bankCount(){ System.out.println(quarters + dimes + nickels + pennies); } } public class CoinTester { public static void main(String[] args) { Coins numCoins = new Coins(4, 3, 2, 1); numCoins.addQuarter(); numCoins.addQuarter(); numCoins.addDime(); numCoins.addDime(); numCoins.addPenny(); numCoins.bankCount(); numCoins.bankValue(); } }

2.4.8 Greetings and Salutations

public class SalutationsTester { public static void main(String[] args) { Salutations hello = new Salutations("emily"); hello.addressLetter(); hello.signLetter(); hello.addressMemo(); hello.signMemo(); } } public class Salutations { private String name; public Salutations(String theName) { name = theName; } public void addressLetter() { System.out.println("Dear " + name); } public void signLetter() { System.out.println("Sincerely,"); System.out.println(name); } public void addressMemo() { System.out.println("To whom it may concern"); } public void signMemo() { System.out.println("Best,"); System.out.println(name); } }


Conjuntos de estudio relacionados

Geosystems Quiz #11 (Chapter 14)

View Set

Exam 1 (Ch. 1-5 and Dietary Supplements)

View Set