Compare commits
2 Commits
8f7143d6b4
...
9ef1dbc6c9
Author | SHA1 | Date | |
---|---|---|---|
9ef1dbc6c9 | |||
3ae4b43933 |
28
cpp/10.7.1-lab-vending-machine/VendingMachine.cpp
Normal file
28
cpp/10.7.1-lab-vending-machine/VendingMachine.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "VendingMachine.h"
|
||||
using namespace std;
|
||||
|
||||
|
||||
VendingMachine::VendingMachine() {
|
||||
bottles = 20;
|
||||
}
|
||||
|
||||
void VendingMachine::Purchase(int amount) {
|
||||
bottles = bottles - amount;
|
||||
}
|
||||
|
||||
int VendingMachine::GetInventory() {
|
||||
return bottles;
|
||||
}
|
||||
|
||||
|
||||
void VendingMachine::Restock(int amount) {
|
||||
bottles = bottles + amount;
|
||||
}
|
||||
|
||||
// set the random number generator seed for testing
|
||||
void VendingMachine::Report() {
|
||||
cout << "Inventory: " << bottles << " bottles" << endl;
|
||||
}
|
20
cpp/10.7.1-lab-vending-machine/VendingMachine.h
Normal file
20
cpp/10.7.1-lab-vending-machine/VendingMachine.h
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef VENDINGMACHINE_H_
|
||||
#define VENDINGMACHINE_H_
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class VendingMachine {
|
||||
|
||||
public:
|
||||
VendingMachine();
|
||||
void Purchase(int amount);
|
||||
int GetInventory();
|
||||
void Restock(int amount);
|
||||
void Report();
|
||||
|
||||
private:
|
||||
int bottles;
|
||||
};
|
||||
|
||||
#endif /* VENDINGMACHINE_H_ */
|
19
cpp/10.7.1-lab-vending-machine/main.cpp
Normal file
19
cpp/10.7.1-lab-vending-machine/main.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "VendingMachine.h"
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
VendingMachine vendor;
|
||||
int int1;
|
||||
int int2;
|
||||
cin >> int1;
|
||||
cin >> int2;
|
||||
|
||||
vendor.Purchase(int1);
|
||||
vendor.Restock(int2);
|
||||
vendor.Report();
|
||||
|
||||
|
||||
/* Type your code here */
|
||||
}
|
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;
|
||||
}
|
49
cpp/8.9.29-max-min-nums/main.cpp
Normal file
49
cpp/8.9.29-max-min-nums/main.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* input
|
||||
* 7 15 3
|
||||
* output
|
||||
* largest: 15
|
||||
* smallest: 3
|
||||
*
|
||||
* Use functions:
|
||||
* int LargestNumber(int num1, int num2, int num3)
|
||||
* int SmallestNumber(int num1, int num2, int num3)
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
/* Define your functions here */
|
||||
int LargestNumber(int num1, int num2, int num3){
|
||||
int max = num1;
|
||||
|
||||
if (num2 > max){
|
||||
max = num2;}
|
||||
if (num3 > max){
|
||||
max = num3;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
int SmallestNumber(int num1, int num2, int num3){
|
||||
int min = num1;
|
||||
|
||||
if (num2 < min){
|
||||
min = num2;}
|
||||
if (num3 < min){
|
||||
min = num3;
|
||||
}
|
||||
return min;
|
||||
}
|
||||
|
||||
int main() {
|
||||
/* Type your code here. Your code must call the functions. */
|
||||
int num1, num2, num3;
|
||||
cin >> num1;
|
||||
cin >> num2;
|
||||
cin >> num3;
|
||||
|
||||
cout << "largest: " << LargestNumber(num1, num2, num3) << endl;
|
||||
cout << "smallest: " << SmallestNumber(num1, num2, num3) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
0
cpp/8.9.29-max-min-nums/main.cpp~
Normal file
0
cpp/8.9.29-max-min-nums/main.cpp~
Normal file
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;
|
||||
}
|
80
cpp/8.9.33-lab-even-odd-values-in-a-vector/main.cpp
Normal file
80
cpp/8.9.33-lab-even-odd-values-in-a-vector/main.cpp
Normal file
@ -0,0 +1,80 @@
|
||||
/*
|
||||
* read list of ints
|
||||
* output whether list contains all even, all odd, or neither.
|
||||
* input begins with an int indicating the number of ints in the list
|
||||
*
|
||||
* input:
|
||||
* 5 2 4 6 8 10
|
||||
*
|
||||
* output:
|
||||
* all even
|
||||
*
|
||||
* input:
|
||||
* 5 1 -3 5 -7 9
|
||||
*
|
||||
* output:
|
||||
* all odd
|
||||
*
|
||||
* input:
|
||||
* 5 1 2 3 4 5
|
||||
*
|
||||
* output:
|
||||
* not even or odd
|
||||
*
|
||||
* Two functions
|
||||
*
|
||||
* bool IsVectorEven(vector<int> myVec)
|
||||
* bool IsVectorOdd(vector<int> myVec)
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
/* Define your function here */
|
||||
bool IsVectorEven(vector<int> myVec) {
|
||||
for (int i=0;i<myVec.size();i++){
|
||||
if (myVec.at(i) % 2 != 0){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IsVectorOdd(vector<int> myVec) {
|
||||
for (int i=0;i<myVec.size();i++){
|
||||
if (myVec.at(i) % 2 == 0){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
/* Type your code here. */
|
||||
|
||||
int length;
|
||||
int input;
|
||||
cin >> length;
|
||||
|
||||
vector<int> listOfNums;
|
||||
|
||||
for (int i=0;i<length;i++){
|
||||
cin >> input;
|
||||
listOfNums.push_back(input);
|
||||
}
|
||||
|
||||
if (IsVectorEven(listOfNums)){
|
||||
cout << "all even" << endl;
|
||||
}
|
||||
else if (IsVectorOdd(listOfNums)){
|
||||
cout << "all odd" << endl;
|
||||
}
|
||||
else{
|
||||
cout << "not even or odd" << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
28
cpp/8.9.33-lab-even-odd-values-in-a-vector/main.cpp~
Normal file
28
cpp/8.9.33-lab-even-odd-values-in-a-vector/main.cpp~
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* read list of ints
|
||||
* output whether list contains all even, all odd, or neither.
|
||||
* input begins with an int indicating the number of ints in the list
|
||||
*
|
||||
* input:
|
||||
* 5 2 4 6 8 10
|
||||
*
|
||||
* output:
|
||||
* all even
|
||||
*
|
||||
* input:
|
||||
* 5 1 -3 5 -7 9
|
||||
*
|
||||
* output:
|
||||
* all odd
|
||||
*
|
||||
* input:
|
||||
* 5 1 2 3 4 5
|
||||
*
|
||||
* output:
|
||||
* not even or odd
|
||||
*
|
||||
* Two functions
|
||||
*
|
||||
* bool IsVectorEven(vector<int> myVec)
|
||||
* bool IsVectorOdd(vector<int> myVec)
|
||||
*/
|
25
cpp/9.6-lab-triangle-area-comparison-classes/Triangle.cpp
Normal file
25
cpp/9.6-lab-triangle-area-comparison-classes/Triangle.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
#include "Triangle.h"
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <math.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void Triangle::SetBase(double userBase) {
|
||||
base = userBase;
|
||||
}
|
||||
|
||||
void Triangle::SetHeight(double userHeight) {
|
||||
height = userHeight;
|
||||
}
|
||||
|
||||
double Triangle::GetArea() const {
|
||||
return 0.5 * base * height;
|
||||
}
|
||||
|
||||
void Triangle::PrintInfo() const {
|
||||
cout << fixed << setprecision(2);
|
||||
cout << "Base: " << base << endl;
|
||||
cout << "Height: " << height << endl;
|
||||
cout << "Area: " << round(GetArea() * 100.0f) / 100.0f << endl;
|
||||
}
|
16
cpp/9.6-lab-triangle-area-comparison-classes/Triangle.h
Normal file
16
cpp/9.6-lab-triangle-area-comparison-classes/Triangle.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef TRIANGLEH
|
||||
#define TRIANGLEH
|
||||
|
||||
class Triangle {
|
||||
private:
|
||||
double base;
|
||||
double height;
|
||||
|
||||
public:
|
||||
void SetBase(double userBase);
|
||||
void SetHeight(double userHeight);
|
||||
double GetArea() const;
|
||||
void PrintInfo() const;
|
||||
};
|
||||
|
||||
#endif
|
BIN
cpp/9.6-lab-triangle-area-comparison-classes/Triangle.o
Normal file
BIN
cpp/9.6-lab-triangle-area-comparison-classes/Triangle.o
Normal file
Binary file not shown.
BIN
cpp/9.6-lab-triangle-area-comparison-classes/a.out
Executable file
BIN
cpp/9.6-lab-triangle-area-comparison-classes/a.out
Executable file
Binary file not shown.
54
cpp/9.6-lab-triangle-area-comparison-classes/main.cpp
Normal file
54
cpp/9.6-lab-triangle-area-comparison-classes/main.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
Example input:
|
||||
3.0 4.0
|
||||
4.0 5.0
|
||||
|
||||
Example output:
|
||||
|
||||
Triangle with larger area:
|
||||
Base: 4.00
|
||||
Height: 5.00
|
||||
Area: 10.00
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include "Triangle.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
Triangle triangle1;
|
||||
Triangle triangle2;
|
||||
|
||||
// TODO: Read and set base and height for triangle1 (use SetBase() and SetHeight())
|
||||
string userInput;
|
||||
|
||||
cin >> userInput;
|
||||
triangle1.SetBase(stod(userInput));
|
||||
cin >> userInput;
|
||||
triangle1.SetHeight(stod(userInput));
|
||||
|
||||
|
||||
// TODO: Read and set base and height for triangle2 (use SetBase() and SetHeight())
|
||||
|
||||
cin >> userInput;
|
||||
triangle2.SetBase(stod(userInput));
|
||||
cin >> userInput;
|
||||
triangle2.SetHeight(stod(userInput));
|
||||
|
||||
|
||||
// TODO: Determine larger triangle (use GetArea())
|
||||
cout << "Triangle with larger area:" << endl;
|
||||
|
||||
if (triangle1.GetArea() > triangle2.GetArea()){
|
||||
triangle1.PrintInfo();
|
||||
}
|
||||
else{
|
||||
triangle2.PrintInfo();
|
||||
}
|
||||
|
||||
|
||||
// TODO: Output larger triangle's info (use PrintInfo())
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
40
cpp/9.7-lab-car-value-classes/Car.cpp
Normal file
40
cpp/9.7-lab-car-value-classes/Car.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
#include "Car.h"
|
||||
using namespace std;
|
||||
|
||||
void Car::SetModelYear(int userYear){
|
||||
modelYear = userYear;
|
||||
}
|
||||
|
||||
int Car::GetModelYear() const {
|
||||
return modelYear;
|
||||
}
|
||||
|
||||
// TODO: Implement SetPurchasePrice() function
|
||||
void Car::SetPurchasePrice(int price){
|
||||
purchasePrice = price;
|
||||
}
|
||||
|
||||
// TODO: Implement GetPurchasePrice() function
|
||||
double Car::GetPurchasePrice(){
|
||||
return purchasePrice;
|
||||
}
|
||||
|
||||
void Car::CalcCurrentValue(int currentYear) {
|
||||
double depreciationRate = 0.15;
|
||||
int carAge = currentYear - modelYear;
|
||||
|
||||
// Car depreciation formula
|
||||
currentValue = (int)
|
||||
round(purchasePrice * pow((1 - depreciationRate), carAge));
|
||||
}
|
||||
|
||||
// TODO: Implement PrintInfo() function to output modelYear, purchasePrice, and
|
||||
void Car::PrintInfo() {
|
||||
cout << "Car's information:" << endl;
|
||||
cout << " Model year: " << modelYear << endl;
|
||||
cout << " Purchase price: " << purchasePrice << endl;
|
||||
cout << " Current value: " << currentValue << endl;
|
||||
}
|
||||
// currentValue
|
28
cpp/9.7-lab-car-value-classes/Car.cpp~
Normal file
28
cpp/9.7-lab-car-value-classes/Car.cpp~
Normal file
@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
#include "Car.h"
|
||||
using namespace std;
|
||||
|
||||
void Car::SetModelYear(int userYear){
|
||||
modelYear = userYear;
|
||||
}
|
||||
|
||||
int Car::GetModelYear() const {
|
||||
return modelYear;
|
||||
}
|
||||
|
||||
// TODO: Implement SetPurchasePrice() function
|
||||
|
||||
// TODO: Implement GetPurchasePrice() function
|
||||
|
||||
void Car::CalcCurrentValue(int currentYear) {
|
||||
double depreciationRate = 0.15;
|
||||
int carAge = currentYear - modelYear;
|
||||
|
||||
// Car depreciation formula
|
||||
currentValue = (int)
|
||||
round(purchasePrice * pow((1 - depreciationRate), carAge));
|
||||
}
|
||||
|
||||
// TODO: Implement PrintInfo() function to output modelYear, purchasePrice, and
|
||||
// currentValue
|
29
cpp/9.7-lab-car-value-classes/Car.h
Normal file
29
cpp/9.7-lab-car-value-classes/Car.h
Normal file
@ -0,0 +1,29 @@
|
||||
#ifndef CARH
|
||||
#define CARH
|
||||
|
||||
class Car {
|
||||
private:
|
||||
int modelYear;
|
||||
// TODO: Declare purchasePrice member (int)
|
||||
int purchasePrice;
|
||||
int currentValue;
|
||||
|
||||
public:
|
||||
void SetModelYear(int userYear);
|
||||
|
||||
int GetModelYear() const;
|
||||
|
||||
// TODO: Declare SetPurchasePrice() function
|
||||
void SetPurchasePrice(int price);
|
||||
|
||||
// TODO: Declare GetPurchasePrice() function
|
||||
double GetPurchasePrice();
|
||||
|
||||
void CalcCurrentValue(int currentYear);
|
||||
|
||||
// TODO: Define PrintInfo() method to output modelYear, purchasePrice, and
|
||||
// currentValue
|
||||
void PrintInfo();
|
||||
};
|
||||
|
||||
#endif
|
26
cpp/9.7-lab-car-value-classes/Car.h~
Normal file
26
cpp/9.7-lab-car-value-classes/Car.h~
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef CARH
|
||||
#define CARH
|
||||
|
||||
class Car {
|
||||
private:
|
||||
int modelYear;
|
||||
// TODO: Declare purchasePrice member (int)
|
||||
int currentValue;
|
||||
|
||||
public:
|
||||
void SetModelYear(int userYear);
|
||||
|
||||
int GetModelYear() const;
|
||||
|
||||
// TODO: Declare SetPurchasePrice() function
|
||||
|
||||
// TODO: Declare GetPurchasePrice() function
|
||||
|
||||
void CalcCurrentValue(int currentYear);
|
||||
|
||||
// TODO: Define PrintInfo() method to output modelYear, purchasePrice, and
|
||||
// currentValue
|
||||
|
||||
};
|
||||
|
||||
#endif
|
BIN
cpp/9.7-lab-car-value-classes/a.out
Executable file
BIN
cpp/9.7-lab-car-value-classes/a.out
Executable file
Binary file not shown.
40
cpp/9.7-lab-car-value-classes/main.cpp
Normal file
40
cpp/9.7-lab-car-value-classes/main.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
Given main(), complete the Car class (in files Car.h and Car.cpp) with member functions to set and get the purchase price of a car (SetPurchasePrice(), GetPurchasePrice()), and to output the car's information (PrintInfo()).
|
||||
|
||||
Ex: If the input is:
|
||||
|
||||
2011
|
||||
18000
|
||||
2018
|
||||
|
||||
where 2011 is the car's model year, 18000 is the purchase price, and 2018 is the current year, the output is:
|
||||
|
||||
Car's information:
|
||||
Model year: 2011
|
||||
Purchase price: 18000
|
||||
Current value: 5770
|
||||
|
||||
Note: printInfo() should use three spaces for indentation.
|
||||
*/
|
||||
#include <iostream>
|
||||
#include "Car.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
int userYear;
|
||||
int userPrice;
|
||||
int userCurrentYear;
|
||||
Car myCar;
|
||||
|
||||
cin >> userYear;
|
||||
cin >> userPrice;
|
||||
cin >> userCurrentYear;
|
||||
|
||||
myCar.SetModelYear(userYear);
|
||||
myCar.SetPurchasePrice(userPrice);
|
||||
myCar.CalcCurrentValue(userCurrentYear);
|
||||
|
||||
myCar.PrintInfo();
|
||||
|
||||
return 0;
|
||||
}
|
22
cpp/9.7-lab-car-value-classes/main.cpp~
Normal file
22
cpp/9.7-lab-car-value-classes/main.cpp~
Normal file
@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
#include "Car.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
int userYear;
|
||||
int userPrice;
|
||||
int userCurrentYear;
|
||||
Car myCar;
|
||||
|
||||
cin >> userYear;
|
||||
cin >> userPrice;
|
||||
cin >> userCurrentYear;
|
||||
|
||||
myCar.SetModelYear(userYear);
|
||||
myCar.SetPurchasePrice(userPrice);
|
||||
myCar.CalcCurrentValue(userCurrentYear);
|
||||
|
||||
myCar.PrintInfo();
|
||||
|
||||
return 0;
|
||||
}
|
22
cpp/9.8-lab-winning-team-classes/Team.cpp
Normal file
22
cpp/9.8-lab-winning-team-classes/Team.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
#include "Team.h"
|
||||
|
||||
// TODO: Implement mutator functions -
|
||||
// SetTeamName(), SetTeamWins(), SetTeamLosses()
|
||||
|
||||
void Team::SetTeamName(std::string name) { teamName = name; }
|
||||
void Team::SetTeamWins(int wins) { teamWins = wins; }
|
||||
void Team::SetTeamLosses(int losses) { teamLosses = losses;}
|
||||
|
||||
|
||||
|
||||
|
||||
// TODO: Implement accessor functions -
|
||||
// GetTeamName(), GetTeamWins(), GetTeamLosses()
|
||||
std::string Team::GetTeamName() { return teamName; }
|
||||
int Team::GetTeamWins() { return teamWins; }
|
||||
int Team::GetTeamLosses() { return teamLosses; }
|
||||
|
||||
// TODO: Implement GetWinPercentage()
|
||||
|
||||
double Team::GetWinPercentage() { return (double)teamWins / (teamWins + teamLosses); }
|
||||
|
9
cpp/9.8-lab-winning-team-classes/Team.cpp~
Normal file
9
cpp/9.8-lab-winning-team-classes/Team.cpp~
Normal file
@ -0,0 +1,9 @@
|
||||
#include "Team.h"
|
||||
|
||||
// TODO: Implement mutator functions -
|
||||
// SetTeamName(), SetTeamWins(), SetTeamLosses()
|
||||
|
||||
// TODO: Implement accessor functions -
|
||||
// GetTeamName(), GetTeamWins(), GetTeamLosses()
|
||||
|
||||
// TODO: Implement GetWinPercentage()
|
30
cpp/9.8-lab-winning-team-classes/Team.h
Normal file
30
cpp/9.8-lab-winning-team-classes/Team.h
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef TEAMH
|
||||
#define TEAMH
|
||||
|
||||
#include <string>
|
||||
|
||||
class Team {
|
||||
// TODO: Declare private data members - teamName, teamWins, teamLosses
|
||||
private:
|
||||
std::string teamName;
|
||||
int teamWins;
|
||||
int teamLosses;
|
||||
|
||||
public:
|
||||
// TODO: Declare mutator functions -
|
||||
// SetTeamName(), SetTeamWins(), SetTeamLosses()
|
||||
void SetTeamName(std::string name);
|
||||
void SetTeamWins(int wins);
|
||||
void SetTeamLosses(int losses);
|
||||
|
||||
// TODO: Declare accessor functions -
|
||||
// GetTeamName(), GetTeamWins(), GetTeamLosses()
|
||||
std::string GetTeamName();
|
||||
int GetTeamWins();
|
||||
int GetTeamLosses();
|
||||
|
||||
// TODO: Declare GetWinPercentage()
|
||||
double GetWinPercentage();
|
||||
};
|
||||
|
||||
#endif
|
18
cpp/9.8-lab-winning-team-classes/Team.h~
Normal file
18
cpp/9.8-lab-winning-team-classes/Team.h~
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef TEAMH
|
||||
#define TEAMH
|
||||
|
||||
#include <string>
|
||||
|
||||
class Team {
|
||||
// TODO: Declare private data members - teamName, teamWins, teamLosses
|
||||
|
||||
// TODO: Declare mutator functions -
|
||||
// SetTeamName(), SetTeamWins(), SetTeamLosses()
|
||||
|
||||
// TODO: Declare accessor functions -
|
||||
// GetTeamName(), GetTeamWins(), GetTeamLosses()
|
||||
|
||||
// TODO: Declare GetWinPercentage()
|
||||
};
|
||||
|
||||
#endif
|
BIN
cpp/9.8-lab-winning-team-classes/a.out
Executable file
BIN
cpp/9.8-lab-winning-team-classes/a.out
Executable file
Binary file not shown.
48
cpp/9.8-lab-winning-team-classes/main.cpp
Normal file
48
cpp/9.8-lab-winning-team-classes/main.cpp
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
Given main(), define the Team class (in files Team.h and Team.cpp). For class member function GetWinPercentage(), the formula is:
|
||||
teamWins / (teamWins + teamLosses)
|
||||
|
||||
Note: Use casting to prevent integer division.
|
||||
|
||||
Ex: If the input is:
|
||||
|
||||
Ravens
|
||||
13
|
||||
3
|
||||
|
||||
where Ravens is the team's name, 13 is number of team wins, and 3 is the number of team losses, the output is:
|
||||
|
||||
Congratulations, Team Ravens has a winning average!
|
||||
|
||||
If the input is Angels 80 82, the output is:
|
||||
|
||||
Team Angels has a losing average.
|
||||
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "Team.h"
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
string name;
|
||||
int wins;
|
||||
int losses;
|
||||
Team team;
|
||||
|
||||
cin >> name;
|
||||
cin >> wins;
|
||||
cin >> losses;
|
||||
|
||||
team.SetTeamName(name);
|
||||
team.SetTeamWins(wins);
|
||||
team.SetTeamLosses(losses);
|
||||
|
||||
if (team.GetWinPercentage() >= 0.5) {
|
||||
cout << "Congratulations, Team " << team.GetTeamName() <<
|
||||
" has a winning average!" << endl;
|
||||
}
|
||||
else {
|
||||
cout << "Team " << team.GetTeamName() << " has a losing average." << endl;
|
||||
}
|
||||
}
|
0
cpp/9.8-lab-winning-team-classes/main.cpp~
Normal file
0
cpp/9.8-lab-winning-team-classes/main.cpp~
Normal file
100
java/old/week8/exception-output-file/mainProgram.java
Normal file
100
java/old/week8/exception-output-file/mainProgram.java
Normal file
@ -0,0 +1,100 @@
|
||||
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 mainProgram {
|
||||
|
||||
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;
|
||||
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();
|
||||
}
|
||||
System.out.println("Closing scanner:");
|
||||
fileByteStream.close();
|
||||
scnr.close();
|
||||
return results;
|
||||
}
|
||||
|
||||
public static void writeArrayListToFile(ArrayList<String> arr,
|
||||
final String fileName)
|
||||
throws IOException{
|
||||
FileOutputStream fileStream = null;
|
||||
PrintWriter file = null;
|
||||
|
||||
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
|
||||
userInputs = readInput(INPUTFILENAME);
|
||||
|
||||
//Write to file
|
||||
writeArrayListToFile(userInputs, OUTPUTFILENAME);
|
||||
}
|
||||
}
|
@ -2,4 +2,4 @@ Lee 18
|
||||
Lua 21
|
||||
Mary Beth 19
|
||||
Stu 33
|
||||
-2
|
||||
-1
|
@ -1,28 +1,35 @@
|
||||
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.
|
||||
* 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 ArrayList<String> getInput(Scanner scnr){
|
||||
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;
|
||||
|
||||
ArrayList<String> results = 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");
|
||||
|
||||
while(true){
|
||||
|
||||
//Get name or exit
|
||||
s = scnr.next();
|
||||
s = inFS.next();
|
||||
|
||||
if (s.equals("-1")){
|
||||
System.out.println();
|
||||
@ -35,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" +
|
||||
@ -44,51 +51,21 @@ public class main {
|
||||
|
||||
}
|
||||
//Finish line
|
||||
scnr.nextLine();
|
||||
inFS.nextLine();
|
||||
|
||||
//Add name plus new age to arr
|
||||
results.add(name + " " + Integer.toString(age));
|
||||
userInputs.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");
|
||||
System.out.println("Closing scanner and printing out results ");
|
||||
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);
|
||||
System.out.println(S);
|
||||
}
|
||||
outFS.flush();
|
||||
fileStream.close();
|
||||
outFS.close();
|
||||
}
|
||||
}
|
||||
|
44
java/old/week9/VideoAssignment/StockDatabase.java
Normal file
44
java/old/week9/VideoAssignment/StockDatabase.java
Normal file
@ -0,0 +1,44 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class StockDatabase {
|
||||
private ArrayList<StockEntry> database;
|
||||
|
||||
public StockDatabase() {
|
||||
database = new ArrayList<StockEntry>();
|
||||
}
|
||||
|
||||
public ArrayList<StockEntry> getDatabase() {
|
||||
return database;
|
||||
}
|
||||
|
||||
public void readStockData(String filename){
|
||||
FileInputStream fileByteStream = null;
|
||||
Scanner scnr = null;
|
||||
try{
|
||||
//open file & set delimeters
|
||||
fileByteStream = new FileInputStream(filename);
|
||||
scnr = new Scanner(fileByteStream);
|
||||
scnr.useDelimiter("[,\r\n]+");
|
||||
scnr.nextLine();
|
||||
while(scnr.hasNext()){
|
||||
String date = scnr.next();
|
||||
double open = scnr.nextDouble();
|
||||
double high = scnr.nextDouble();
|
||||
double low = scnr.nextDouble();
|
||||
double close = scnr.nextDouble();
|
||||
StockEntry s = new StockEntry(
|
||||
date, open,
|
||||
high, low, close);
|
||||
database.add(s);
|
||||
}
|
||||
fileByteStream.close();
|
||||
}
|
||||
catch(IOException error1){
|
||||
System.out.println("Error with file " + filename);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
9
java/old/week9/VideoAssignment/StockDatabaseManager.java
Normal file
9
java/old/week9/VideoAssignment/StockDatabaseManager.java
Normal file
@ -0,0 +1,9 @@
|
||||
public class StockDatabaseManager {
|
||||
|
||||
public static void main (String [] args){
|
||||
|
||||
StockDatabase db = new StockDatabase();
|
||||
db.readStockData("data/stock_data.csv");
|
||||
System.out.println();
|
||||
}
|
||||
}
|
68
java/old/week9/VideoAssignment/StockEntry.java
Normal file
68
java/old/week9/VideoAssignment/StockEntry.java
Normal file
@ -0,0 +1,68 @@
|
||||
public class StockEntry {
|
||||
private String date;
|
||||
private double open;
|
||||
private double high;
|
||||
private double low;
|
||||
private double close;
|
||||
|
||||
public StockEntry(String date, double open,
|
||||
double high, double low,
|
||||
double close) {
|
||||
this.date = date;
|
||||
this.open = open;
|
||||
this.high = high;
|
||||
this.low = low;
|
||||
this.close = close;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "StockEntry{" +
|
||||
"date='" + date + '\'' +
|
||||
", open=" + open +
|
||||
", high=" + high +
|
||||
", low=" + low +
|
||||
", close=" + close +
|
||||
'}';
|
||||
}
|
||||
|
||||
public String getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(String date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public double getOpen() {
|
||||
return open;
|
||||
}
|
||||
|
||||
public void setOpen(double open) {
|
||||
this.open = open;
|
||||
}
|
||||
|
||||
public double getHigh() {
|
||||
return high;
|
||||
}
|
||||
|
||||
public void setHigh(double high) {
|
||||
this.high = high;
|
||||
}
|
||||
|
||||
public double getLow() {
|
||||
return low;
|
||||
}
|
||||
|
||||
public void setLow(double low) {
|
||||
this.low = low;
|
||||
}
|
||||
|
||||
public double getClose() {
|
||||
return close;
|
||||
}
|
||||
|
||||
public void setClose(double close) {
|
||||
this.close = close;
|
||||
}
|
||||
}
|
62
java/old/week9/VideoAssignment/stock_data.csv
Normal file
62
java/old/week9/VideoAssignment/stock_data.csv
Normal file
@ -0,0 +1,62 @@
|
||||
Date,Open,High,Low,Close
|
||||
3/15/21,277.52,283.00,206.00,220.14
|
||||
3/12/21,275.00,295.50,262.27,264.50
|
||||
3/11/21,241.64,281.50,232.60,260.00
|
||||
3/10/21,269.43,348.50,172.00,265.00
|
||||
3/9/21,217.71,249.85,208.51,246.90
|
||||
3/8/21,154.89,210.87,146.10,194.50
|
||||
3/5/21,128.17,151.53,127.50,137.74
|
||||
3/4/21,125.00,147.87,115.30,132.35
|
||||
3/3/21,122.51,127.75,113.12,124.18
|
||||
3/2/21,116.93,133.20,112.20,118.18
|
||||
3/1/21,104.54,133.99,99.97,120.40
|
||||
2/26/21,117.46,142.90,86.00,101.74
|
||||
2/25/21,169.56,184.68,101.00,108.73
|
||||
2/24/21,44.70,91.71,44.70,91.71
|
||||
2/23/21,44.97,46.23,40.00,44.97
|
||||
2/22/21,46.69,48.51,42.40,46.00
|
||||
2/19/21,41.28,43.89,38.50,40.59
|
||||
2/18/21,48.49,48.87,40.65,40.69
|
||||
2/17/21,49.77,51.19,44.56,45.94
|
||||
2/16/21,52.66,53.50,49.04,49.51
|
||||
2/12/21,50.75,55.24,48.05,52.40
|
||||
2/11/21,50.01,55.32,48.22,51.10
|
||||
2/10/21,50.77,62.83,46.55,51.20
|
||||
2/9/21,56.61,57.00,46.52,50.31
|
||||
2/8/21,72.41,72.66,58.02,60.00
|
||||
2/5/21,54.04,95.00,51.09,63.77
|
||||
2/4/21,91.19,91.50,53.33,53.50
|
||||
2/3/21,112.01,113.40,85.25,92.41
|
||||
2/2/21,140.76,158.00,74.22,90.00
|
||||
2/1/21,316.56,322.00,212.00,225.00
|
||||
1/29/21,379.71,413.98,250.00,325.00
|
||||
1/28/21,265.00,483.00,112.25,193.60
|
||||
1/27/21,354.83,380.00,249.00,347.51
|
||||
1/26/21,88.56,150.00,80.20,147.98
|
||||
1/25/21,96.73,159.18,61.13,76.79
|
||||
1/22/21,42.59,76.76,42.32,65.01
|
||||
1/21/21,39.23,44.75,37.00,43.03
|
||||
1/20/21,37.37,41.19,36.06,39.12
|
||||
1/19/21,41.55,45.52,36.64,39.36
|
||||
1/15/21,38.49,40.75,34.01,35.50
|
||||
1/14/21,38.09,43.06,33.05,39.91
|
||||
1/13/21,20.42,38.65,20.03,31.40
|
||||
1/12/21,19.96,20.40,19.32,19.95
|
||||
1/11/21,19.41,20.65,19.01,19.94
|
||||
1/8/21,18.18,18.30,17.08,17.69
|
||||
1/7/21,18.47,19.45,18.02,18.08
|
||||
1/6/21,17.34,18.98,17.33,18.36
|
||||
1/5/21,17.35,18.08,17.23,17.37
|
||||
1/4/21,19.00,19.10,17.15,17.25
|
||||
12/31/20,19.25,19.80,18.80,18.84
|
||||
12/30/20,19.38,20.00,18.85,19.26
|
||||
12/29/20,20.82,21.07,18.56,19.38
|
||||
12/28/20,21.31,21.97,20.35,20.99
|
||||
12/24/20,21.01,21.48,19.95,20.15
|
||||
12/23/20,20.17,22.35,19.13,20.57
|
||||
12/22/20,16.22,20.04,16.15,19.46
|
||||
12/21/20,15.81,16.35,15.28,15.53
|
||||
12/18/20,15.78,16.30,15.18,15.63
|
||||
12/17/20,13.96,14.98,13.59,14.83
|
||||
12/16/20,13.96,14.31,13.58,13.85
|
||||
12/15/20,12.78,14.02,12.48,13.85
|
|
79
java/old/week9/assignment1/ClientDemo.java
Normal file
79
java/old/week9/assignment1/ClientDemo.java
Normal file
@ -0,0 +1,79 @@
|
||||
package edu.grcc;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.Date;
|
||||
|
||||
public class ClientDemo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
|
||||
final String serverHost = "localhost";
|
||||
|
||||
Socket socketOfClient = null;
|
||||
BufferedWriter os = null;
|
||||
BufferedReader is = null;
|
||||
|
||||
try {
|
||||
|
||||
|
||||
// Send a request to connect to the server is listening
|
||||
// on machine 'localhost' port 7777.
|
||||
socketOfClient = new Socket(serverHost, 7777);
|
||||
|
||||
// Create output stream at the client (to send data to the server)
|
||||
os = new BufferedWriter(new OutputStreamWriter(socketOfClient.getOutputStream()));
|
||||
|
||||
|
||||
// Input stream at Client (Receive data from the server).
|
||||
is = new BufferedReader(new InputStreamReader(socketOfClient.getInputStream()));
|
||||
|
||||
} catch (UnknownHostException e) {
|
||||
System.err.println("Don't know about host " + serverHost);
|
||||
return;
|
||||
} catch (IOException e) {
|
||||
System.err.println("Couldn't get I/O for the connection to " + serverHost);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
// Write data to the output stream of the Client Socket.
|
||||
os.write("HELO! now is " + new Date());
|
||||
|
||||
// End of line
|
||||
os.newLine();
|
||||
|
||||
// Flush data.
|
||||
os.flush();
|
||||
os.write("I am Tom Cat");
|
||||
os.newLine();
|
||||
os.flush();
|
||||
os.write("QUIT");
|
||||
os.newLine();
|
||||
os.flush();
|
||||
|
||||
|
||||
|
||||
// Read data sent from the server.
|
||||
// By reading the input stream of the Client Socket.
|
||||
String responseLine;
|
||||
while ((responseLine = is.readLine()) != null) {
|
||||
System.out.println("Server: " + responseLine);
|
||||
if (responseLine.indexOf("OK") != -1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
os.close();
|
||||
is.close();
|
||||
socketOfClient.close();
|
||||
} catch (UnknownHostException e) {
|
||||
System.err.println("Trying to connect to unknown host: " + e);
|
||||
} catch (IOException e) {
|
||||
System.err.println("IOException: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
24
java/old/week9/assignment1/Main.java
Normal file
24
java/old/week9/assignment1/Main.java
Normal file
@ -0,0 +1,24 @@
|
||||
package edu.grcc;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
// write your code here
|
||||
try(Socket s = new Socket("time-a.nist.gov", 13);
|
||||
Scanner in = new Scanner(s.getInputStream(), "UTF-8")){
|
||||
|
||||
while(in.hasNextLine())
|
||||
{
|
||||
String line = in.nextLine();
|
||||
System.out.println(line);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
BIN
java/old/week9/assignment1/Networking.pps
Normal file
BIN
java/old/week9/assignment1/Networking.pps
Normal file
Binary file not shown.
99
java/old/week9/assignment1/ServerProgram.java
Normal file
99
java/old/week9/assignment1/ServerProgram.java
Normal file
@ -0,0 +1,99 @@
|
||||
package org.o7planning.tutorial.socketthread;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ServerProgram {
|
||||
|
||||
public static void main(String args[]) throws IOException {
|
||||
|
||||
ServerSocket listener = null;
|
||||
|
||||
System.out.println("Server is waiting to accept user...");
|
||||
int clientNumber = 0;
|
||||
|
||||
// Try to open a server socket on port 7777
|
||||
// Note that we can't choose a port less than 1023 if we are not
|
||||
// privileged users (root)
|
||||
|
||||
try {
|
||||
listener = new ServerSocket(7777);
|
||||
} catch (IOException e) {
|
||||
System.out.println(e);
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
// Accept client connection request
|
||||
// Get new Socket at Server.
|
||||
|
||||
Socket socketOfServer = listener.accept();
|
||||
new ServiceThread(socketOfServer, clientNumber++).start();
|
||||
}
|
||||
} finally {
|
||||
listener.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void log(String message) {
|
||||
System.out.println(message);
|
||||
}
|
||||
|
||||
private static class ServiceThread extends Thread {
|
||||
|
||||
private int clientNumber;
|
||||
private Socket socketOfServer;
|
||||
|
||||
public ServiceThread(Socket socketOfServer, int clientNumber) {
|
||||
this.clientNumber = clientNumber;
|
||||
this.socketOfServer = socketOfServer;
|
||||
|
||||
// Log
|
||||
log("New connection with client# " + this.clientNumber + " at " + socketOfServer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
try {
|
||||
|
||||
// Open input and output streams
|
||||
BufferedReader is = new BufferedReader(new InputStreamReader(socketOfServer.getInputStream()));
|
||||
BufferedWriter os = new BufferedWriter(new OutputStreamWriter(socketOfServer.getOutputStream()));
|
||||
|
||||
while (true) {
|
||||
// Read data to the server (sent from client).
|
||||
String line = is.readLine();
|
||||
|
||||
// Write to socket of Server
|
||||
// (Send to client)
|
||||
os.write(">> " + line);
|
||||
// End of line.
|
||||
os.newLine();
|
||||
// Flush data.
|
||||
os.flush();
|
||||
|
||||
|
||||
// If users send QUIT (To end conversation).
|
||||
if (line.equals("QUIT")) {
|
||||
os.write(">> OK");
|
||||
os.newLine();
|
||||
os.flush();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.out.println(e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
77
java/old/week9/assignment1/SimpleClientDemo.java
Normal file
77
java/old/week9/assignment1/SimpleClientDemo.java
Normal file
@ -0,0 +1,77 @@
|
||||
package edu.grcc;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
public class SimpleClientDemo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// Server Host
|
||||
final String serverHost = "localhost";
|
||||
|
||||
Socket socketOfClient = null;
|
||||
BufferedWriter os = null;
|
||||
BufferedReader is = null;
|
||||
|
||||
try {
|
||||
|
||||
// Send a request to connect to the server is listening
|
||||
// on machine 'localhost' port 9999.
|
||||
socketOfClient = new Socket(serverHost, 7777);
|
||||
|
||||
// Create output stream at the client (to send data to the server)
|
||||
os = new BufferedWriter(new OutputStreamWriter(socketOfClient.getOutputStream()));
|
||||
|
||||
|
||||
// Input stream at Client (Receive data from the server).
|
||||
is = new BufferedReader(new InputStreamReader(socketOfClient.getInputStream()));
|
||||
|
||||
} catch (UnknownHostException e) {
|
||||
System.err.println("Don't know about host " + serverHost);
|
||||
return;
|
||||
} catch (IOException e) {
|
||||
System.err.println("Couldn't get I/O for the connection to " + serverHost);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
// Write data to the output stream of the Client Socket.
|
||||
os.write("HELlO");
|
||||
|
||||
// End of line
|
||||
os.newLine();
|
||||
|
||||
// Flush data.
|
||||
os.flush();
|
||||
os.write("I am Tom Cat");
|
||||
os.newLine();
|
||||
os.flush();
|
||||
os.write("QUIT");
|
||||
os.newLine();
|
||||
os.flush();
|
||||
|
||||
|
||||
|
||||
// Read data sent from the server.
|
||||
// By reading the input stream of the Client Socket.
|
||||
String responseLine;
|
||||
while ((responseLine = is.readLine()) != null) {
|
||||
System.out.println("Server: " + responseLine);
|
||||
if (responseLine.indexOf("OK") != -1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
os.close();
|
||||
is.close();
|
||||
socketOfClient.close();
|
||||
} catch (UnknownHostException e) {
|
||||
System.err.println("Trying to connect to unknown host: " + e);
|
||||
} catch (IOException e) {
|
||||
System.err.println("IOException: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
74
java/old/week9/assignment1/SimpleServerProgram(1).java
Normal file
74
java/old/week9/assignment1/SimpleServerProgram(1).java
Normal file
@ -0,0 +1,74 @@
|
||||
package edu.grcc;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
public class SimpleServerProgram {
|
||||
|
||||
public static void main(String args[]) {
|
||||
|
||||
ServerSocket listener = null;
|
||||
String line;
|
||||
BufferedReader is;
|
||||
BufferedWriter os;
|
||||
Socket socketOfServer = null;
|
||||
|
||||
// Try to open a server socket on port 7777
|
||||
// Note that we can't choose a port less than 1023 if we are not
|
||||
// privileged users (root)
|
||||
|
||||
|
||||
try {
|
||||
listener = new ServerSocket(7777);
|
||||
} catch (IOException e) {
|
||||
System.out.println(e);
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
System.out.println("Server is waiting to accept user...");
|
||||
|
||||
// Accept client connection request
|
||||
// Get new Socket at Server.
|
||||
socketOfServer = listener.accept();
|
||||
System.out.println("Accept a client!");
|
||||
|
||||
// Open input and output streams
|
||||
is = new BufferedReader(new InputStreamReader(socketOfServer.getInputStream()));
|
||||
os = new BufferedWriter(new OutputStreamWriter(socketOfServer.getOutputStream()));
|
||||
|
||||
|
||||
while (true) {
|
||||
// Read data to the server (sent from client).
|
||||
line = is.readLine();
|
||||
|
||||
// Write to socket of Server
|
||||
// (Send to client)
|
||||
os.write(">> " + line);
|
||||
// End of line
|
||||
os.newLine();
|
||||
// Flush data.
|
||||
os.flush();
|
||||
|
||||
|
||||
// If users send QUIT (To end conversation).
|
||||
if (line.equals("QUIT")) {
|
||||
os.write(">> OK");
|
||||
os.newLine();
|
||||
os.flush();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.out.println(e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("Sever stopped!");
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
public class Client {
|
||||
public static void main(String[] args) {
|
||||
|
||||
// Server Host
|
||||
|
||||
final String serverHost = "localhost";
|
||||
|
||||
Socket socketOfClient = null;
|
||||
BufferedWriter os = null;
|
||||
BufferedReader is = null;
|
||||
|
||||
try {
|
||||
|
||||
//Send request to connect on 6666
|
||||
socketOfClient = new Socket(serverHost, 6666);
|
||||
|
||||
// Create ouput stream at client to send out data
|
||||
os = new BufferedWriter(new OutputStreamWriter(socketOfClient.getOutputStream()));
|
||||
is = new BufferedReader(new InputStreamReader(socketOfClient.getInputStream()));
|
||||
|
||||
} catch (UnknownHostException e) {
|
||||
System.err.println("Don't know about host " + serverHost);
|
||||
return;
|
||||
} catch (IOException e) {
|
||||
System.err.println("Couldn't get I/O for the connection to " + serverHost);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// write data to output client socket
|
||||
os.write("10");
|
||||
os.newLine();
|
||||
|
||||
os.write("20");
|
||||
os.newLine();
|
||||
|
||||
os.write("11");
|
||||
os.newLine();
|
||||
|
||||
os.write("75");
|
||||
os.newLine();
|
||||
|
||||
os.write("QUIT");
|
||||
os.newLine();
|
||||
|
||||
// Flush data.
|
||||
|
||||
os.flush();
|
||||
|
||||
// Read data from server
|
||||
// by reading client input socket
|
||||
String responseLine;
|
||||
while ((responseLine = is.readLine()) != null) {
|
||||
System.out.println("Server: " + responseLine);
|
||||
if (responseLine.indexOf("OK") != -1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
os.close();
|
||||
is.close();
|
||||
socketOfClient.close();
|
||||
} catch (UnknownHostException e) {
|
||||
System.err.println("Trying to connect to unknown host: " + e);
|
||||
} catch (IOException e) {
|
||||
System.err.println("IOException: " + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -0,0 +1,91 @@
|
||||
/*
|
||||
Create a Server.
|
||||
Server takes in a number from a client
|
||||
Calculates area of a circle with this number
|
||||
pi r^2
|
||||
*/
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
public class Server {
|
||||
public static void main(String args[]) {
|
||||
ServerSocket listener = null;
|
||||
String line;
|
||||
BufferedReader is;
|
||||
BufferedWriter os;
|
||||
Socket socketOfServer = null;
|
||||
|
||||
double dblRadius = 0;
|
||||
String strRadius = null;
|
||||
|
||||
// Try to open a port
|
||||
|
||||
try {
|
||||
listener = new ServerSocket(6666);
|
||||
} catch (IOException e) {
|
||||
System.out.println(e);
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
//Try to listen with port
|
||||
|
||||
try {
|
||||
System.out.println("Server is waiting to accept numbers...");
|
||||
|
||||
//Accept client requests
|
||||
|
||||
socketOfServer = listener.accept();
|
||||
System.out.println("Client Accepted!");
|
||||
|
||||
is = new BufferedReader
|
||||
(new InputStreamReader
|
||||
(socketOfServer.getInputStream()));
|
||||
os = new BufferedWriter
|
||||
(new OutputStreamWriter
|
||||
(socketOfServer.getOutputStream()));
|
||||
|
||||
while (true) {
|
||||
// Read data to the server
|
||||
line = is.readLine();
|
||||
|
||||
if (line.equals("QUIT")) {
|
||||
os.write(">> OK");
|
||||
os.newLine();
|
||||
os.flush();
|
||||
break;
|
||||
}
|
||||
|
||||
try{
|
||||
dblRadius = Double.parseDouble(line);
|
||||
dblRadius = Math.pow(dblRadius, 2);
|
||||
dblRadius = Math.PI * dblRadius;
|
||||
strRadius = Double.toString(dblRadius);
|
||||
|
||||
}catch (Exception e){
|
||||
os.write(">> " + line + " Was not'QUIT' or convertable to Double\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Write to socket of Server
|
||||
// send to client
|
||||
os.write(">> area of " + line + " is " + dblRadius);
|
||||
// End of line
|
||||
os.newLine();
|
||||
// Flush data
|
||||
os.flush();
|
||||
|
||||
//exit with quit message
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println(e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("Server stopped!");
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
/*
|
||||
Create a Server.
|
||||
Server takes in a number from a client
|
||||
Calculates area of a circle with this number
|
||||
pi r^2
|
||||
*/
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
public class Server {
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
/*
|
||||
Create a Server.
|
||||
Server takes in a number from a client
|
||||
Calculates area of a circle with this number
|
||||
pi r^2
|
||||
*/
|
||||
|
||||
import java.io.BufferedReader;
|
3
java/old/week9/assignment1/instructions.txt
Normal file
3
java/old/week9/assignment1/instructions.txt
Normal file
@ -0,0 +1,3 @@
|
||||
1. Create a Client
|
||||
2. Create a Server.
|
||||
3. Your client should be able to send data to the server. The server should receive the data, use it to produce a result, and finally send the result back to the client. The client prints the result on the screen. In this example, the data sent from the client is the radius of a circle, and the result produced by the server is the area of the circle.
|
0
java/old/week9/assignment1/instructions.txt~
Normal file
0
java/old/week9/assignment1/instructions.txt~
Normal file
30
java/week10/Main.java
Normal file
30
java/week10/Main.java
Normal file
@ -0,0 +1,30 @@
|
||||
|
||||
interface Animal{
|
||||
public void speak();
|
||||
}
|
||||
|
||||
class Cat implements Animal{
|
||||
@Override
|
||||
public void speak(){
|
||||
System.out.println("Meow Meow");
|
||||
}
|
||||
}
|
||||
|
||||
class Dog implements Animal{
|
||||
@Override
|
||||
public void speak(){
|
||||
System.out.println("Woof Woof");
|
||||
}
|
||||
}
|
||||
|
||||
public class Main{
|
||||
public static void main(String [] args){
|
||||
Animal [] animals = {
|
||||
new Dog(),
|
||||
new Cat()
|
||||
};
|
||||
for(Animal animal : animals){
|
||||
animal.speak();
|
||||
}
|
||||
}
|
||||
}
|
77
java/week10/assignment/Main.java
Normal file
77
java/week10/assignment/Main.java
Normal file
@ -0,0 +1,77 @@
|
||||
package edu.grcc;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// Create tasks
|
||||
Runnable printA = new PrintChar('a', 100);
|
||||
Runnable printB = new PrintChar('b', 100);
|
||||
Runnable print100 = new PrintNum(100);
|
||||
|
||||
//Create threads
|
||||
Thread thread1 = new Thread(printA);
|
||||
Thread thread2 = new Thread(printB);
|
||||
Thread thread3 = new Thread(print100);
|
||||
|
||||
// Start threads
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread3.start();
|
||||
}
|
||||
}
|
||||
|
||||
// The task for printing a specified character in specified times
|
||||
|
||||
class PrintChar implements Runnable {
|
||||
private char charToPrint; // the char to print
|
||||
private int times; // Times to repeat
|
||||
|
||||
/** Construct a task with specified character and number of
|
||||
* times to print the char
|
||||
*/
|
||||
public PrintChar(char c, int t) {
|
||||
charToPrint = c;
|
||||
times = t;
|
||||
}
|
||||
|
||||
@Override /** Override the run() method to tell the system
|
||||
* what the task to perform
|
||||
*/
|
||||
public void run() {
|
||||
for (int i = 0; i < times; i++) {
|
||||
System.out.print(charToPrint);
|
||||
Thread.yield();
|
||||
|
||||
try {
|
||||
if (i >= 50) Thread.sleep(100);
|
||||
}
|
||||
catch (InterruptedException ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The task class for printing number from 1 to n for a given n
|
||||
class PrintNum implements Runnable {
|
||||
private int lastNum;
|
||||
|
||||
// Construct a task for printing 1, 2, ... i
|
||||
public PrintNum(int n) {
|
||||
lastNum = n;
|
||||
}
|
||||
|
||||
@Override // Tell the thread how to run
|
||||
public void run() {
|
||||
Thread thread4 = new Thread(
|
||||
new PrintChar('c', 40));
|
||||
thread4.start();
|
||||
|
||||
try {
|
||||
for (int i = 1; i <= lastNum; i++) {
|
||||
System.out.print(" " + i);
|
||||
if (i == 50) thread4.join();
|
||||
}
|
||||
}
|
||||
catch (InterruptedException ex) {
|
||||
}
|
||||
}
|
||||
}
|
61
java/week10/assignment/Main.java~
Normal file
61
java/week10/assignment/Main.java~
Normal file
@ -0,0 +1,61 @@
|
||||
package edu.grcc;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// Create tasks
|
||||
Runnable printA = new PrintChar('a', 100);
|
||||
Runnable printB = new PrintChar('b', 100);
|
||||
Runnable print100 = new PrintNum(100);
|
||||
|
||||
//Create threads
|
||||
Thread thread1 = new Thread(printA);
|
||||
Thread thread2 = new Thread(printB);
|
||||
Thread thread3 = new Thread(print100);
|
||||
|
||||
// Start threads
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread3.start();
|
||||
}
|
||||
}
|
||||
|
||||
// The task for printing a specified character in specified times
|
||||
|
||||
class PrintChar implements Runnable {
|
||||
private char charToPrint; // the char to print
|
||||
private int times; // Times to repeat
|
||||
|
||||
/** Construct a task with specified character and number of
|
||||
* times to print the char
|
||||
*/
|
||||
public PrintChar(char c, int t) {
|
||||
charToPrint = c;
|
||||
times = t;
|
||||
}
|
||||
|
||||
@Override /** Override the run() method to tell the system
|
||||
* what the task to perform
|
||||
*/
|
||||
public void run() {
|
||||
for (int i = 0; i < times; i++) {
|
||||
System.out.print(charToPrint);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The task class for printing number from 1 to n for a given n
|
||||
class PrintNum implements Runnable {
|
||||
private int lastNum;
|
||||
|
||||
// Construct a task for printing 1, 2, ... i
|
||||
public PrintNum(int n) {
|
||||
lastNum = n;
|
||||
}
|
||||
|
||||
@Override // Tell the thread how to run
|
||||
public void run() {
|
||||
for (int i = 1; i <= lastNum; i++) {
|
||||
System.out.print(" " + i);
|
||||
}
|
||||
}
|
||||
}
|
100
java/week10/assignment/finished-assignment/File100.txt
Normal file
100
java/week10/assignment/finished-assignment/File100.txt
Normal file
@ -0,0 +1,100 @@
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
||||
10
|
||||
11
|
||||
12
|
||||
13
|
||||
14
|
||||
15
|
||||
16
|
||||
17
|
||||
18
|
||||
19
|
||||
20
|
||||
21
|
||||
22
|
||||
23
|
||||
24
|
||||
25
|
||||
26
|
||||
27
|
||||
28
|
||||
29
|
||||
30
|
||||
31
|
||||
32
|
||||
33
|
||||
34
|
||||
35
|
||||
36
|
||||
37
|
||||
38
|
||||
39
|
||||
40
|
||||
41
|
||||
42
|
||||
43
|
||||
44
|
||||
45
|
||||
46
|
||||
47
|
||||
48
|
||||
49
|
||||
50
|
||||
51
|
||||
52
|
||||
53
|
||||
54
|
||||
55
|
||||
56
|
||||
57
|
||||
58
|
||||
59
|
||||
60
|
||||
61
|
||||
62
|
||||
63
|
||||
64
|
||||
65
|
||||
66
|
||||
67
|
||||
68
|
||||
69
|
||||
70
|
||||
71
|
||||
72
|
||||
73
|
||||
74
|
||||
75
|
||||
76
|
||||
77
|
||||
78
|
||||
79
|
||||
80
|
||||
81
|
||||
82
|
||||
83
|
||||
84
|
||||
85
|
||||
86
|
||||
87
|
||||
88
|
||||
89
|
||||
90
|
||||
91
|
||||
92
|
||||
93
|
||||
94
|
||||
95
|
||||
96
|
||||
97
|
||||
98
|
||||
99
|
||||
100
|
100
java/week10/assignment/finished-assignment/FileA.txt
Normal file
100
java/week10/assignment/finished-assignment/FileA.txt
Normal file
@ -0,0 +1,100 @@
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
100
java/week10/assignment/finished-assignment/FileB.txt
Normal file
100
java/week10/assignment/finished-assignment/FileB.txt
Normal file
@ -0,0 +1,100 @@
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
||||
b
|
102
java/week10/assignment/finished-assignment/Main.java
Normal file
102
java/week10/assignment/finished-assignment/Main.java
Normal file
@ -0,0 +1,102 @@
|
||||
package edu.grcc;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// Create tasks
|
||||
// Only need one task. One that prints current line.
|
||||
Runnable printA = new PrintFile("FileA.txt");
|
||||
Runnable printB = new PrintFile("FileB.txt");
|
||||
Runnable print100 = new PrintFileNumbers("File100.txt");
|
||||
|
||||
//Create threads
|
||||
Thread thread1 = new Thread(printA);
|
||||
Thread thread2 = new Thread(printB);
|
||||
Thread thread3 = new Thread(print100);
|
||||
|
||||
// Start threads
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread3.start();
|
||||
}
|
||||
}
|
||||
|
||||
class PrintFile implements Runnable {
|
||||
|
||||
private String fileToPrint;
|
||||
|
||||
public PrintFile(String s) {
|
||||
fileToPrint = s;
|
||||
}
|
||||
|
||||
@Override /** Override the run() method to tell the system
|
||||
* what the task to perform
|
||||
*/
|
||||
public void run() {
|
||||
FileInputStream fileByteStream = null;
|
||||
Scanner inFS = null;
|
||||
|
||||
String s = "";
|
||||
|
||||
try{
|
||||
fileByteStream = new FileInputStream(fileToPrint);
|
||||
inFS = new Scanner(fileByteStream);
|
||||
|
||||
while (inFS.hasNextLine()){
|
||||
|
||||
s = inFS.nextLine();
|
||||
System.out.print(s);
|
||||
Thread.yield();
|
||||
}
|
||||
inFS.close();
|
||||
}
|
||||
catch(IOException e){
|
||||
}
|
||||
}
|
||||
}
|
||||
class PrintFileNumbers implements Runnable {
|
||||
private String fileToPrint;
|
||||
|
||||
public PrintFileNumbers(String s){
|
||||
fileToPrint = s;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
FileInputStream fileByteStream = null;
|
||||
Scanner inFS = null;
|
||||
|
||||
String s = "";
|
||||
|
||||
Thread thread4 = new Thread(
|
||||
new PrintFile("FileA.txt"));
|
||||
thread4.start();
|
||||
|
||||
try{
|
||||
fileByteStream = new FileInputStream(fileToPrint);
|
||||
inFS = new Scanner(fileByteStream);
|
||||
|
||||
while (inFS.hasNextLine()){
|
||||
|
||||
s = inFS.nextLine();
|
||||
System.out.print(s);
|
||||
|
||||
try{
|
||||
if (Integer.parseInt(s) >= 35) Thread.sleep(100);
|
||||
if (Integer.parseInt(s) == 50){
|
||||
System.out.println("\n Joining thread!");
|
||||
thread4.join();
|
||||
}
|
||||
}
|
||||
catch(Exception e){
|
||||
}
|
||||
|
||||
}
|
||||
inFS.close();
|
||||
}
|
||||
catch(IOException e){
|
||||
}
|
||||
}
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user