inital commit

This commit is contained in:
gabriel venberg 2021-03-26 22:45:38 -05:00
commit aa37ae1eec
55 changed files with 3927 additions and 0 deletions

19
lab08/grades.txt Normal file
View file

@ -0,0 +1,19 @@
88
78
96
75
99
56
78
84
93
79
90
85
79
90
85
79
92
99
94

77
lab08/src/Grades.java Normal file
View file

@ -0,0 +1,77 @@
/*
* Copyright (C) 2020 Gabriel Venberg
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
*
* @author toric
*/
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class Grades {
public static void main(String args[]) throws IOException {
//initialization
int grade=0;
int as=0, bs=0, cs=0, ds=0, fs=0;
int passed;
int studentCount=0;
int highestGrade=0;
int lowestGrade=100;
int average=0;
File inputFile = new File("grades.txt");
Scanner file = new Scanner(inputFile);
//start iterating over file.
while(file.hasNext()) {
grade = file.nextInt();
studentCount ++;
//for now we use average to keep a rolling total. we will divide by num of students after.
average = average+grade;
highestGrade = Math.max(grade, highestGrade);
lowestGrade = Math.min(grade, lowestGrade);
//calculate whether this was an a, b, c, d, or f.
if (grade>=90){
as++;
}
else if (grade>=80) {
bs++;
}
else if (grade>=70) {
cs++;
}
else if (grade>=60) {
ds++;
}
else {
fs++;
}
}
//final calcs, now that we have the whole file.
average = average/studentCount;
passed = as+bs+cs+ds;
//output everything.
System.out.println("Pleas put the data in a text file, labeled \'grades.txt\', and format it as one grade per line.");
System.out.println("Out of "+studentCount+" students, "+passed+" passed,"+'\n'+
as+" got A's, "+bs+" got B's, "+cs+" got C's, "+ds+" got D's, and "+fs+" failed.");
System.out.println("The hightest grade was "+highestGrade+" and the lowest grade was "+lowestGrade+'.'+'\n'+
"The average of all the grades was "+average+'.');
}
}

79
lab08/src/HiLo.java Normal file
View file

@ -0,0 +1,79 @@
/*
* Copyright (C) 2020 Gabriel Venberg
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
*
* @author toric
*/
import java.util.Scanner;
import java.util.Random;
public class HiLo {
public static void main(String args[]) {
//initialization
Random random = new Random();
Scanner scan = new Scanner(System.in);
int target;
int guess=0;
int tries = 1;
String input;
final int SENTINEL = -1;
final int maxTarget = 100;
final int minTarget = 1;
//generate our target.
target = random.nextInt((maxTarget-minTarget)+1)+minTarget;
//explain the game.
System.out.println("I have chosen a number between "+minTarget+" and "+maxTarget+".");
System.out.println("Each time you guess, I will tell you if your guess is high or low.");
System.out.println("The goal is to get the number in an few tries as possible.");
System.out.println("If you want to stop playing, type \'exit\'");
while(guess!=SENTINEL) {
//get next guess.
System.out.println("Try "+tries);
System.out.print("Enter your guess >");
input = scan.nextLine();
System.out.println();
//figure out what our output should be
if (input.equals("exit")){
//why cant we just use a break here?
guess = SENTINEL;
}
else{
//if we are not exiting, convert the input to a proper integer, and do the comparison.
guess = Integer.valueOf(input);
if (guess < target) {
System.out.println("Your guess is too low.");
tries ++;
}
else if (guess > target) {
System.out.println("your guess is too high");
tries ++;
}
else if (guess == target) {
System.out.println("Your guess is correct!");
System.out.println("It took you "+tries+" tries to guess it.");
//if I didnt have to use a sentinel value here, I could have prevented the nested if statements and just used a break.
guess = SENTINEL;
}
}
}
}
}

View file

@ -0,0 +1,102 @@
/*
* Copyright (C) 2020 Gabriel Venberg
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
*
* @author toric
*/
import java.util.Scanner;
import java.util.Random;
public class RockPaperSissors {
public static void main(String args[]) {
//initialization
Random random = new Random();
Scanner scan = new Scanner(System.in);
int wins=0;
int losses=0;
int comp;
int human;
int result;
//humanChoice must be a string for the exit command. compChoice can just be a char.
char humanChoice;
char compChoice;
while (true) {
comp = random.nextInt(3);
System.out.println("Enter \'R\', \'P\' or \'S\' for your input.");
System.out.println("or enter \'Q\' to exit the game.");
System.out.print(">");
humanChoice = scan.next().charAt(0);
if (humanChoice == 'Q'){
System.out.println("You had "+wins+" victories and "+losses+" defeats.");
break;
}
//converting between the numbers and the human readable stuff.
switch(comp) {
case 0:
compChoice = 'R';
break;
case 1:
compChoice = 'P';
break;
case 2:
compChoice = 'S';
break;
default:
//compiler wont shut up if variable does not have a default.
compChoice = '?';
System.out.println("Something went wrong!");
continue;
}
switch(humanChoice) {
case 'R':
human = 0;
break;
case 'P':
human = 1;
break;
case 'S':
human = 2;
break;
default:
//compiler wont shut up if variable does not have a default.
//we restart the loop if the input is invalid.
human = 0;
System.out.println("Sorry, invalid input.");
continue;
}
//some modulus magic finds the winner.
//Java does strange things with negative moduluses, so im adding a 3 in there so it is always positive.
result = (comp-human+3)%3;
System.out.println("You chose "+humanChoice+", and the computer chose "+compChoice);
switch(result){
case 0:
System.out.println("You tied.");
break;
case 1:
System.out.println("You lost.");
losses++;
break;
case 2:
System.out.println("You won!");
wins++;
break;
}
}
}
}