CSE 110 - Lab 3
What this Lab Is About:
• Getting familiar with Control Statements (If, else if , else)
• Knowing about Random Class and getting random integer
• Knowing how to get string length
Problem Description: Finding Simple Interest and knowing if the user given string is even
length or odd length
For this Lab you have to calculate simple Interest based on the principle amount and time entered
by the user. However the rate of interest depends on Random number in the program which ranges
from 1 to 5.If the random number is less than 3 interest rate is 5 ,if it is between 3 and 5 interest rate
is 10 and if it more than 5 interest rate is 15.
Follow the steps mentioned below to know about Random class and random number
You are also required to take string input from the user and say if it is even length or odd length
Step 1: Getting Started Create a class called Lab3. Be sure to name your file Lab3.java.
Lab Documentation:
At the beginning of each programming assignment you must have a comment block with the
following information:
/*-------------------------------------------------------------------------
// AUTHOR: your name
// FILENAME: title of the source file
// SPECIFICATION: description of the program
// FOR: CSE 110- Lab #3
// TIME SPENT: how long it took you to complete the assignment //--------------------------------------
---------------------*/
Step 2: Declaring variables
Examining the problem, we see that we will need three inputs from the user.
Two of them are double type and one is of String data type. We will need variables to hold all of the
inputs. We can declare in the following way
double principal, time;
String str;
However we need variables to get interest rate and length to store the value of Simple Interest and
length of the string. Though these are not given by the user we have to declare variable so that they
hold values
int interestRate, len;
double si; (si stands for Simple Interest)
Step 3: Setting up Random object since you want to generate a random number so that the
rate of interest is known.
To create an object for Random class. We have to import Random class similar to Scanner class
how we did for Lab1
import java.util.Random
Create a Random object by using following syntax
Random <object name>=new Random(); /* give some name to the object which ever you
want */
Step4: Getting Random Interger
Random class has many datatypes like boolean, long, int etc. But we want Integer data type
Declare a variable num of int type.
int num;
num=<object name>.nextInt(5) /* <object name > is the name of the Random Object
*/
/* nextInt(5) ensures that the random number is between 0 to 5*/
Step5: Get the Intrest Rate based on the Random Integer Number
if the num is less than 3 then interestRate =5
if the num is greater or equal to 3 and less than 5 then interestRate=10
if num is greater than 5 interestRate =15
Step6: Calculate the Simple Intrest
si=(principal * time * Irate)/100;
Step7: Get the string from User and check if it is even length or odd length
len=str.lenght() /* <name>.length() gives the length of the string and stores the value in len
variable*/
if len is divisibe by 2 it is even length else odd length
Step8: Display the output
You should display the interest rate corresponding to Random number as well as Simple Interest
You should also display that weather the string entered is even length or odd length
Sample Output
Below is an example of what your output should roughly look like when this lab is completed.
All text in bold represents user input.
Sample Run 1:
Enter the amount: 1500
Enter the time: 5
Enter the string: John
The Interest Rate is 5.0 and Simple Interest is 375.0
You have entered even length string
Sample Run 2:
Enter the amount: 800
Enter the time: 2
Enter your name: Today is Sunday
The Interest Rate is 15.0 and Simple Interest is 240.0
You have entered odd length string
Last Step: Submit your lab by following the instructions below:
*******************************************************************************
Submit your Lab3.java file to the Submission Server. Go to the Submission Server site,
https://courses.eas.asu.edu/cse110/login , then click on Lab Submissions in the left frame. The
dropdown box will start in Lab3, so you don’t have to change it (you will for future labs). Click on
the browse button and find where you saved your Lab3.java file (and not the Lab1.class file) on
your computer. Upload the file to the site and then click on the Submit button.
Your file will be submitted and a screen will show up displaying if your program compiled and
what your output is when run on some sample input (in this case nothing).
You should then check to make sure that the actual file submitted properly and is readable to the
grader. To do so click on Grades in the frame on the left of the page and then click on the 0
underneath Lab3. You will again see that your program compiled and the sample output, but you
should scroll down to the bottom of the screen and make sure your file is readable as well.
Powered by TCPDF (www.tcpdf.org)