+ All Categories
Home > Documents > Lab8 - Texas Christian Universitycsfaculty.tcu.edu/10403/NLab8/Lab8.pdf · Lab8 will use a Java...

Lab8 - Texas Christian Universitycsfaculty.tcu.edu/10403/NLab8/Lab8.pdf · Lab8 will use a Java...

Date post: 30-May-2020
Category:
Upload: others
View: 8 times
Download: 0 times
Share this document with a friend
6
CoSc 10403 Lab # 8 (an array of a class) Note. THIS LAB IS A BONUS LAB FOR THOSE THAT MIGHT HAVE MISSED A PREVIOUS LAB, THEREFORE IT IS NOT REQUIRED. WE WILL CONSIDER ONLY THE BEST 7 OUT OF 8 LABS Due Date: Part I, Experiment – classtime, Tuesday, Dec 11 th , 2019. Part II, Program - by midnight, Tuesday, Dec 11 h , 2019. Part I is the Experiment component and will not be accepted late. Part II is the programming component. Part I. Experiment. (20%) (There is no Lab8Experiment on the web this time.) REMEMBER - ALL EXPERIMENTS MUST BE TYPED – NOT HANDWRITTEN!!!! You should first "hand-run" the code and then double check your accuracy and understanding by checking output using System.out.println. Answer each of the following questions by providing the output generated. If there is an error in the code that prevents it from compiling or executing to completion explain the error. Note: in order to execute these code segments – build a Lab8Experiment. Copy and paste each of the below segments of code in the constructor of the JFrame. 1. Give the output generated by the following code segment. If the code does not execute, explain why. int sum = 0; int i = 10; do { if (i % 2 == 0) { sum += i; System.out.println("i = "+i+" sum = "+sum); } i--; } while (i > 0);
Transcript
Page 1: Lab8 - Texas Christian Universitycsfaculty.tcu.edu/10403/NLab8/Lab8.pdf · Lab8 will use a Java one-dimensional array to construct a an array from collection of ( i.e. BMIUsers[]).

CoSc 10403

Lab # 8 (an array of a class)

Note. THIS LAB IS A BONUS LAB FOR THOSE THAT MIGHT HAVE MISSED A PREVIOUS LAB, THEREFORE IT IS NOT REQUIRED. WE WILL CONSIDER ONLY THE

BEST 7 OUT OF 8 LABS

Due Date:

Part I, Experiment – classtime, Tuesday, Dec 11th, 2019.

Part II, Program - by midnight, Tuesday, Dec 11h, 2019.

Part I is the Experiment component and will not be accepted late. Part II is the programming component.

Part I. Experiment. (20%) (There is no Lab8Experiment on the web this time.)

REMEMBER - ALL EXPERIMENTS MUST BE TYPED – NOT HANDWRITTEN!!!!

You should first "hand-run" the code and then double check your accuracy and understanding by checking output using System.out.println. Answer each of the following questions by providing the output generated. If there is an error in the code that prevents it from compiling or executing to completion explain the error.

Note: in order to execute these code segments – build a Lab8Experiment. Copy and paste each of the below segments of code in the constructor of the JFrame.

1. Give the output generated by the following code segment. If the code does not execute, explain why.

int sum = 0; int i = 10; do { if (i % 2 == 0) { sum += i; System.out.println("i = "+i+" sum = "+sum); } i--; } while (i > 0);

Page 2: Lab8 - Texas Christian Universitycsfaculty.tcu.edu/10403/NLab8/Lab8.pdf · Lab8 will use a Java one-dimensional array to construct a an array from collection of ( i.e. BMIUsers[]).

2. Give the output generated by the following code segment. If the code does not execute, explain why.

int sum = 0; for (int i = 1; i <= 3; i++) { int j; for (j = 1; j <= i; j++) { sum += i+j; System.out.println("i, j, & sum = "+i+" "+j+" "+sum); } }

3. a) How would execution change if a ";" was placed immediately after the boolean expression of the inner loop of the code given for question # 2? Give the output generated.

for (j = 1; j <= i; j++);

b) How would execution change if the declaration of j was removed from its current location and placed inside the for loop header and the modification in part 3 a) was made? Explain.

int sum = 0; for (int i = 1; i <= 3; i++) { for (int j = 1; j <= i; j++); { sum += i+j; System.out.println("i, j, & sum = "+i+" "+j+" "+sum); } }

4. a) Give the output generated by the following code segment.

double values [] = {62, 85, 55, 68, 45, 70}; int i = 0, count = 0; final int CUTOFF = 60; while (i <= values.length -1) { if(values[i] >= CUTOFF) count++; i++; } System.out.println("count = "+count);

b) Modify the code as shown and explain the output (or error). (You should save all your work first!)

double values [] = {62, 85, 55, 68, 45, 70}; int i = 0, count = 0; final int CUTOFF = 60;

Page 3: Lab8 - Texas Christian Universitycsfaculty.tcu.edu/10403/NLab8/Lab8.pdf · Lab8 will use a Java one-dimensional array to construct a an array from collection of ( i.e. BMIUsers[]).

while (i <= values.length -1) { if(values[i] >= CUTOFF) { count++; i++; } } System.out.println("count = "+count);

c) Modify the code as shown and explain the output (or error).

double values [] = {62, 85, 55, 68, 45, 70}; int i = 0, count = 0; final int CUTOFF = 60; while (i <= values.length) { if(values[i] >= CUTOFF) count++; i++; } System.out.println("count = "+count);

5. Give the output generated by the following code segment.

int [] num = new int [6]; JTextField tf [] = { new JTextField("62",3), new JTextField("80",3), new JTextField("55",3), new JTextField("69",3), new JTextField("81",3), new JTextField("46",3)}; int sum = 0; double avg; for (int i=0; i < tf.length; i++) { num[i] = Integer.parseInt(tf[i].getText().trim()); sum += num[i]; } avg = (double)sum/num.length; for (int i=0; i < tf.length; i++) if (num[i] > avg) System.out.println("num at position "+i+" is > "+avg); else System.out.println("num at position "+i+" is <= "+avg);

Part II (80%) – This is the final programming project for the semester. As with earlier projects, you will need to zip together all Lab8 java files before submitting your final program using D2L. Note that you will need to provide four classes in the zip file

Page 4: Lab8 - Texas Christian Universitycsfaculty.tcu.edu/10403/NLab8/Lab8.pdf · Lab8 will use a Java one-dimensional array to construct a an array from collection of ( i.e. BMIUsers[]).

{Lab3.java (the View), Lab8.java(the Controller), Lab8Model.java(the Model) and BMIUsers.java (the collection) }. The purpose of this assignment is to familiarize you with a Collection Class to implement a One-Dimensional Array.

In this next assignment you will provide new selection interactivity for the BMI Calculation. For this assignment, your Lab7 will be renamed to become your Lab8 – but will now require new code in order to implement new JButtons that allow to compute multiple BMI calculations. Lab8 will use a Java one-dimensional array to construct a an array from collection of ( i.e. BMIUsers[]). The array and will be used to store the various details for each BMI Calculation. You will need to add additional methods to Lab8 and your Lab8Model in order to implement these new features. Note: This new requirement will necessitate the creation of a completely new Java class named BMIUsers (see below for an outline of this class). public class BMIUsers { private String name; private String gender; private double bmiVal; private int feet; private int inches; private int weight; public BMIUsers(){} public String getName() { return name;} public void setName(String n) { name = n; } public String getGender(){ return gender;} public void setGender(String g) { gender = g; } public double getBMIVal(){ return bmiVal;} public void setBMIVal(double b) { bmiVal = b; } public int getFeet(){ return feet;} public void setFeet(int f) { feet = f; } public int getInches(){ return inches;} public void setInches(int i) { inches = i; } public int getWeight(){ return weight;} public void setWeight(int w) { weight = w; } } The declaration for this array and methods that work with it (storing values and recalling values) will reside in your Lab8Model. Your Lab8Model declaration will look like: public BMIUsers[] users = new BMIUsers[50]; // up to 50 users After the BMI computation is performed you will be able to store the information in an an array and display it using two new Buttons (Next User and Show Stats). Their functionality is as follows:

Page 5: Lab8 - Texas Christian Universitycsfaculty.tcu.edu/10403/NLab8/Lab8.pdf · Lab8 will use a Java one-dimensional array to construct a an array from collection of ( i.e. BMIUsers[]).

1) By selecting NextUser, the current computation is stored and a clear of the View is done.

2) By selecting Show Stats a new JFrame appears showing the details of stored BMI computations.

3) This new BMI Frame includes two buttons one for Sort and for Exit from the stats frame

4) By selecting the Sort button the BMI computations are sorted by name. Our implementation of the final JFrame is shown below. Feel free to experiment with it. HAPPY PROGRAMMING!!!! Remember -- submit ONLY these four .java ( files (again as a zip file) code using D2L

The View with one BMI computation

When the next user button is selected the information is store for this calculation and the View is cleared for the next calculation

After new computations are added, whenever the Show Stats Button is selected a new JFrame appears with the information stored

Page 6: Lab8 - Texas Christian Universitycsfaculty.tcu.edu/10403/NLab8/Lab8.pdf · Lab8 will use a Java one-dimensional array to construct a an array from collection of ( i.e. BMIUsers[]).

Recommended