Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
f23c39cc0a | |||
6336ce0e2d |
@ -1,12 +1,23 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/*
|
||||
CS2 Challenge Solution
|
||||
|
||||
Original Challenge Description: Build a program that requests a user's name,
|
||||
and then prints a greeting.
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
//Setup a Scanner to work with System.in
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
//Start by printing some text that will prompt the user to enter a name
|
||||
//then print another line that Says "Hello" and then whatever the user entered.
|
||||
System.out.print("Please enter your name: ");
|
||||
System.out.println("Hello " + scan.nextLine());
|
||||
|
||||
//Close the scanner, while not really necessary, it's a good habit to get into
|
||||
//for other resources.
|
||||
scan.close();
|
||||
}
|
||||
}
|
@ -1,9 +1,18 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/*
|
||||
CS3 Challenge Solution
|
||||
|
||||
Original Challenge Description: Build a program that requests
|
||||
three numbers from the user, and performs a math operation on
|
||||
them and prints the result.
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
//Start with setting up System.in through a Scanner
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
//Grab three values from the user, all of them are doubles
|
||||
System.out.print("Value 1: ");
|
||||
double firstValue = scan.nextDouble();
|
||||
|
||||
@ -13,8 +22,17 @@ public class Main {
|
||||
System.out.print("Value 3: ");
|
||||
double thirdValue = scan.nextDouble();
|
||||
|
||||
//Print out the operation being performed. Two things of note here,
|
||||
//the first thing in the println is an empty string, this helps Java to know
|
||||
//that it's supposed to be combining Strings, not attempting to do regular
|
||||
//mathematical addition.
|
||||
//The second thing is that we have the actual math at the end of the println
|
||||
//statement wrapped in parenthesis. This helps to ensure that Java treats
|
||||
//that as a math operation, and separates it from all of the combining of Strings
|
||||
//that is going on outside the parenthesis.
|
||||
System.out.println("" + firstValue + " / " + secondValue + " - " + thirdValue + " = " + (firstValue / secondValue - thirdValue));
|
||||
|
||||
//Close the scanner, because good practice.
|
||||
scan.close();
|
||||
}
|
||||
}
|
@ -1,23 +1,42 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/*
|
||||
CS4 Challenge Solution
|
||||
|
||||
Original Challenge Description: Built a program that requests the user's age,
|
||||
and return on of the following:
|
||||
<= 18 - You're a youngin'
|
||||
> 18 AND <= 35 - The early adult years
|
||||
> 35 AND <= 55 - Middle aged, time to buy a really expensive car
|
||||
> 55 AND <= 64 - Getting old...
|
||||
> 64 - Retirement! Time to live out the golden years.
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
//Start with setting up System.in through a Scanner
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
//Request the user's age, as an int
|
||||
System.out.print("Enter your age: ");
|
||||
int age = scan.nextInt();
|
||||
|
||||
//Close the scanner, because good practice
|
||||
scan.close();
|
||||
|
||||
if(age <= 18) {
|
||||
//This is a larger if statement than what we saw in the demos and for good reason.
|
||||
//We cover all the different case scenarios here as described in the challenge, but
|
||||
//notice how I didn't explicitly put an else if statement for > 64 years old condition.
|
||||
//Because it's the last condition, and we have if's or else if's for the other possible
|
||||
//conditions, we can put the last possible condition as the "else" statement.
|
||||
if(age <= 18) { // <= 18
|
||||
System.out.println("You're a youngin'");
|
||||
} else if(age > 18 && age <= 35) {
|
||||
} else if(age > 18 && age <= 35) { // > 18 AND <= 35
|
||||
System.out.println("The early adult years");
|
||||
} else if(age > 35 && age <= 55) {
|
||||
} else if(age > 35 && age <= 55) { // > 35 AND <= 55
|
||||
System.out.println("Middle aged, time to buy a really expensive car");
|
||||
} else if(age > 55 && age <= 64) {
|
||||
} else if(age > 55 && age <= 64) { // > 55 AND <= 64
|
||||
System.out.println("Getting old...");
|
||||
} else {
|
||||
} else { // > 64
|
||||
System.out.println("Retirement! Time to live out the golden years.");
|
||||
}
|
||||
}
|
||||
|
@ -1,26 +1,51 @@
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
|
||||
/*
|
||||
CS5 Challenge Solution
|
||||
|
||||
Original Challenge Description: Write a program that requests an integer
|
||||
from the user, and prints that many random numbers AND prints the sum
|
||||
and average of those numbers.
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
//Setup our random number generator and a scanner
|
||||
//to get input from the user
|
||||
Random random = new Random();
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
//Get the number of random numbers the user
|
||||
//wants us to generate
|
||||
System.out.print("Number of random values to create: ");
|
||||
int numberToRandomize = scan.nextInt();
|
||||
|
||||
//Close the scanner, we shouldn't need it anymore.
|
||||
scan.close();
|
||||
|
||||
//Create a variable to store the sum of all the numbers
|
||||
//we're about to generate. Notice that it is a DOUBLE,
|
||||
//not an int
|
||||
double sum = 0;
|
||||
|
||||
//Setup a for loop, this loop will run for the number
|
||||
//of random numbers the user requested.
|
||||
for(int i = 0; i < numberToRandomize; i++) {
|
||||
//Generate a random number and store it temporarily
|
||||
double randomNumber = random.nextDouble();
|
||||
|
||||
//Properly print the current random number (i + 1),
|
||||
//along with the number that we actually generated
|
||||
System.out.println("Random Number " + (i + 1) + ": " + randomNumber);
|
||||
|
||||
//Add the current random number to the sum.
|
||||
//sum += randomNumber is equivalent to sum = sum + randomNumber
|
||||
sum += randomNumber;
|
||||
}
|
||||
|
||||
//Print the sum, and then print the average.
|
||||
//The average being the sum divided by the number of numbers
|
||||
//to randomize requested by the user.
|
||||
System.out.println("Sum = " + sum);
|
||||
System.out.println("Average = " + (sum / numberToRandomize));
|
||||
}
|
||||
|
@ -1,19 +1,45 @@
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
|
||||
/*
|
||||
CS6 Challenge Solution
|
||||
|
||||
Original Challenge Description: Write a program that generates
|
||||
a random number, and repeatedly asks the user to guess the number,
|
||||
telling them if they're too low, too high, of if they guess correctly.
|
||||
The program should end once they guess the correct number.
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
//Setup our random number generator and a scanner
|
||||
//to get input from the user
|
||||
Random random = new Random();
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
//Generate a random integer 0-100 (inclusive)
|
||||
int randomValue = random.nextInt(101);
|
||||
|
||||
//Create a value for guess and set it to something that
|
||||
//definitely isn't going to be the random number we just
|
||||
//generated. We do this because the while loop that we are
|
||||
//going to use needs to run so long as the guess does not
|
||||
//equal the random value we generated. If it's something
|
||||
//we know isn't possible for the random number, then
|
||||
//we know our loop will run properly at least once.
|
||||
int guess = -1;
|
||||
|
||||
//While the user hasn't guessed the right number
|
||||
while(randomValue != guess) {
|
||||
//Ask the user for a number, and overwrite
|
||||
//the value of guess with that number
|
||||
System.out.print("Guess the number: ");
|
||||
guess = scan.nextInt();
|
||||
|
||||
//This if statement just checks to see if the user's guess
|
||||
//is less, more or the same as what the random generated
|
||||
//number was. If the guess is the same as the random number
|
||||
//the condition of the while loop will handle getting out
|
||||
//of the loop, so no need to call break.
|
||||
if(guess < randomValue) {
|
||||
System.out.println("You're too low");
|
||||
} else if(guess > randomValue) {
|
||||
@ -23,6 +49,7 @@ public class Main {
|
||||
}
|
||||
}
|
||||
|
||||
//Close our scanner, for good measure
|
||||
scan.close();
|
||||
}
|
||||
}
|
@ -1,30 +1,52 @@
|
||||
//A class based representation of a ball
|
||||
public class Ball {
|
||||
//3 instance variables, the first, diameter,
|
||||
//stores the diameter of the ball in whatever
|
||||
//units you want.
|
||||
//The second, a String representation of the color
|
||||
//of the ball.
|
||||
//The third, an integer that stores the number
|
||||
//of times the ball has been bounced.
|
||||
private double diameter;
|
||||
private String color;
|
||||
private int timesBounced;
|
||||
|
||||
//Constructor to create balls with, specifying
|
||||
//the diameter and color of the ball.
|
||||
//Note that timesBounced is set to 0,
|
||||
//this constructor assumes that this is a fresh
|
||||
//ball that has never been bounced.
|
||||
public Ball(double newDiameter, String newColor) {
|
||||
diameter = newDiameter;
|
||||
color = newColor;
|
||||
timesBounced = 0;
|
||||
}
|
||||
|
||||
//Returns the current diameter of the ball
|
||||
public double getDiameter() {
|
||||
return diameter;
|
||||
}
|
||||
|
||||
//Returns the current color of the ball
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
//Returns the number of times this ball has
|
||||
//been bounced
|
||||
public int getTimesBounced() {
|
||||
return timesBounced;
|
||||
}
|
||||
|
||||
//Bounces the ball, all this does is add
|
||||
//one to the timesBounced instance variable
|
||||
public void bounceBall() {
|
||||
timesBounced++;
|
||||
}
|
||||
|
||||
//A normal toString method, that provides the
|
||||
//instance information about the Ball that we
|
||||
//are currently working with as a String
|
||||
public String toString() {
|
||||
return "This ball is " + color + " with a diameter of " +
|
||||
diameter + " and has been bounced " + timesBounced +
|
||||
|
@ -1,14 +1,45 @@
|
||||
/*
|
||||
CS7 Challenge Solution
|
||||
|
||||
Original Challenge Description:
|
||||
|
||||
Write a class Ball with the following:
|
||||
Characteristics
|
||||
diameter (double)
|
||||
color (string)
|
||||
timesBounced (int)
|
||||
Behaviors
|
||||
bounceBall
|
||||
getDiameter
|
||||
getColor
|
||||
getTimesBounced
|
||||
Create 2 Balls
|
||||
Ball 1
|
||||
Diameter: 5
|
||||
Color: Red
|
||||
Bounce the ball 10 times
|
||||
Ball 2
|
||||
Diameter: 1
|
||||
Color: Yellow
|
||||
Bounce the ball 1 time
|
||||
Print all the values for both balls
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
//Create two balls, one 5 units in diameter and red,
|
||||
//and another that's one unit in diameter and yellow
|
||||
Ball ball1 = new Ball(5, "Red");
|
||||
Ball ball2 = new Ball(1, "Yellow");
|
||||
|
||||
//Using a for loop, bounce ball1 10 times
|
||||
for(int i = 0; i < 10; i++) {
|
||||
ball1.bounceBall();
|
||||
}
|
||||
|
||||
//Bounce ball2 once
|
||||
ball2.bounceBall();
|
||||
|
||||
//Print information about both ball1 and ball2
|
||||
System.out.println(ball1);
|
||||
System.out.println(ball2);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user