Created
January 28, 2021 11:55
-
-
Save OpenSrcerer/32898cf362667c19e36d93cc3a9dfe8c to your computer and use it in GitHub Desktop.
1-20 + Extra Exercises CS106
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tester; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.*; | |
public class Test { | |
public static void main(String[] args) { | |
rollingDice(); | |
} | |
private static void exercise1() { | |
for (int iterator = 5; iterator <= 99; ++iterator) { | |
if (iterator % 2 != 0) { | |
System.out.println(iterator); | |
} | |
} | |
} | |
private static void exercise2() { | |
for (float celsius = -40; celsius <= 40; celsius+=0.5) { | |
float fahrenheit = ((9 * celsius) / 5) + 32; | |
System.out.println(celsius + "C in Fahrenheit is: " + fahrenheit + "F"); | |
} | |
} | |
public static void exercise3() { | |
int sum = 0; | |
for (int i = 1; i <= 100; ++i) { | |
sum += i; | |
} | |
System.out.println("Sum: " + sum); | |
} | |
public static void exercise4() { | |
int product = 1; | |
for (int i = 1; i <= 21; i+=2) { | |
product = product * i; | |
} | |
System.out.println(product); | |
} | |
public static void exercise5() { | |
int sum = 0, var = 5; | |
while (true) { | |
sum += Math.pow(var, 2); | |
var += 5; | |
if (sum > 10000) { | |
sum -= Math.pow(var, 2); | |
break; | |
} | |
} | |
System.out.println(sum); | |
} | |
public static void exercise6() { | |
double[] heights = { 1.81, 2.01, 1.93, 1.92, 1.99, 1.93, 1.95, 1.60, 1.99, 2.00 }; | |
OptionalDouble d = Arrays.stream(heights).average(); | |
if (d.isPresent()) { | |
System.out.println(d.getAsDouble()); | |
} | |
} | |
public static void exercise7() { | |
try ( | |
BufferedReader reader = new BufferedReader( | |
new InputStreamReader(System.in) | |
) | |
) { | |
int max = 0, aStudents = 0; | |
for (int index = 0; index < 8; ++index) { | |
int grade = Integer.parseInt(reader.readLine()); | |
if (grade < 0 || grade > 100) { | |
throw new IllegalArgumentException("Grade should be between 0 and 100. (inclusive)"); | |
} | |
if (grade >= 90) { | |
aStudents++; | |
} | |
if (grade > max) { | |
max = grade; | |
} | |
} | |
System.out.println("The highest grade is " + max + " and the number of students that have received an A is " + aStudents); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public static void exercise8() { | |
try ( | |
BufferedReader reader = new BufferedReader( | |
new InputStreamReader(System.in) | |
) | |
) { | |
int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE, inputs = 0; | |
while (true) { | |
int a = Integer.parseInt(reader.readLine()); | |
if (a == 0) { | |
break; | |
} | |
if (a > max) { | |
max = a; | |
} | |
if (a < min) { | |
min = a; | |
} | |
++inputs; | |
} | |
System.out.println(max + "\n" + min + "\n" + inputs + "\n"); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public static void exercise9() { | |
try ( | |
BufferedReader reader = new BufferedReader( | |
new InputStreamReader(System.in) | |
) | |
) { | |
int sum = 0, count = 0; | |
while (true) { | |
int a = Integer.parseInt(reader.readLine()); | |
sum += a; | |
if (sum > 2000) { | |
break; | |
} | |
++count; | |
} | |
System.out.println("Average: " + sum / count); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public static void exercise10() { | |
int population = 10000000; | |
for (int year = 1; year <= 15; ++year) { | |
population *= 1.02; | |
} | |
System.out.println(population); | |
} | |
public static void exercise11() { | |
try ( | |
BufferedReader reader = new BufferedReader( | |
new InputStreamReader(System.in) | |
) | |
) { | |
int year = Integer.parseInt(reader.readLine()); | |
int days = 0; | |
for (int index = year; index < 2020; ++index) { | |
if (index % 4 == 0) { | |
days += 366; | |
} else { | |
days += 365; | |
} | |
} | |
System.out.println(days); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public static void exercise12() { | |
int row=32; | |
int totalseats=800; | |
for (int i=0; i<row; ++i) | |
{ | |
totalseats=totalseats+100; | |
} | |
System.out.println("The number of total seats are: "+totalseats); | |
} | |
public static void exercise13() throws IOException // Declares that an error (exception) may occur | |
{ | |
int cost = 200, budget = 47000, sum = 0, depth = 0; | |
while (sum < 47000) | |
{ | |
sum = sum + cost; | |
if(sum > 47000) | |
{ | |
break; | |
} | |
cost = cost + 50; | |
depth++; | |
} | |
System.out.println("the maximal depth the farmer can afford is: " + depth + " meters" ); | |
} | |
public static void exercise14() { | |
Scanner sc = new Scanner(System.in); | |
System.out.println("enter an integer"); | |
int a = sc.nextInt(); | |
System.out.println(""); | |
System.out.println("enter another integer"); | |
int b = sc.nextInt(); | |
int count_for12 = 0; | |
while(a < b) | |
{ | |
if(a % 12 == 0) | |
{ | |
count_for12 = count_for12 + 1; | |
} | |
a = a+1; | |
} | |
System.out.println( "there are " + count_for12 + " multiples of 3 and 4" ); | |
} | |
public static void exercise15() throws IOException // Declares that an error (exception) may occur | |
{ | |
int a = 0; | |
int count = 0; | |
while(a < 1000) | |
{ | |
if(a % 2 == 0 && a % 3 == 0 && a % 4 != 0) | |
{ | |
System.out.println(a); | |
count = count+1; | |
} | |
a = a+1; | |
} | |
System.out.println("there are: " + count + " numbers divisible by 2 and 3 but not by 4" ); | |
} | |
public static void exercise16() { | |
try ( | |
BufferedReader reader = new BufferedReader( | |
new InputStreamReader(System.in) | |
) | |
) { | |
int evens = 0, odds = 0; | |
while (true) { | |
int input = Integer.parseInt(reader.readLine()); | |
if (input < 1) { | |
throw new IllegalArgumentException(); | |
} | |
if (input % 2 == 0) { | |
++evens; | |
} else { | |
++odds; | |
} | |
if (evens == 400) { | |
System.out.println("Evens!"); | |
break; | |
} | |
if (odds == 400) { | |
System.out.println("Odds!"); | |
break; | |
} | |
} | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public static void exercise17() { | |
try ( | |
BufferedReader reader = new BufferedReader( | |
new InputStreamReader(System.in) | |
) | |
) { | |
int input, numbers = 5, sum = 0; | |
for (int index = 0; index < 5; ++index) { | |
input = Integer.parseInt(reader.readLine()); | |
sum += input; | |
} | |
while (sum < 1000) { | |
input = Integer.parseInt(reader.readLine()); | |
sum += input; | |
++numbers; | |
} | |
System.out.println("Numbers that were entered: " + numbers); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public static void exercise18() { | |
int[] integerArray = new int[10]; | |
integerArray[0] = 3; | |
for (int index = 1; index < integerArray.length; ++index) { | |
integerArray[index] = integerArray[index - 1] * index; | |
} | |
} | |
public static void exercise19() throws IOException { | |
InputStreamReader isr = new InputStreamReader(System.in); // Variable (object) reading the keystrokes | |
BufferedReader br = new BufferedReader(isr); // Variable (object) translating the input stream | |
String inData = null; // Text variable to receive the user's input | |
int[] elements = new int[5]; | |
for (int i = 0; i < 5; i++) { | |
System.out.println("Enter a number: "); | |
inData = br.readLine(); | |
elements[i] = Integer.parseInt(inData); | |
} | |
for (int i = elements.length - 1; i >= 0; i--) { | |
System.out.println(2 * elements[i]); | |
} | |
} | |
public static void exercise20v1() { | |
Scanner input = new Scanner(System.in); | |
int[] ID = new int[10]; | |
int[] grade = new int[10]; | |
int average, sum = 0; | |
System.out.println("\tPlease enter your ID and your Grade:"); | |
for (int i = 0; i < 10; i++) { | |
System.out.println("\tPlease enter your ID"); | |
while (!input.hasNextInt()) | |
{ | |
System.out.print("\tPlease enter an integer!.\n"); | |
String var9 = input.next(); | |
} | |
ID[i] = input.nextInt(); | |
if (ID[i]< 0 || ID[i] > 30) | |
{ | |
System.out.println("Please enter a valid ID"); | |
while(ID[i]< 0 || ID[i] > 30) | |
{ | |
System.out.println("Please enter a valid ID"); | |
ID[i] = input.nextInt(); | |
} | |
} | |
System.out.println("\tPlease enter your Grade:"); | |
while (!input.hasNextInt()) { | |
System.out.print("\tPlease enter an integer!.\n"); | |
String var9 = input.next(); | |
} | |
grade[i] = input.nextInt(); | |
if (grade[i] < 0 || grade[i]> 100) | |
{ | |
System.out.println("Please enter a valid grade."); | |
while(grade[i] < 0 || grade[i]> 100) | |
{ | |
System.out.println("Please enter a valid grade."); | |
grade[i] = input.nextInt(); | |
} | |
} | |
sum = sum + grade[i]; | |
} | |
average = sum / 10; | |
for(int i=0; i< grade.length; ++i) | |
{ | |
if (grade[i]>average) | |
System.out.println("The student with ID "+ ID[i]+ " has a grade "+ grade [i]+ " over the average of the class of "+ average+"."); | |
} | |
} | |
public static void exercise20v2() { | |
Map<Integer, Float> map = new HashMap<>(); | |
Random random = new Random(); | |
int id; | |
float average, input, gradeSum = 0; | |
try ( | |
BufferedReader reader = new BufferedReader( | |
new InputStreamReader(System.in) | |
) | |
) { | |
for (int index = 0; index < 10; ++index) { | |
id = random.nextInt(31) + 1; | |
input = Float.parseFloat(reader.readLine()); | |
gradeSum += input; | |
map.put(id, input); | |
} | |
average = gradeSum / 10; | |
for (Map.Entry<Integer, Float> entry : map.entrySet()) { | |
if (entry.getValue() > average) { | |
System.out.println(entry.getKey()); | |
} | |
} | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public static void rollingDice() { | |
Random random = new Random(); | |
int[] frequencies = {0, 0, 0, 0, 0, 0}; | |
int[] doubleFrequencies = {0, 0, 0, 0, 0, 0}; | |
int[] sums = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | |
try ( | |
BufferedReader reader = new BufferedReader( | |
new InputStreamReader(System.in) | |
) | |
) { | |
int numberOfDiceThrows = Integer.parseInt(reader.readLine()); | |
for (int index = 0; index < numberOfDiceThrows; ++index) { | |
int die1 = random.nextInt(6) + 1; | |
int die2 = random.nextInt(6) + 1; | |
++frequencies[die1 - 1]; | |
++frequencies[die2 - 1]; | |
++sums[die1 + die2 - 2]; | |
if (die1 == die2) { | |
++doubleFrequencies[die1 - 1]; | |
} | |
} | |
System.out.println("---------- Statistics ----------"); | |
System.out.println("---------- Number Frequencies ----------"); | |
for (int index = 1; index <= 6; ++index) { | |
System.out.println("Frequency of [" + index + "]: " + frequencies[index - 1]); | |
System.out.println("Fraction of roll: " + (frequencies[index - 1] / (float) Arrays.stream(frequencies).sum()) * 100 + "%"); | |
} | |
System.out.println("---------- Double Frequencies ----------"); | |
for (int index = 1; index <= 6; ++index) { | |
System.out.println("Frequency of double roll [" + index + "]: " + doubleFrequencies[index - 1]); | |
System.out.println("Fraction of roll: " + (doubleFrequencies[index - 1] / (float) Arrays.stream(doubleFrequencies).sum()) * 100 + "%"); | |
} | |
System.out.println("---------- Sum Frequencies ----------"); | |
for (int index = 2; index <= 12; ++index) { | |
System.out.println("Frequency of sum [" + index + "]: " + sums[index - 2]); | |
System.out.println("Fraction of sum: " + (sums[index - 2] / (float) Arrays.stream(sums).sum()) * 100 + "%"); | |
} | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment