Browse Source

Finished stock prices and skijumper

master
school 7 years ago
parent
commit
d8646d71e7
  1. 49
      files/SkiJumper.java
  2. 19
      files/StockPrices.java

49
files/SkiJumper.java

@ -5,10 +5,49 @@
* Add code to this class by implementing the interfaces
* ITrainer and IAthlete such that the code in main()
* executes providing the output shown in Problem 3.
*/
*/
public class SkiJumper implements IAthlete, ITrainer
{
} // End class SkiJumper
private String name;
private double hours;
private int nJumps;
public SkiJumper(String name){
this.name=name;
this.hours=0;
this.nJumps=0;
}
public String getName(){
return this.name;
}
public int getNJumps(){
return this.nJumps;
}
public void jump(){
this.nJumps += 1;
}
@Override
public void data(){
System.out.println("Name: "+this.name);
System.out.println("Hours: "+this.hours);
System.out.println("Number of jumps: "+this.nJumps);
}
@Override
public void train(double h){
System.out.println("I am on slopes "+h+" hours today");
this.hours += h;
}
@Override
public boolean equals(Object other) {
boolean eq;
if (other == null){
return false;
}
else if (this.getClass() != other.getClass()){
return false;
}
else{
SkiJumper sj = (SkiJumper) other;
return this.nJumps == sj.getNJumps();
}
} // End class SkiJumper
}

19
files/StockPrices.java

@ -5,23 +5,30 @@ public class StockPrices
public static void main( String[] args )
{
double total = 0.0;
double current;
String line;
File portfolio_txt = new File("portfolio.txt");
Scanner inputFile = null;
// Use a loop in the try block to read each line of the file.
// Lecture Slides 13 (posted on D2L) provide a good example
try
{
inputFile = new Scanner(portfolio_txt);
while (inputFile.hasNextLine()) {
line = inputFile.nextLine();
String[] parts = line.split(" ");
current = Double.parseDouble(parts[1]);
total += current;
}
}
catch( )
catch(FileNotFoundException fnfe)
{
System.out.println("Can't find file");
// A FileNotFoundException needs to be handled here
}
finally
{
inputFile.close();
}
System.out.println( "The total value is: $" + total );

Loading…
Cancel
Save