finish java;complete cpp 8.23 lab
This commit is contained in:
parent
8f7143d6b4
commit
3ae4b43933
36
cpp/8.23-lab-swapping-variables/main.cpp
Normal file
36
cpp/8.23-lab-swapping-variables/main.cpp
Normal file
@ -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 <iostream>
|
||||
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;
|
||||
}
|
0
cpp/8.23-lab-swapping-variables/main.cpp~
Normal file
0
cpp/8.23-lab-swapping-variables/main.cpp~
Normal file
28
cpp/8.8.1-function-pass-by-reference/main.cpp
Normal file
28
cpp/8.8.1-function-pass-by-reference/main.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
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;
|
||||
}
|
25
cpp/8.8.1-function-pass-by-reference/main.cpp~
Normal file
25
cpp/8.8.1-function-pass-by-reference/main.cpp~
Normal file
@ -0,0 +1,25 @@
|
||||
#include <iostream>
|
||||
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;
|
||||
}
|
25
cpp/8.9.2-Modify-a-string-arameter/main.cpp
Normal file
25
cpp/8.9.2-Modify-a-string-arameter/main.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
void MakeSentenceExcited(string& sentenceText) {
|
||||
|
||||
/* Your solution goes here */
|
||||
//Note that I had to use single quotes.
|
||||
for (int i =0; i<sentenceText.size();i++){
|
||||
if (sentenceText.at(i) == '.'){
|
||||
sentenceText.at(i) = '!';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
string testStr;
|
||||
|
||||
getline(cin, testStr);
|
||||
MakeSentenceExcited(testStr);
|
||||
cout << testStr;
|
||||
|
||||
return 0;
|
||||
}
|
34
cpp/8.9.3-swap-vector-ends/main.cpp
Normal file
34
cpp/8.9.3-swap-vector-ends/main.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
/* Your solution goes here */
|
||||
|
||||
void SwapVectorEnds(vector<int>& v){
|
||||
int temp;
|
||||
temp = v.at(0);
|
||||
|
||||
v.at(0) = v.at(v.size()-1);
|
||||
|
||||
v.at(v.size()-1) = temp;
|
||||
}
|
||||
|
||||
int main() {
|
||||
vector<int> 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;
|
||||
}
|
@ -9,9 +9,17 @@ import java.io.IOException;
|
||||
* instead of the screen.
|
||||
*/
|
||||
|
||||
public class main {
|
||||
public class mainProgram {
|
||||
|
||||
public static ArrayList<String> getInput(Scanner scnr){
|
||||
public static ArrayList<String> 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<String> arr,
|
||||
final String fileName)
|
||||
throws IOException{
|
||||
FileOutputStream fileStream = null;
|
||||
PrintWriter file = null;
|
||||
|
||||
ArrayList<String> userInputs = new ArrayList<String>();
|
||||
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<String> 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);
|
||||
}
|
||||
}
|
||||
|
@ -2,4 +2,4 @@ Lee 18
|
||||
Lua 21
|
||||
Mary Beth 19
|
||||
Stu 33
|
||||
-2
|
||||
-1
|
||||
|
@ -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<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: ");
|
||||
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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user