Compare commits

...

2 Commits

Author SHA1 Message Date
9ef1dbc6c9 Update: Whatever 2021-03-27 15:33:25 -04:00
3ae4b43933 finish java;complete cpp 8.23 lab 2021-03-11 11:24:36 -05:00
64 changed files with 2199 additions and 101 deletions

View 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;
}

View 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_ */

View 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 */
}

View 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;
}

View 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;
}

View 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;
}

View 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;
}

View 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;
}

View File

View 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;
}

View 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;
}

View 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)
*/

View 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;
}

View 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

Binary file not shown.

View 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;
}

View 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

View 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

View 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

View 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

Binary file not shown.

View 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;
}

View 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;
}

View 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); }

View 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()

View 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

View 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

Binary file not shown.

View 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;
}
}

View 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);
}
}

View File

@ -2,4 +2,4 @@ Lee 18
Lua 21 Lua 21
Mary Beth 19 Mary Beth 19
Stu 33 Stu 33
-2 -1

View File

@ -1,28 +1,35 @@
import java.io.PrintWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Scanner; import java.util.Scanner;
import java.io.FileOutputStream;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
/* /*
* Same as the input one and the string vs int one, but now writting to a file * Same as the lab str vs int, except the input comes from a file
* instead of the screen. * instead of the keyboard.
* Use NamesAndAges.txt for input
*/ */
public class main { 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 = ""; String s = "";
ArrayList<String> userInputs = new ArrayList<String>();
String name; String name;
int age; 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){ while(true){
//Get name or exit //Get name or exit
s = scnr.next(); s = inFS.next();
if (s.equals("-1")){ if (s.equals("-1")){
System.out.println(); System.out.println();
@ -35,7 +42,7 @@ public class main {
//Get age //Get age
try{ try{
age = scnr.nextInt() +1; age = inFS.nextInt() +1;
} }
catch(Exception e){ catch(Exception e){
System.out.println("Age must be an Integer\n" + System.out.println("Age must be an Integer\n" +
@ -44,51 +51,21 @@ public class main {
} }
//Finish line //Finish line
scnr.nextLine(); inFS.nextLine();
//Add name plus new age to arr //Add name plus new age to arr
results.add(name + " " + Integer.toString(age)); userInputs.add(name + " " + Integer.toString(age));
System.out.println(); System.out.println();
} }
return results;
}
public static void main(String[] args) throws IOException { System.out.println("Closing scanner and printing out results ");
FileInputStream fileByteStream = null; // File input stream
Scanner inFS = null; // Scanner object
ArrayList<String> userInputs = new ArrayList<String>();
//Get input
System.out.println("Opening file NamesAndAges.txt.");
fileByteStream = new FileInputStream("NamesAndAges.txt");
inFS = new Scanner(fileByteStream);
System.out.println("Reading in file and processing");
userInputs = getInput(inFS);
System.out.println("Closing scanner and writing out results " +
"To NamesAndAgesUpdated.txt");
fileByteStream.close(); fileByteStream.close();
inFS.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){ for (String S:userInputs){
outFS.println(S); System.out.println(S);
} }
outFS.flush();
fileStream.close();
outFS.close();
} }
} }

View 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);
}
}
}

View 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();
}
}

View 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;
}
}

View 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
1 Date Open High Low Close
2 3/15/21 277.52 283.00 206.00 220.14
3 3/12/21 275.00 295.50 262.27 264.50
4 3/11/21 241.64 281.50 232.60 260.00
5 3/10/21 269.43 348.50 172.00 265.00
6 3/9/21 217.71 249.85 208.51 246.90
7 3/8/21 154.89 210.87 146.10 194.50
8 3/5/21 128.17 151.53 127.50 137.74
9 3/4/21 125.00 147.87 115.30 132.35
10 3/3/21 122.51 127.75 113.12 124.18
11 3/2/21 116.93 133.20 112.20 118.18
12 3/1/21 104.54 133.99 99.97 120.40
13 2/26/21 117.46 142.90 86.00 101.74
14 2/25/21 169.56 184.68 101.00 108.73
15 2/24/21 44.70 91.71 44.70 91.71
16 2/23/21 44.97 46.23 40.00 44.97
17 2/22/21 46.69 48.51 42.40 46.00
18 2/19/21 41.28 43.89 38.50 40.59
19 2/18/21 48.49 48.87 40.65 40.69
20 2/17/21 49.77 51.19 44.56 45.94
21 2/16/21 52.66 53.50 49.04 49.51
22 2/12/21 50.75 55.24 48.05 52.40
23 2/11/21 50.01 55.32 48.22 51.10
24 2/10/21 50.77 62.83 46.55 51.20
25 2/9/21 56.61 57.00 46.52 50.31
26 2/8/21 72.41 72.66 58.02 60.00
27 2/5/21 54.04 95.00 51.09 63.77
28 2/4/21 91.19 91.50 53.33 53.50
29 2/3/21 112.01 113.40 85.25 92.41
30 2/2/21 140.76 158.00 74.22 90.00
31 2/1/21 316.56 322.00 212.00 225.00
32 1/29/21 379.71 413.98 250.00 325.00
33 1/28/21 265.00 483.00 112.25 193.60
34 1/27/21 354.83 380.00 249.00 347.51
35 1/26/21 88.56 150.00 80.20 147.98
36 1/25/21 96.73 159.18 61.13 76.79
37 1/22/21 42.59 76.76 42.32 65.01
38 1/21/21 39.23 44.75 37.00 43.03
39 1/20/21 37.37 41.19 36.06 39.12
40 1/19/21 41.55 45.52 36.64 39.36
41 1/15/21 38.49 40.75 34.01 35.50
42 1/14/21 38.09 43.06 33.05 39.91
43 1/13/21 20.42 38.65 20.03 31.40
44 1/12/21 19.96 20.40 19.32 19.95
45 1/11/21 19.41 20.65 19.01 19.94
46 1/8/21 18.18 18.30 17.08 17.69
47 1/7/21 18.47 19.45 18.02 18.08
48 1/6/21 17.34 18.98 17.33 18.36
49 1/5/21 17.35 18.08 17.23 17.37
50 1/4/21 19.00 19.10 17.15 17.25
51 12/31/20 19.25 19.80 18.80 18.84
52 12/30/20 19.38 20.00 18.85 19.26
53 12/29/20 20.82 21.07 18.56 19.38
54 12/28/20 21.31 21.97 20.35 20.99
55 12/24/20 21.01 21.48 19.95 20.15
56 12/23/20 20.17 22.35 19.13 20.57
57 12/22/20 16.22 20.04 16.15 19.46
58 12/21/20 15.81 16.35 15.28 15.53
59 12/18/20 15.78 16.30 15.18 15.63
60 12/17/20 13.96 14.98 13.59 14.83
61 12/16/20 13.96 14.31 13.58 13.85
62 12/15/20 12.78 14.02 12.48 13.85

View 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);
}
}
}

View 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);
}
}
}
}

Binary file not shown.

View 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();
}
}
}
}

View 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);
}
}
}

View 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!");
}
}

View File

@ -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);
}
}
}

View File

@ -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!");
}
}

View File

@ -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 {
}

View File

@ -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;

View 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.

30
java/week10/Main.java Normal file
View 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();
}
}
}

View 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) {
}
}
}

View 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);
}
}
}

View 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

View 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

View 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

View 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){
}
}
}

View File

@ -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);
}
}
}