0% found this document useful (0 votes)
20K views8 pages

CS 116 Spring 2020 Lab #1: Due: Sunday, February 2nd, Midnight Points: 20

The document provides instructions for CS 116 Spring 2020 Lab #1. Students are asked to design classes for everyday objects, including attributes and methods, and to complete problems involving converting marathon times to seconds and calculating the percentage slower than the world record. The lab is due by February 2nd at midnight and should be submitted to Blackboard.

Uploaded by

Andrew Cordell
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20K views8 pages

CS 116 Spring 2020 Lab #1: Due: Sunday, February 2nd, Midnight Points: 20

The document provides instructions for CS 116 Spring 2020 Lab #1. Students are asked to design classes for everyday objects, including attributes and methods, and to complete problems involving converting marathon times to seconds and calculating the percentage slower than the world record. The lab is due by February 2nd at midnight and should be submitted to Blackboard.

Uploaded by

Andrew Cordell
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Andrew Cordell

Elizabeth Aquino

CS 116 Spring 2020 Lab #1


Due: Sunday, February 2nd, midnight
Points: 20

Instructions:
1. Use this document template to report your answers. Enter all lab partner names
at the top of first page.
2. You don’t need to finish your lab work during the corresponding lab session.
3. Name the complete document as follows:

LastName_FirstName_CS116_Lab1_Report.doc

4. Submit the final document to Blackboard Assignments section before the due
date. No late submissions will be accepted.
5. ALL lab partners need to submit a report, even if it is the same document.

Objectives:
1. (12 points) Design a class for an everyday object, including required data and
methods.
2. (8 points) Use arithmetic operators. Understand and apply precedence of
operations. Understand and apply explicit casting. Use methods of the Java
predefined String classes.

Problem 1 [12 points]:


Design a class for an everyday object, including required data and methods.

1
Define the private attributes (and their data types and valid ranges) and public
methods (and their arguments and return types) for the following classes. You do
not need to code, just design.

Populate provided tables (enter as many rows as you find necessary; add more if
needed) with your answers. Feel free to add extra tables, boxes, comments, etc. if
needed.

1. A fair 10-section game spinner (as shown below) [2 points].

Private attributes (use “N/A”, “undefined”, “none”, etc. If necessary)

Attribute name Data type Valid range of values Comments

pointerValue Int 0-9 Current value of the spinner (random)

Public methods (use “N/A”, “undefined”, “none”, etc. If necessary)

Method name Return Arguments Comments


data type

spin Int None Returns random value 0-9

spinX Int X Spins x number of times are return

2
2. A fraction data type [4 points].

Private attributes (use “N/A”, “undefined”, “none”, etc. If necessary)

Attribute name Data type Valid range of values Comments

Numerator Int All

Denominator Int All

Public methods (use “N/A”, “undefined”, “none”, etc. If necessary)

Method name Return Arguments Comments


data type

convertToString String Numerator and Returns numerator/denominator


denominator

convertToDeci Float Numerator and Returns the decimal approximate


mal denominator

3. A soda vending machine [6 points].

Private attributes (use “N/A”, “undefined”, “none”, etc. If necessary)

Attribute name Data type Valid range of values Comments

Price float 0-2 The price of any item

name String Any of the stocked item’s If not, reject


names

Stock int 0-100

3
cashStored Float 0-1000 Amount of cash in machine

cashIn Float 0-20 Amount of cash put in machine (max


$20)

Public methods (use “N/A”, “undefined”, “none”, etc. If necessary)

Method name Return Arguments Comments


data type

getPrice float Item name Returns the item’s price

getStock Int Item Name Returns the number of item stocked

setPrice Float ItemName Changes the item’s price

setCash Float Price Adds the items price to cash stored

getCash float cashIn-price Dispenses this amount of cash

Problem 2 [8 points]:
Use arithmetic operators. Understand and apply explicit casting. Use methods of the
java predefined String classes.

Marathon times are usually recorded in hours, minutes and seconds. Also, marathon
runners like to compare their time with the world record holder's time and see what
percentage slower they are than the world record time. To make the comparison
easier and to calculate the percentage slower, it helps to convert into seconds both a
person's time (entered as three integers: hours, minutes and seconds) and the world
record holder's time (2 hours, 3 minutes and 59 seconds). It is OK to display the
percentage in its decimal form.

person' s marathon t ime in seconds


percentage _ slower   1 .0
world record marathon t ime in seconds

Now, consider the following Java class:

Java Runner class


//Andrew Cordell
//Elizabeth Aquino

4
//Lab 1 due 2/2/2020

public class Runner {


private String name;
private int marathonTimeSeconds;
private static final int WORLD_RECORD_TIME_SECONDS = (2*60*60)+(3*60)+59;
private int h;
private int m;
private int s;

// add expression to convert the record hours and minutes and seconds to
seconds;
public Runner (String n, int h, int m, int s){
name = n;
setTime(h, m, s);
}

public Runner(String n, String data){


name = n;
h = Integer.parseInt(data.substring(0,1));
m = Integer.parseInt(data.substring(2,4));
s = Integer.parseInt(data.substring(5,7));
setTime(h,m,s);
}

public void setTime (int h, int m, int s){


if (h>=0 && m>=0 && s>=0)
marathonTimeSeconds = (h*60*60)+(m*60)+s;
// add expression to convert the h and m and s to seconds
}

public String getName()


{
return name;
}

public int getSeconds()


{
return marathonTimeSeconds;
}

public double percentageSlower (){


// ADD CODE HERE TO CALCULATE THE PERCENTAGE SLOWER THAN THE WORLD RECORD
double per = 0;
per = Math.abs(WORLD_RECORD_TIME_SECONDS-marathonTimeSeconds);
double error = per/WORLD_RECORD_TIME_SECONDS*100;
return error;
}
}

For the Runner class above:

 Complete the two expressions , and

5
 add two methods to this class:
 "percentageSlower", and

 a second constructor that takes two String arguments, name and data, and
parses the second colon-delimited String into integer hours, minutes,
seconds.

Test your Runner class by adding code to RunnerClient.java below

Java RunnerClient class


class RunnerClient {
public static void main(String[] args) {
// add code here to test more

Runner r1 = new Runner("Bikila", 2, 15,16);


System.out.println(r1.getName() + " Time in seconds: " +
r1.getSeconds() + " Percentage slower: " + r1.percentageSlower());

Runner r2 = new Runner("Radcliffe", "2:15:25");


System.out.println(r2.getName() + " Time in seconds: " +
r2.getSeconds() + " Percentage slower: " + r2.percentageSlower());

Runner r3 = new Runner("Gomez", 4, 29, 54);


System.out.println(r3.getName() + " Time in seconds: " +
r3.getSeconds() + " Percentage slower: " + r3.percentageSlower());

Runner r4 = new Runner("Cordell", 4, 30, 00);


System.out.println(r4.getName() + " Time in seconds: " +
r4.getSeconds() + " Percentage slower: " + r4.percentageSlower());
}
}

Now:
 Write pseudocode (place it in the box below) for solving the problem of
calculating the above described marathon statistic,

Pseudocode:

Method setTime:
public void setTime (h,m, s){
if (h>=0 && m>=0 && s>=0)

6
marathonTimeSeconds =(h*60*60)+(m*60)+s;
}
Method: percentageSlower
public double percentageSlower (){
double per = 0
per =Math.abs(worldRecordTime);
double error = per/ worldRecordTime *100;
return error;
}

 Step through your pseudocode to be sure that it works,


 Implement your pseudocode in Java. Be sure your program is appropriately
documented. Assume the user inputs the correct type of data,
 Compile and run your program to see if it runs (no run-time errors),
 Complete the following test plan (using a calculator),
 Test your program with the test plan below. If you discover mistakes in your
program, correct them and execute the test plan again. Your results may differ
slightly in the number of decimal digits displayed. In a later lab you will learn
how to format output to a specified number of digits.

Test plan
Test case Sample data Expected result Verified?
World record in Bikila Time in seconds: 8116 Yes
1960 Percentage slower: 9.10
Name: Bikila
Bikila
Hours: 2
2
Minutes: 15
15
Seconds: 16
16

7
Current women's Radcliffe Time in seconds: Yes
record 8125 Percentage slower:
9.22
Name: Radcliffe
Radcliffe
Hours: 2
2
Minutes: 15
15
Seconds: 25
25
My friend's time Gomez Gomez Time in seconds: Yes
16194 Percentage slower:
Name: Gomez 4 117.69054980508133

Hours: 4 29
Minutes: 29 54
Seconds: 54
Your own Cordell Cordell Time in seconds: Yes
16200 Percentage slower:
marathon time
4 117.77120580723215
(or estimate)
30
Name:
00
Hours:
Minutes:
Seconds:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy