diff --git a/cpp/10.7.1-lab-vending-machine/VendingMachine.cpp b/cpp/10.7.1-lab-vending-machine/VendingMachine.cpp new file mode 100644 index 0000000..630c5a1 --- /dev/null +++ b/cpp/10.7.1-lab-vending-machine/VendingMachine.cpp @@ -0,0 +1,28 @@ +#include +#include + +#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; +} diff --git a/cpp/10.7.1-lab-vending-machine/VendingMachine.h b/cpp/10.7.1-lab-vending-machine/VendingMachine.h new file mode 100644 index 0000000..671a338 --- /dev/null +++ b/cpp/10.7.1-lab-vending-machine/VendingMachine.h @@ -0,0 +1,20 @@ +#ifndef VENDINGMACHINE_H_ +#define VENDINGMACHINE_H_ + +#include +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_ */ diff --git a/cpp/10.7.1-lab-vending-machine/main.cpp b/cpp/10.7.1-lab-vending-machine/main.cpp new file mode 100644 index 0000000..8938d89 --- /dev/null +++ b/cpp/10.7.1-lab-vending-machine/main.cpp @@ -0,0 +1,19 @@ +#include + +#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 */ +} diff --git a/cpp/8.9.29-max-min-nums/main.cpp b/cpp/8.9.29-max-min-nums/main.cpp new file mode 100644 index 0000000..686230b --- /dev/null +++ b/cpp/8.9.29-max-min-nums/main.cpp @@ -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 +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; +} diff --git a/cpp/8.9.29-max-min-nums/main.cpp~ b/cpp/8.9.29-max-min-nums/main.cpp~ new file mode 100644 index 0000000..e69de29 diff --git a/cpp/8.9.33-lab-even-odd-values-in-a-vector/main.cpp b/cpp/8.9.33-lab-even-odd-values-in-a-vector/main.cpp new file mode 100644 index 0000000..11b9b84 --- /dev/null +++ b/cpp/8.9.33-lab-even-odd-values-in-a-vector/main.cpp @@ -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 myVec) + * bool IsVectorOdd(vector myVec) + */ + +#include +#include +using namespace std; + +/* Define your function here */ +bool IsVectorEven(vector myVec) { + for (int i=0;i myVec) { + for (int i=0;i> length; + + vector listOfNums; + + for (int i=0;i> 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; +} diff --git a/cpp/8.9.33-lab-even-odd-values-in-a-vector/main.cpp~ b/cpp/8.9.33-lab-even-odd-values-in-a-vector/main.cpp~ new file mode 100644 index 0000000..55fd6b5 --- /dev/null +++ b/cpp/8.9.33-lab-even-odd-values-in-a-vector/main.cpp~ @@ -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 myVec) + * bool IsVectorOdd(vector myVec) + */ diff --git a/cpp/9.6-lab-triangle-area-comparison-classes/Triangle.cpp b/cpp/9.6-lab-triangle-area-comparison-classes/Triangle.cpp new file mode 100644 index 0000000..13a7052 --- /dev/null +++ b/cpp/9.6-lab-triangle-area-comparison-classes/Triangle.cpp @@ -0,0 +1,25 @@ +#include "Triangle.h" +#include +#include +#include + +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; +} diff --git a/cpp/9.6-lab-triangle-area-comparison-classes/Triangle.h b/cpp/9.6-lab-triangle-area-comparison-classes/Triangle.h new file mode 100644 index 0000000..926c809 --- /dev/null +++ b/cpp/9.6-lab-triangle-area-comparison-classes/Triangle.h @@ -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 diff --git a/cpp/9.6-lab-triangle-area-comparison-classes/Triangle.o b/cpp/9.6-lab-triangle-area-comparison-classes/Triangle.o new file mode 100644 index 0000000..d36b1bb Binary files /dev/null and b/cpp/9.6-lab-triangle-area-comparison-classes/Triangle.o differ diff --git a/cpp/9.6-lab-triangle-area-comparison-classes/a.out b/cpp/9.6-lab-triangle-area-comparison-classes/a.out new file mode 100755 index 0000000..f83d28a Binary files /dev/null and b/cpp/9.6-lab-triangle-area-comparison-classes/a.out differ diff --git a/cpp/9.6-lab-triangle-area-comparison-classes/main.cpp b/cpp/9.6-lab-triangle-area-comparison-classes/main.cpp new file mode 100644 index 0000000..df61fe4 --- /dev/null +++ b/cpp/9.6-lab-triangle-area-comparison-classes/main.cpp @@ -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 +#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; +} diff --git a/cpp/9.6-lab-triangle-area-comparison-classes/main.cpp~ b/cpp/9.6-lab-triangle-area-comparison-classes/main.cpp~ new file mode 100644 index 0000000..e69de29 diff --git a/cpp/9.7-lab-car-value-classes/Car.cpp b/cpp/9.7-lab-car-value-classes/Car.cpp new file mode 100644 index 0000000..1f275ea --- /dev/null +++ b/cpp/9.7-lab-car-value-classes/Car.cpp @@ -0,0 +1,40 @@ +#include +#include +#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 diff --git a/cpp/9.7-lab-car-value-classes/Car.cpp~ b/cpp/9.7-lab-car-value-classes/Car.cpp~ new file mode 100644 index 0000000..98a8514 --- /dev/null +++ b/cpp/9.7-lab-car-value-classes/Car.cpp~ @@ -0,0 +1,28 @@ +#include +#include +#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 diff --git a/cpp/9.7-lab-car-value-classes/Car.h b/cpp/9.7-lab-car-value-classes/Car.h new file mode 100644 index 0000000..91ef8a9 --- /dev/null +++ b/cpp/9.7-lab-car-value-classes/Car.h @@ -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 diff --git a/cpp/9.7-lab-car-value-classes/Car.h~ b/cpp/9.7-lab-car-value-classes/Car.h~ new file mode 100644 index 0000000..c164b41 --- /dev/null +++ b/cpp/9.7-lab-car-value-classes/Car.h~ @@ -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 diff --git a/cpp/9.7-lab-car-value-classes/a.out b/cpp/9.7-lab-car-value-classes/a.out new file mode 100755 index 0000000..b39b473 Binary files /dev/null and b/cpp/9.7-lab-car-value-classes/a.out differ diff --git a/cpp/9.7-lab-car-value-classes/main.cpp b/cpp/9.7-lab-car-value-classes/main.cpp new file mode 100644 index 0000000..b17681f --- /dev/null +++ b/cpp/9.7-lab-car-value-classes/main.cpp @@ -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 +#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; +} diff --git a/cpp/9.7-lab-car-value-classes/main.cpp~ b/cpp/9.7-lab-car-value-classes/main.cpp~ new file mode 100644 index 0000000..40b0e1e --- /dev/null +++ b/cpp/9.7-lab-car-value-classes/main.cpp~ @@ -0,0 +1,22 @@ +#include +#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; +} diff --git a/cpp/9.8-lab-winning-team-classes/Team.cpp b/cpp/9.8-lab-winning-team-classes/Team.cpp new file mode 100644 index 0000000..3b6fb7a --- /dev/null +++ b/cpp/9.8-lab-winning-team-classes/Team.cpp @@ -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); } + diff --git a/cpp/9.8-lab-winning-team-classes/Team.cpp~ b/cpp/9.8-lab-winning-team-classes/Team.cpp~ new file mode 100644 index 0000000..014bd50 --- /dev/null +++ b/cpp/9.8-lab-winning-team-classes/Team.cpp~ @@ -0,0 +1,9 @@ +#include "Team.h" + +// TODO: Implement mutator functions - +// SetTeamName(), SetTeamWins(), SetTeamLosses() + +// TODO: Implement accessor functions - +// GetTeamName(), GetTeamWins(), GetTeamLosses() + +// TODO: Implement GetWinPercentage() diff --git a/cpp/9.8-lab-winning-team-classes/Team.h b/cpp/9.8-lab-winning-team-classes/Team.h new file mode 100644 index 0000000..e94a29f --- /dev/null +++ b/cpp/9.8-lab-winning-team-classes/Team.h @@ -0,0 +1,30 @@ +#ifndef TEAMH +#define TEAMH + +#include + +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 diff --git a/cpp/9.8-lab-winning-team-classes/Team.h~ b/cpp/9.8-lab-winning-team-classes/Team.h~ new file mode 100644 index 0000000..7b34d40 --- /dev/null +++ b/cpp/9.8-lab-winning-team-classes/Team.h~ @@ -0,0 +1,18 @@ +#ifndef TEAMH +#define TEAMH + +#include + +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 diff --git a/cpp/9.8-lab-winning-team-classes/a.out b/cpp/9.8-lab-winning-team-classes/a.out new file mode 100755 index 0000000..d0ba21e Binary files /dev/null and b/cpp/9.8-lab-winning-team-classes/a.out differ diff --git a/cpp/9.8-lab-winning-team-classes/main.cpp b/cpp/9.8-lab-winning-team-classes/main.cpp new file mode 100644 index 0000000..4bf3752 --- /dev/null +++ b/cpp/9.8-lab-winning-team-classes/main.cpp @@ -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 +#include +#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; + } +} diff --git a/cpp/9.8-lab-winning-team-classes/main.cpp~ b/cpp/9.8-lab-winning-team-classes/main.cpp~ new file mode 100644 index 0000000..e69de29 diff --git a/java/week8/exception-output-file/NamesAndAges.txt b/java/old/week8/exception-output-file/NamesAndAges.txt similarity index 100% rename from java/week8/exception-output-file/NamesAndAges.txt rename to java/old/week8/exception-output-file/NamesAndAges.txt diff --git a/java/week8/exception-output-file/NamesAndAgesUpdated.txt b/java/old/week8/exception-output-file/NamesAndAgesUpdated.txt similarity index 100% rename from java/week8/exception-output-file/NamesAndAgesUpdated.txt rename to java/old/week8/exception-output-file/NamesAndAgesUpdated.txt diff --git a/java/week8/exception-output-file/mainProgram.java b/java/old/week8/exception-output-file/mainProgram.java similarity index 100% rename from java/week8/exception-output-file/mainProgram.java rename to java/old/week8/exception-output-file/mainProgram.java diff --git a/java/week8/exceptions-input-file/NamesAndAges.txt b/java/old/week8/exceptions-input-file/NamesAndAges.txt similarity index 100% rename from java/week8/exceptions-input-file/NamesAndAges.txt rename to java/old/week8/exceptions-input-file/NamesAndAges.txt diff --git a/java/week8/exceptions-input-file/main.java b/java/old/week8/exceptions-input-file/main.java similarity index 100% rename from java/week8/exceptions-input-file/main.java rename to java/old/week8/exceptions-input-file/main.java diff --git a/java/week8/lab-string-vs-int/main.java b/java/old/week8/lab-string-vs-int/main.java similarity index 100% rename from java/week8/lab-string-vs-int/main.java rename to java/old/week8/lab-string-vs-int/main.java diff --git a/java/old/week9/VideoAssignment/StockDatabase.java b/java/old/week9/VideoAssignment/StockDatabase.java new file mode 100644 index 0000000..7eca152 --- /dev/null +++ b/java/old/week9/VideoAssignment/StockDatabase.java @@ -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 database; + + public StockDatabase() { + database = new ArrayList(); + } + + public ArrayList 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); + } + + } +} diff --git a/java/old/week9/VideoAssignment/StockDatabaseManager.java b/java/old/week9/VideoAssignment/StockDatabaseManager.java new file mode 100644 index 0000000..00724e0 --- /dev/null +++ b/java/old/week9/VideoAssignment/StockDatabaseManager.java @@ -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(); + } +} diff --git a/java/old/week9/VideoAssignment/StockEntry.java b/java/old/week9/VideoAssignment/StockEntry.java new file mode 100644 index 0000000..de036aa --- /dev/null +++ b/java/old/week9/VideoAssignment/StockEntry.java @@ -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; + } +} diff --git a/java/old/week9/VideoAssignment/stock_data.csv b/java/old/week9/VideoAssignment/stock_data.csv new file mode 100644 index 0000000..8cd848f --- /dev/null +++ b/java/old/week9/VideoAssignment/stock_data.csv @@ -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 \ No newline at end of file diff --git a/java/old/week9/assignment1/ClientDemo.java b/java/old/week9/assignment1/ClientDemo.java new file mode 100644 index 0000000..79020bb --- /dev/null +++ b/java/old/week9/assignment1/ClientDemo.java @@ -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); + } + } + +} \ No newline at end of file diff --git a/java/old/week9/assignment1/Main.java b/java/old/week9/assignment1/Main.java new file mode 100644 index 0000000..8ce118a --- /dev/null +++ b/java/old/week9/assignment1/Main.java @@ -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); + } + } + + + + } +} diff --git a/java/old/week9/assignment1/Networking.pps b/java/old/week9/assignment1/Networking.pps new file mode 100644 index 0000000..a551a8c Binary files /dev/null and b/java/old/week9/assignment1/Networking.pps differ diff --git a/java/old/week9/assignment1/ServerProgram.java b/java/old/week9/assignment1/ServerProgram.java new file mode 100644 index 0000000..fa843f0 --- /dev/null +++ b/java/old/week9/assignment1/ServerProgram.java @@ -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(); + } + } + } +} diff --git a/java/old/week9/assignment1/SimpleClientDemo.java b/java/old/week9/assignment1/SimpleClientDemo.java new file mode 100644 index 0000000..88ed0ed --- /dev/null +++ b/java/old/week9/assignment1/SimpleClientDemo.java @@ -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); + } + } + +} \ No newline at end of file diff --git a/java/old/week9/assignment1/SimpleServerProgram(1).java b/java/old/week9/assignment1/SimpleServerProgram(1).java new file mode 100644 index 0000000..d7ed668 --- /dev/null +++ b/java/old/week9/assignment1/SimpleServerProgram(1).java @@ -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!"); + } +} \ No newline at end of file diff --git a/java/old/week9/assignment1/calc-radius-circle-client/Client.java b/java/old/week9/assignment1/calc-radius-circle-client/Client.java new file mode 100644 index 0000000..3e8876a --- /dev/null +++ b/java/old/week9/assignment1/calc-radius-circle-client/Client.java @@ -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); + } + } +} + + + diff --git a/java/old/week9/assignment1/calc-radius-circle-client/Client.java~ b/java/old/week9/assignment1/calc-radius-circle-client/Client.java~ new file mode 100644 index 0000000..e69de29 diff --git a/java/old/week9/assignment1/calc-radius-circle-server/Server.java b/java/old/week9/assignment1/calc-radius-circle-server/Server.java new file mode 100644 index 0000000..7ea72dd --- /dev/null +++ b/java/old/week9/assignment1/calc-radius-circle-server/Server.java @@ -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!"); + } +} diff --git a/java/old/week9/assignment1/calc-radius-circle-server/Server.java~ b/java/old/week9/assignment1/calc-radius-circle-server/Server.java~ new file mode 100644 index 0000000..0f8a8fe --- /dev/null +++ b/java/old/week9/assignment1/calc-radius-circle-server/Server.java~ @@ -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 { +} diff --git a/java/old/week9/assignment1/calc-radius-circle-server/server.java~ b/java/old/week9/assignment1/calc-radius-circle-server/server.java~ new file mode 100644 index 0000000..6468121 --- /dev/null +++ b/java/old/week9/assignment1/calc-radius-circle-server/server.java~ @@ -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; diff --git a/java/old/week9/assignment1/instructions.txt b/java/old/week9/assignment1/instructions.txt new file mode 100644 index 0000000..5764b94 --- /dev/null +++ b/java/old/week9/assignment1/instructions.txt @@ -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. diff --git a/java/old/week9/assignment1/instructions.txt~ b/java/old/week9/assignment1/instructions.txt~ new file mode 100644 index 0000000..e69de29 diff --git a/java/week10/Main.java b/java/week10/Main.java new file mode 100644 index 0000000..7d90f23 --- /dev/null +++ b/java/week10/Main.java @@ -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(); + } + } +} diff --git a/java/week10/assignment/Main.java b/java/week10/assignment/Main.java new file mode 100644 index 0000000..0a4d31c --- /dev/null +++ b/java/week10/assignment/Main.java @@ -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) { + } + } +} diff --git a/java/week10/assignment/Main.java~ b/java/week10/assignment/Main.java~ new file mode 100644 index 0000000..56fb824 --- /dev/null +++ b/java/week10/assignment/Main.java~ @@ -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); + } + } +} diff --git a/java/week10/assignment/finished-assignment/File100.txt b/java/week10/assignment/finished-assignment/File100.txt new file mode 100644 index 0000000..190423f --- /dev/null +++ b/java/week10/assignment/finished-assignment/File100.txt @@ -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 diff --git a/java/week10/assignment/finished-assignment/FileA.txt b/java/week10/assignment/finished-assignment/FileA.txt new file mode 100644 index 0000000..9976cd0 --- /dev/null +++ b/java/week10/assignment/finished-assignment/FileA.txt @@ -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 diff --git a/java/week10/assignment/finished-assignment/FileB.txt b/java/week10/assignment/finished-assignment/FileB.txt new file mode 100644 index 0000000..e13658e --- /dev/null +++ b/java/week10/assignment/finished-assignment/FileB.txt @@ -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 diff --git a/java/week10/assignment/finished-assignment/Main.java b/java/week10/assignment/finished-assignment/Main.java new file mode 100644 index 0000000..791bda3 --- /dev/null +++ b/java/week10/assignment/finished-assignment/Main.java @@ -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){ + } + } +}