#6 Exercise 1 - File I/O (Statistics) In a class called Stats (template is provi
ID: 3771789 • Letter: #
Question
#6
Exercise 1 - File I/O
(Statistics) In a class called Stats (template is provided), given a text le of doubles (see this lab’s Canvas page for a sample le), write a program that determines the average, the maximum, the minimum and how many numbers are in the following buckets: less than 0, between 0 (inclusive) and 100 (exclusive), and greater than or equal to 100.
Your program should print the following message to another le called “leOut.txt”:
Statistics for the numbers in fileIn.txt: average: <average> max: <max> min: <min>
There are <negNum> negative numbers, <btw0and100> numbers between 0 (inclusive) and 100 (exclusive), and <geq100> numbers that are greater than or equal to 100.
(Optional Challenge - work on it after completing the rest of the lab!) Write a program that generates a text le that can be used as input for the problem described above. Use your knowledge of le output, loops and the random method from the Math library. How random and in what range the numbers should be it totally up to you!
Exercise 2 - More File I/O
In a class called Diary (template is provided), write a program that prompts users to enter the date as three integers separated by spaces (i.e mm dd yyyy) then it prompts them enter as many lines of prose they wish (for their to-do’s list or diary entry). Your program should store their entries at the end of a le called “diaryLog.txt” and old data should NOT be erased. Each entry should be formatted as follows (example for a diary entry entered on 7/14/2014):
...old stuff.
Date: 07/14/2014 Dear diary, today we had a midterm. I think it went ok, ...
Exercise 3 - Your rst class!
In this section we’ll build a small class together, useful for managing gift cards. This class will keep track of the total balance and expiration date on a gift card. Our gift card class will contain functionality that will allow for a starting total and date, deductions, a gift card status report, etc. To manage such details, we’ll need instance variables to store the balance, and the expiration month and year, as well as a few methods to implement the actions mentioned above. The GiftCard class should have the following data members and methods:
• Private instance variables: balance represented as a double, an expiration month represented as an int from 1-12 and and expiration year represented as an int from 2015-2030.
• Constructors: a no argument constructor with balance set to 0 and exp month and year to 12 and 2015 respectively; and a constructor that takes all data as arguments and sets them (i.e. the balance, month and year are taken as arguments).
• Getters: one for each instance variable. • Setters: one for each instance variable. • toString method: returns a String describing the balance and exp. date on the card as follows:
Balance: <balance>, exp. date: <MM/YYYY>
• Add a buyItem(double costOfItem) method that subtracts from the balance (if the card has enough money) the cost of the item.
• Write a GiftCardDriver class with a main method in order to test all your constructors and methods from GiftCard.
Notes: your methods should not allow for the balance to become negative, so that if a card does not have enough money to make a purchase it will simply be rejected. Also, the expiration month must be from 1-12 and the expiration year must be between 2015-2030. Purchases with an expired card should also be rejected.
Explanation / Answer
Example 1:
Main.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class project {
public static void main(String[] args) {
BufferedReader br = null;
Stats s = new Stats();
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\testing.txt"));
while ((sCurrentLine = br.readLine()) != null) {
s.addtoListofNumber(Double.parseDouble(sCurrentLine));
}
System.out.println("Avg: " + s.getAvg() + "Max: " + s.getMax() + "Min: " + s.getMin() + "less than 0:"
+ s.getLessthan0() + "bet 0 to 100" + s.getLessthan0() + "grt than 100" + s.getGrtthan100());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Stats.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Stats {
List<Double> listofNumber = new ArrayList<>();
private int btwn0to100 = 0;
public int getBtwn0to100() {
return btwn0to100;
}
public void setBtwn0to100(int btwn0to100) {
this.btwn0to100 = btwn0to100;
}
public int getLessthan0() {
return lessthan0;
}
public void setLessthan0(int lessthan0) {
this.lessthan0 = lessthan0;
}
public int getGrtthan100() {
return grtthan100;
}
public void setGrtthan100(int grtthan100) {
this.grtthan100 = grtthan100;
}
private int lessthan0 = 0;
private int grtthan100 = 0;
public List<Double> getListofNumber() {
return listofNumber;
}
public void addtoListofNumber(Double Number) {
listofNumber.add(Number);
if (Number < 0)
lessthan0++;
else if (Number > 100)
grtthan100++;
else
btwn0to100++;
}
public void gettoListofNumber(int index) {
listofNumber.get(index);
}
public double getMax() {
return Collections.max(listofNumber);
}
public double getMin() {
return Collections.min(listofNumber);
}
public double getAvg() {
double sum = 0;
for (Double double1 : listofNumber) {
sum = sum + double1;
}
return sum / (listofNumber.size());
}
}
Exercise 3:
GiftCard.java
public class GiftCard {
private double total;
private int expirationMonth;
private int expirationYear;
public GiftCard() {
this.total = 0;
this.expirationMonth = 12;
this.expirationYear = 2015;
}
public GiftCard(Double total, int month, int year) {
this.total = total;
this.expirationMonth = month;
this.expirationYear = year;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
public int getExpirationMonth() {
return expirationMonth;
}
public boolean setExpirationMonth(int expirationMonth) {
if (verifyMonth(expirationMonth)) {
this.expirationMonth = expirationMonth;
return true;
}
return false;
}
public int getExpirationYear() {
return expirationYear;
}
public boolean verifyYear(int year) {
if (year < 2015 || year > 2030)
return false;
return true;
}
public boolean verifyMonth(int month) {
if (month < 0 || month > 12)
return false;
return true;
}
public boolean setExpirationYear(int expirationYear) {
if (verifyYear(expirationYear)) {
this.expirationYear = expirationYear;
return true;
}
return false;
}
public boolean buyItem(double cost) {
if (total - cost < 0) {
return false;
}
total = total - cost;
return true;
}
@Override
public String toString() {
return "Balance:" + getTotal() + "Expiration: " + getExpirationMonth() + "/" + getExpirationYear();
}
}
Runner.java
public class Runner {
public static void main(String[] args) {
GiftCard g1 = new GiftCard();
g1.setTotal(5000);
g1.setExpirationMonth(2);
g1.setExpirationMonth(2019);
GiftCard g2 = new GiftCard(10000.00, 5, 2030);
g2.buyItem(200.00);
System.out.println(g1.toString());
System.out.println(g2.toString());
}
}
Example 2:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Runner {
public static void main(String[] args) {
try {
Scanner in = new Scanner(System.in);
System.out.println("Enter the date : ");
String date = in.nextLine();
System.out.println("Enter the comments : ");
String content = in.nextLine();
String data = " Date : " + date.split(" ")[0] + "/" + date.split(" ")[1] + "/" + date.split(" ")[2] + " "
+ content;
File file = new File("javaio-appendfile.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// true = append file
FileWriter fileWritter = new FileWriter(file.getName(), true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.write(data);
bufferWritter.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.