CS Practice
So Awesome!
import java.util.Scanner;
public class AndreaStudies{
private int year;
private String input;
private ArrayList<String> presidents;
// private String [][] presidents;
public AndreaStudies(){
this.year = 1788 + (int) (Math.random()*235);
this.presidents = new ArrayList<String>();
presidents.add("George Washington");
presidents.add("John Adams");
presidents.add("Thomas Jefferson");
presidents.add("James Madison");
presidents.add("James Monroe");
presidents.add("John Quincy Adams");
presidents.add("Andrew Jackson");
presidents.add("Martin Van Buren");
presidents.add("William Henry Harrison");
presidents.add("John Tyler");
presidents.add("James K. Polk");
presidents.add("Zachary Taylor");
presidents.add("Millard Fillmore");
presidents.add("Franklin Pierce");
presidents.add("James Buchanan");
presidents.add("Abraham Lincoln");
presidents.add("Andrew Johnson");
presidents.add("Ulysses S. Grant");
presidents.add("Rutherford B. Hayes");
presidents.add("James A. Garfield");
presidents.add("Chester A. Arthur");
presidents.add("Grover Cleveland");
presidents.add("Benjamin Harrison");
presidents.add("William McKinley");
presidents.add("Theodore Roosevelt");
presidents.add("William Howard Taft");
presidents.add("Woodrow Wilson");
presidents.add("Warren G. Harding");
presidents.add("Calvin Coolidge");
presidents.add("Herbert Hoover");
presidents.add("Franklin D. Roosevelt");
presidents.add("Harry S. Truman");
presidents.add("Dwight D. Eisenhower");
presidents.add("John F. Kennedy");
presidents.add("Lyndon B. Johnson");
presidents.add("Richard Nixon");
presidents.add("Gerald Ford");
presidents.add("Jimmy Carter");
presidents.add("Ronald Reagan");
presidents.add("George H. W. Bush");
presidents.add("Bill Clinton");
presidents.add("George W. Bush");
presidents.add("Barack Obama");
presidents.add("Donald Trump");
presidents.add("Joe Biden");
// Scanner scanner = new Scanner(System.in);
// System.out.print("President: ");
// this.input = scanner.nextLine();
}
public int getYear(){
return year;
}
public String returnPres(this.year){
if this.year < 1789{
return presidents.get(0);
}
if this.year
}
public String toString() {
return ("Andrea, you got this! " + "Year: " + this.year);
}
public static void main(String[] args) {
AndreaStudies presidents = new AndreaStudies();
System.out.println(presidents.toString());
// Scanner name = new Scanner(System.in);
// String pres = name.nextLine();
// System.out.println(pres);
}
}
AndreaStudies.main(null);
import java.util.Scanner;
import java.util.Random;
import java.util.Scanner;
public class Main {
static String[] words = {"apple", "banana", "cherry", "orange", "grape", "watermelon, pear, cherry, pomegranate, kiwi, guava"};
static String word = words[new Random().nextInt(words.length)];
static int count = 0;
static char[] dashes = new char[word.length()];
public static void main(String[] args) {
for (int i = 0; i < dashes.length; i++) {
dashes[i] = '_';
}
Scanner input = new Scanner(System.in);
boolean gameOver = false;
while (!gameOver) {
System.out.println(dashes);
System.out.print("You have seven chances to get the word wrong. The category is fruit. ");
System.out.print("Guess a letter: ");
char guess = input.next().charAt(0);
boolean correctGuess = false;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == guess) {
System.out.println("Correct!");
dashes[i] = guess;
correctGuess = true;
}
}
if (!correctGuess) {
System.out.println("Wrong guess, try again");
count++;
hangmanImage();
}
if (count == 7) {
System.out.println("You lost, the word was: " + word);
gameOver = true;
}
if (new String(dashes).equals(word)) {
System.out.println("Congratulations, you guessed the word!");
gameOver = true;
}
}
}
public static void hangmanImage() {
if (count == 1) {
System.out.println(" +---+");
System.out.println(" | |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("=========");
} else if (count == 2) {
System.out.println(" +---+");
System.out.println(" | |");
System.out.println(" O |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("=========");
} else if (count == 3) {
System.out.println(" +---+");
System.out.println(" | |");
System.out.println(" O |");
System.out.println(" | |");
System.out.println(" |");
System.out.println(" |");
System.out.println("=========");
} else if (count == 4) {
System.out.println(" +---+");
System.out.println(" | |");
System.out.println(" O |");
System.out.println(" /| |");
System.out.println(" |");
System.out.println(" |");
System.out.println("=========");
} else if (count == 5) {
System.out.println(" +---+");
System.out.println(" | |");
System.out.println(" O |");
System.out.println(" /|\\ |");
System.out.println(" |");
System.out.println(" |");
System.out.println("=========");
} else if (count == 6) {
System.out.println(" +---+");
System.out.println(" | |");
System.out.println(" O |");
System.out.println(" /|\\ |");
System.out.println(" / |");
System.out.println(" |");
System.out.println("=========");
} else if (count == 7) {
System.out.println(" +---+");
System.out.println(" | |");
System.out.println(" O |");
System.out.println(" /|\\ |");
System.out.println(" / \\ |");
System.out.println(" |");
System.out.println("=========");
}
}
}
Main.main(null);
//unit 4 hw
//number guesser
import java.util.Scanner;
import java.lang.Math;
Scanner number = new Scanner(System.in);
float num_float = Integer.parseInt(number.nextLine()); // reads input
System.out.println(num_float);
double random = Math.random() * 10;
System.out.println(random);
public class Number{
public static void main(String[] args){
while (num_float != random){
if (num_float < random){
System.out.println("too low. higher");
// Scanner number = new Scanner(System.in);
float num_float = Integer.parseInt(number.nextLine());
System.out.println(num_float);
}
if (num_float > random){
System.out.println("too high. lower");
Scanner number = new Scanner(System.in);
float num_float = Integer.parseInt(number.nextLine());
System.out.println(num_float);
}
}
System.out.println("you got it!");
}
}
Number.main(null)
import java.util.ArrayList;
public static void swapConsecutive(ArrayList<Double> myList)
{
for(int i = 0; i < myList.size()-1; i=i+2)
{
double temp = myList.get(i);
myList.set(i, myList.get(i+1));
myList.set(i+1, temp);
}
}
public double getAverageRating()
{
int sum = 0;
for (i = 0; i<allReviews.length; i++)
{
sum = sum + allReviews[i];
}
return (double) sum/allReviews.length();
}
public class AwesomeClass {
public static void main(String[] args) {
int[] allReviews = {4,3,5,2,3}; // sample input
double averageRating = getAverageRating(allReviews);
System.out.println("Average rating: " + averageRating);
}
public static double getAverageRating(int[] allReviews) {
int sum = 0;
for (int i = 0; i < allReviews.length; i++) {
sum += allReviews[i];
}
return (double) sum / allReviews.length;
}
}
AwesomeClass.main(null);
import java.util.ArrayList;
public class MyClass {
public static void main(String[] args) {
ArrayList<Double> myList = new ArrayList<>();
myList.add(1.0);
myList.add(2.0);
myList.add(3.0);
myList.add(4.0);
System.out.println("Before swapping: " + myList);
swapConsecutive(myList);
System.out.println("After swapping: " + myList);
}
}
MyClass.main(null);
public static boolean recurMethod(String str)
{
if (str.length() <= 1)
{
return true;
}
else if (str.substring(0, 1).compareTo(str.substring(1, 2)) > 0)
{
return recurMethod(str.substring(1));
}
else
{
return false;
}
}
import java.util.ArrayList;
public class MyClass {
public static void main(String[] args) {
System.out.println(recurMethod("edcba"));
}
}
MyClass.main(null);