Compare commits

...

2 Commits

Author SHA1 Message Date
8f7143d6b4 Update java input/output/exceptions 2021-03-10 16:07:13 -05:00
aeb8c6c24f Update java class 2021-03-10 15:30:40 -05:00
28 changed files with 312 additions and 0 deletions

BIN
cpp/midterm/a.out Executable file

Binary file not shown.

39
cpp/midterm/main.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <iostream>
using namespace std;
int main() {
int num;
int curr;
int i;
int sum, currValue, x;
// string input;
// string input2;
//cin >> input;
//getline(cin,input2);
//getline(cin,input2);
//cout << input << " " << input2 << endl;
// for (int i=0; i < 100; i++){
// cout << i + 1 << endl;
// }
//cin >> num;
//for (i = 0; i < num; ++i) {
// cin >> curr;
// cout << curr;
//}
//cin >> x;
/*sum = 0;
for (i = 0; i < x; ++i) {
cin >> currValue;
sum += currValue;
}
cout << sum << endl;
*/
x = 18;
while (x>0){
cout << x << " ";
x = x/3;
}
return 0;
}

View File

@ -0,0 +1,33 @@
public class Flower extends Plant {
private boolean isAnnual;
private String color;
public Flower () {
super();
this.isAnnual = false;
this.color = "TBD";
}
public void setPlantType(boolean isAnnual){
this.isAnnual=isAnnual;
}
public boolean getPlantType(){
return this.isAnnual;
}
public void setColorOfFlowers(String color){
this.color=color;
}
public String getColorOfFlowers(){
return this.color;
}
@Override
public void printInfo(){
System.out.println("Plant Information:");
System.out.println("\tPlant name: " + name);
System.out.println("\tCost: " + cost);
System.out.println("\tAnnual: " + isAnnual);
System.out.println("\tColor of flowers: " + color + "\n");
}
}

View File

@ -0,0 +1,5 @@
Lee 18
Lua 21
Mary Beth 19
Stu 33
-1

View File

@ -0,0 +1,4 @@
Lee 19
Lua 22
Mary 0
Stu 34

View File

@ -0,0 +1,94 @@
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
/*
* Same as the input one and the string vs int one, but now writting to a file
* instead of the screen.
*/
public class main {
public static ArrayList<String> getInput(Scanner scnr){
String s = "";
String name;
int age;
ArrayList<String> results = new ArrayList<String>();
while(true){
//Get name or exit
s = scnr.next();
if (s.equals("-1")){
System.out.println();
break;
}
else{
name = s;
}
//Get age
try{
age = scnr.nextInt() +1;
}
catch(Exception e){
System.out.println("Age must be an Integer\n" +
"Setting age to 0 ");
age = 0;
}
//Finish line
scnr.nextLine();
//Add name plus new age to arr
results.add(name + " " + Integer.toString(age));
System.out.println();
}
return results;
}
public static void main(String[] args) throws IOException {
FileInputStream fileByteStream = null; // File input stream
Scanner inFS = null; // Scanner object
ArrayList<String> userInputs = new ArrayList<String>();
//Get input
System.out.println("Opening file NamesAndAges.txt.");
fileByteStream = new FileInputStream("NamesAndAges.txt");
inFS = new Scanner(fileByteStream);
System.out.println("Reading in file and processing");
userInputs = getInput(inFS);
System.out.println("Closing scanner and writing out results " +
"To NamesAndAgesUpdated.txt");
fileByteStream.close();
inFS.close();
//Write to file
FileOutputStream fileStream = null;
PrintWriter outFS = null;
fileStream = new FileOutputStream("NamesAndAgesUpdated.txt");
outFS = new PrintWriter(fileStream);
for (String S:userInputs){
outFS.println(S);
}
outFS.flush();
fileStream.close();
outFS.close();
}
}

View File

@ -0,0 +1,5 @@
Lee 18
Lua 21
Mary Beth 19
Stu 33
-2

View File

@ -0,0 +1,58 @@
import java.util.ArrayList;
import java.util.Scanner;
/*
* Same as the lab str vs int, except the input comes from a file
* instead of the keyboard.
* Use NamesAndAges.txt for input
*/
public class main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String s = "";
ArrayList<String> userInputs = new ArrayList<String>();
String name;
int age;
//Get input
while(s != "-1"){
System.out.println("Enter a name and age. -1 to exit: ");
//Get name or exit
s = scnr.next();
if (s.equals("-1")){
System.out.println();
break;
}
else{
name = s;
}
//Get age
try{
age = scnr.nextInt() +1;
}
catch(Exception e){
System.out.println("Age must be an Integer\n" +
"Setting age to 0 ");
age = 0;
}
//Finish line
scnr.nextLine();
//Add name plus new age to arr
userInputs.add(name + " " + Integer.toString(age));
System.out.println();
}
for (String S:userInputs){
System.out.println(S);
}
}
}

View File

@ -0,0 +1,74 @@
import java.util.ArrayList;
import java.util.Scanner;
/*
* Take in single-word first names and ages
* end with -1
* program outputs list with age incremented
* program fails and throws exception if second input on a line is a string
*
*
* EX:
* Input:
* Lee 18
* Lua 21
* Mary Beth 19
* Stu 33
* -1
*
* Output:
* Lee 19
* Lua 22
* Mary 0
* Stu 34
*/
public class main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String s = "";
ArrayList<String> userInputs = new ArrayList<String>();
String name;
int age;
//Get input
while(s != "-1"){
System.out.println("Enter a name and age. -1 to exit: ");
//Get name or exit
s = scnr.next();
if (s.equals("-1")){
System.out.println();
break;
}
else{
name = s;
}
//Get age
try{
age = scnr.nextInt() +1;
}
catch(Exception e){
System.out.println("Age must be an Integer\n" +
"Setting age to 0 ");
age = 0;
}
//Finish line
scnr.nextLine();
//Add name plus new age to arr
userInputs.add(name + " " + Integer.toString(age));
System.out.println();
}
for (String S:userInputs){
System.out.println(S);
}
}
}