Compare commits
2 Commits
9611d12b57
...
8f7143d6b4
Author | SHA1 | Date | |
---|---|---|---|
8f7143d6b4 | |||
aeb8c6c24f |
BIN
cpp/midterm/a.out
Executable file
BIN
cpp/midterm/a.out
Executable file
Binary file not shown.
39
cpp/midterm/main.cpp
Normal file
39
cpp/midterm/main.cpp
Normal 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;
|
||||
}
|
33
java/old/plant-information/Flower.java
Normal file
33
java/old/plant-information/Flower.java
Normal 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");
|
||||
}
|
||||
}
|
5
java/week8/exception-output-file/NamesAndAges.txt
Normal file
5
java/week8/exception-output-file/NamesAndAges.txt
Normal file
@ -0,0 +1,5 @@
|
||||
Lee 18
|
||||
Lua 21
|
||||
Mary Beth 19
|
||||
Stu 33
|
||||
-1
|
4
java/week8/exception-output-file/NamesAndAgesUpdated.txt
Normal file
4
java/week8/exception-output-file/NamesAndAgesUpdated.txt
Normal file
@ -0,0 +1,4 @@
|
||||
Lee 19
|
||||
Lua 22
|
||||
Mary 0
|
||||
Stu 34
|
94
java/week8/exception-output-file/main.java
Normal file
94
java/week8/exception-output-file/main.java
Normal 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();
|
||||
}
|
||||
}
|
||||
|
5
java/week8/exceptions-input-file/NamesAndAges.txt
Normal file
5
java/week8/exceptions-input-file/NamesAndAges.txt
Normal file
@ -0,0 +1,5 @@
|
||||
Lee 18
|
||||
Lua 21
|
||||
Mary Beth 19
|
||||
Stu 33
|
||||
-2
|
58
java/week8/exceptions-input-file/main.java
Normal file
58
java/week8/exceptions-input-file/main.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
74
java/week8/lab-string-vs-int/main.java
Normal file
74
java/week8/lab-string-vs-int/main.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user