Objective: The goal of this assignment is to practice 2-dimensional ragged array
ID: 3882361 • Letter: O
Question
Objective: The goal of this assignment is to practice 2-dimensional ragged arrays. Background: Within a healthy, balanced diet, a grownup needs 2,250 calories a day. You will write a program to track calorie intake of a person. Assignment: Calorie intake data from a person is provided in a text file named input.txt. There are arbitrary number of double values on each line, separated by spaces. The numbers represent the number of calories consumed for meals and/or snacks on a day. The file includes data for exactly one week starting from Monday. That is, the file contains seven lines of text. The topmost line is for Monday and the line at the bottom is for Sunday. Your program should read the data from the file into a 2-dimensional array. The number of rows of the 2-dimensional array must be equal to the number of valid lines in the file. The numbers in the i-th row of the 2-dimensional array must appear in the same sequence of numbers in the i-th row of the file. After reading the input file into the 2-dimensional array, report the following items. a list of total calories consumed each day a list of days when more calories than required are consumed average calories consumed during the i-th meal/snack (average over all seven days) You must write a method with exactly one 2-dimensional array parameter to compute each of the listed items above and display the result. Example input file: 800 1000 100 450 100 845 20 1200 200 1800 250 400 0 1500 800 120 600 500 700 1400 1700 100 675 You should print an error message and terminate if there are not exactly 7 lines. Deliverables: The name of your source file must be Fitness.java.Explanation / Answer
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Workout extends JFrame {
public Workout() {
setLayout(new GridLayout(5, 3));
// Labels
add(new JLabel("Workout"));
add(new JLabel("Sets"));
add(new JLabel("Reps"));
}
public static void main(String[] args) throws Exception {
// Get current date information
Calendar cal = Calendar.getInstance();
// Set format to month/day/year hours:minutes
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy HH:mm a");
// Creates file object and sets pointer
File file = new File("Workout Log.txt"); // File name + path
// Ensures file exists and is not a directory
if (!file.exists() || file.isDirectory()) {
PrintWriter writer = new PrintWriter("Workout Log.txt", "UTF-8");
writer.close();
}
// Write to file without overwriting current contents
final PrintWriter output = new PrintWriter(new FileWriter(file, true));
output.println(" " + sdf.format(cal.getTime())); // Add date to file
// Create Frame
JFrame frame = new Workout();
frame.setTitle("My Workout Tracker");
/* Commented out for CR
frame.setIconImage(new ImageIcon("Images/Muscle.png").getImage()); */
// workout, set, and rep fields for data
final JTextField[] workouts = {
new JTextField("Push ups"),
new JTextField("Dumbell Presses"),
new JTextField("Dumbell Curls")
};
final JTextField[] sets = new JTextField[3];
final JTextField[] reps = new JTextField[3];
// add textfield pointers + add textfields to frame
for (int i = 0; i < 3; i++) {
frame.add(workouts[i]);
sets[i] = new JTextField();
frame.add(sets[i]);
reps[i] = new JTextField();
frame.add(reps[i]);
}
// Create checkboxes
final JCheckBox abs = new JCheckBox("Abs?");
final JCheckBox squat = new JCheckBox("Squats?");
// Create Listener
ActionListener checkListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == abs) {
if (abs.isSelected()) {
output.println("Ab Workout app used!");
}
}
else if (e.getSource() == squat) {
if (squat.isSelected()) {
output.println("Squat Workout app used!");
}
}
}
};
// Register listener with checkboxes
abs.addActionListener(checkListener);
squat.addActionListener(checkListener);
// Add checkboxes to frame
frame.add(abs);
frame.add(squat);
JButton log = new JButton("Log"); // To log everything
// Register + create Event Listener
log.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 3; i++) {
// Checks if fields are populated
if (sets[i].getText().length() > 0 && reps[i].getText().length() > 0) {
if (Integer.parseInt(sets[i].getText()) > 0 && Integer.parseInt(reps[i].getText()) > 0) {
output.println(
sets[i].getText() + " x " + reps[i].getText() +
" " + workouts[i].getText()
);
}
}
}
output.close();
try {
Thread.sleep(500);
}
catch (InterruptedException i) {
i.printStackTrace();
}
System.exit(0);
}
});
frame.add(log);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.