From d8646d71e7cb92f6df6b293ce3a48c90665cf8ff Mon Sep 17 00:00:00 2001 From: school Date: Tue, 4 Dec 2018 23:59:37 -0600 Subject: [PATCH] Finished stock prices and skijumper --- files/SkiJumper.java | 61 ++++++++++++++++++++++++++++++++++-------- files/StockPrices.java | 21 ++++++++++----- 2 files changed, 64 insertions(+), 18 deletions(-) diff --git a/files/SkiJumper.java b/files/SkiJumper.java index a523c04..090c2e0 100644 --- a/files/SkiJumper.java +++ b/files/SkiJumper.java @@ -1,14 +1,53 @@ /* SkiJumper.java - * CS207-2 Homework 7 Fall 2018 - * This class represents a Ski Jumper in code. - * - * 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. - */ + * CS207-2 Homework 7 Fall 2018 + * This class represents a Ski Jumper in code. + * + * 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 +} diff --git a/files/StockPrices.java b/files/StockPrices.java index 11a0922..29622fb 100644 --- a/files/StockPrices.java +++ b/files/StockPrices.java @@ -5,26 +5,33 @@ 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 ); - + } }