diff --git a/cpp/8.23-lab-swapping-variables/main.cpp b/cpp/8.23-lab-swapping-variables/main.cpp new file mode 100644 index 0000000..5804dd6 --- /dev/null +++ b/cpp/8.23-lab-swapping-variables/main.cpp @@ -0,0 +1,36 @@ +/* + * Write a program whose input is two integer and whose output is the two + * integers swapped + * Example + * Input: + * 3 8 + * output: + * 8 3 + * Must define and call a function + * void SwapValues(int& userVal1, int& userVal2) + */ + +#include +using namespace std; + +/* Define your function here */ + +void SwapValues(int& userVal1, int& userVal2){ + int temp; + temp = userVal1; + userVal1 = userVal2; + userVal2 = temp; + +} +int main() { + /* Type your code here. Your code must call the function. */ + int val1; + int val2; + + cin >> val1; + cin >> val2; + SwapValues(val1, val2); + cout << val1 << " " << val2 << endl; + + return 0; +} diff --git a/cpp/8.23-lab-swapping-variables/main.cpp~ b/cpp/8.23-lab-swapping-variables/main.cpp~ new file mode 100644 index 0000000..e69de29 diff --git a/cpp/8.8.1-function-pass-by-reference/main.cpp b/cpp/8.8.1-function-pass-by-reference/main.cpp new file mode 100644 index 0000000..7db9c99 --- /dev/null +++ b/cpp/8.8.1-function-pass-by-reference/main.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; + +/* Your solution goes here */ +/* Note how we put the reference bits in the function declaration + * instead of passing &var. + */ + +void CoordTransform(int& xVal, int& yVal, int& xValNew, int& yValNew){ + + xValNew = (xVal + 1) * 2; + yValNew = (yVal + 1) * 2; +} + +int main() { + int xValNew; + int yValNew; + int xValUser; + int yValUser; + + cin >> xValUser; + cin >> yValUser; + + CoordTransform(xValUser, yValUser, xValNew, yValNew); + cout << "(" << xValUser << ", " << yValUser << ") becomes (" << xValNew << ", " << yValNew << ")" << endl; + + return 0; +} diff --git a/cpp/8.8.1-function-pass-by-reference/main.cpp~ b/cpp/8.8.1-function-pass-by-reference/main.cpp~ new file mode 100644 index 0000000..16beb80 --- /dev/null +++ b/cpp/8.8.1-function-pass-by-reference/main.cpp~ @@ -0,0 +1,25 @@ +#include +using namespace std; + +/* Your solution goes here */ + +void CoordTransform(int& xVal, int& yVal, int& xValNew, int& yValNew){ + + xValNew = (xVal + 1) * 2; + yValNew = (yVal + 1) * 2; +} + +int main() { + int xValNew; + int yValNew; + int xValUser; + int yValUser; + + cin >> xValUser; + cin >> yValUser; + + CoordTransform(xValUser, yValUser, xValNew, yValNew); + cout << "(" << xValUser << ", " << yValUser << ") becomes (" << xValNew << ", " << yValNew << ")" << endl; + + return 0; +} diff --git a/cpp/8.9.2-Modify-a-string-arameter/main.cpp b/cpp/8.9.2-Modify-a-string-arameter/main.cpp new file mode 100644 index 0000000..ebbcbc6 --- /dev/null +++ b/cpp/8.9.2-Modify-a-string-arameter/main.cpp @@ -0,0 +1,25 @@ +#include +#include +using namespace std; + +void MakeSentenceExcited(string& sentenceText) { + + /* Your solution goes here */ + //Note that I had to use single quotes. + for (int i =0; i +#include +using namespace std; + +/* Your solution goes here */ + +void SwapVectorEnds(vector& v){ + int temp; + temp = v.at(0); + + v.at(0) = v.at(v.size()-1); + + v.at(v.size()-1) = temp; +} + +int main() { + vector sortVector(4); + unsigned int i; + int userNum; + + for (i = 0; i < sortVector.size(); ++i) { + cin >> userNum; + sortVector.at(i) = userNum; + } + + SwapVectorEnds(sortVector); + + for (i = 0; i < sortVector.size(); ++i) { + cout << sortVector.at(i) << " "; + } + cout << endl; + + return 0; +} diff --git a/java/week8/exception-output-file/main.java b/java/week8/exception-output-file/mainProgram.java similarity index 51% rename from java/week8/exception-output-file/main.java rename to java/week8/exception-output-file/mainProgram.java index 0bcf73b..a7d045b 100644 --- a/java/week8/exception-output-file/main.java +++ b/java/week8/exception-output-file/mainProgram.java @@ -9,9 +9,17 @@ import java.io.IOException; * instead of the screen. */ -public class main { +public class mainProgram { - public static ArrayList getInput(Scanner scnr){ + public static ArrayList readInput(final String fileName) + throws IOException{ + + FileInputStream fileByteStream = null; // File input stream + Scanner scnr = null; // Scanner object + + System.out.println("Opening file " + fileName); + fileByteStream = new FileInputStream(fileName); + scnr = new Scanner(fileByteStream); String s = ""; String name; @@ -51,44 +59,42 @@ public class main { System.out.println(); } + System.out.println("Closing scanner:"); + fileByteStream.close(); + scnr.close(); return results; } - public static void main(String[] args) throws IOException { - FileInputStream fileByteStream = null; // File input stream - Scanner inFS = null; // Scanner object + public static void writeArrayListToFile(ArrayList arr, + final String fileName) + throws IOException{ + FileOutputStream fileStream = null; + PrintWriter file = null; - ArrayList userInputs = new ArrayList(); + fileStream = new FileOutputStream(fileName); + file = new PrintWriter(fileStream); + + System.out.println("Writing out results " + + "To " + fileName ); + for (String S:arr){ + file.println(S); + } + file.flush(); + fileStream.close(); + file.close(); + } + + public static void main(String[] args) throws IOException{ + + final String INPUTFILENAME = "NamesAndAges.txt"; + final String OUTPUTFILENAME = "NamesAndAgesUpdated.txt"; + + ArrayList userInputs; //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(); + userInputs = readInput(INPUTFILENAME); //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(); + writeArrayListToFile(userInputs, OUTPUTFILENAME); } } - diff --git a/java/week8/exceptions-input-file/NamesAndAges.txt b/java/week8/exceptions-input-file/NamesAndAges.txt index bf9fc8d..2dcd7a9 100644 --- a/java/week8/exceptions-input-file/NamesAndAges.txt +++ b/java/week8/exceptions-input-file/NamesAndAges.txt @@ -2,4 +2,4 @@ Lee 18 Lua 21 Mary Beth 19 Stu 33 --2 +-1 diff --git a/java/week8/exceptions-input-file/main.java b/java/week8/exceptions-input-file/main.java index 31b7f6c..8ba9aa0 100644 --- a/java/week8/exceptions-input-file/main.java +++ b/java/week8/exceptions-input-file/main.java @@ -1,5 +1,7 @@ import java.util.ArrayList; import java.util.Scanner; +import java.io.FileInputStream; +import java.io.IOException; /* * Same as the lab str vs int, except the input comes from a file * instead of the keyboard. @@ -8,19 +10,26 @@ import java.util.Scanner; public class main { - public static void main(String[] args) { - Scanner scnr = new Scanner(System.in); + public static void main(String[] args) throws IOException { + FileInputStream fileByteStream = null; // File input stream + Scanner inFS = null; // Scanner object + String s = ""; ArrayList userInputs = new ArrayList(); String name; int age; //Get input - while(s != "-1"){ - System.out.println("Enter a name and age. -1 to exit: "); + System.out.println("Opening file NamesAndAges.txt."); + fileByteStream = new FileInputStream("NamesAndAges.txt"); + inFS = new Scanner(fileByteStream); + + System.out.println("Reading in file and processing"); + + while(true){ //Get name or exit - s = scnr.next(); + s = inFS.next(); if (s.equals("-1")){ System.out.println(); @@ -33,7 +42,7 @@ public class main { //Get age try{ - age = scnr.nextInt() +1; + age = inFS.nextInt() +1; } catch(Exception e){ System.out.println("Age must be an Integer\n" + @@ -42,7 +51,7 @@ public class main { } //Finish line - scnr.nextLine(); + inFS.nextLine(); //Add name plus new age to arr userInputs.add(name + " " + Integer.toString(age)); @@ -50,6 +59,10 @@ public class main { System.out.println(); } + System.out.println("Closing scanner and printing out results "); + fileByteStream.close(); + inFS.close(); + for (String S:userInputs){ System.out.println(S); }