0% found this document useful (0 votes)
31 views5 pages

CSC 117 Lab 3 - Fall 2019 - Java Fundamentals (Chapter 1-3) Lab Objectives

This document outlines the objectives and exercises for Lab 3 of CSC 117. The objectives are to read and understand source code, identify syntax errors, logic errors, and runtime errors, use String class methods, apply conditionals, and format output. Exercise 1 involves debugging a payroll calculation program. Exercise 2 involves a string manipulation program. Exercise 3 involves writing a program to calculate stock purchase and sale amounts along with broker commissions. Students are required to submit the source code for exercises 1-3 by a deadline.

Uploaded by

Bj Roots
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)
31 views5 pages

CSC 117 Lab 3 - Fall 2019 - Java Fundamentals (Chapter 1-3) Lab Objectives

This document outlines the objectives and exercises for Lab 3 of CSC 117. The objectives are to read and understand source code, identify syntax errors, logic errors, and runtime errors, use String class methods, apply conditionals, and format output. Exercise 1 involves debugging a payroll calculation program. Exercise 2 involves a string manipulation program. Exercise 3 involves writing a program to calculate stock purchase and sale amounts along with broker commissions. Students are required to submit the source code for exercises 1-3 by a deadline.

Uploaded by

Bj Roots
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/ 5

CSC 117 Lab 3 – Fall 2019 –

Java Fundamentals (Chapter 1-3)


Lab Objectives
 Be able to read and understand source code.
 Be able to identify and fix syntax errors
 Be able to identify and fix logic errors
 Be able to identify and fix runtime errors
 Be able to debug a program
 Use the String class methods
 Apply conditionals
 Format output

Exercise 1:
A firm hires you to debug the following program.

1. This program calculates the user’s gross pay by multiplying the number of hours worked by
hourly pay rate. However, it is not always calculated this way. What if you work 45 hours in a
week? The hours that worked over 40 hours are considered overtime. The user will need to be
paid time and a half for the overtime hours worked. The program calculates user’s gross pay
with or without overtime.
a. If this program correctly implemented?
No because the calculations were wrong .
b. Did you find syntax error? Which one(s)?
No I didn’t find no syntax errors.
c. Did you identify logic error? Which one(s).
These were the two errors on this code which I corrected to
if(hours > 40) - if(hours < 40)
pay = (hours + 40) * (1.2 * rate) + 40 * rate- pay = (hours - 40) * (1.5 * rate) + 40 *
rate;

d. Recode the program without error and test it.


When I recode the program and tested it the program worked.
2. Using the corrected code, develop a set of test cases with different user input to calculate by
hand. Fill in the table below. Then compile the program and test it using with the user input
you’ve selected below. Verify the results match with what you expected.

Hours Pay per hour Earnings

20 10 200

40 10 400

60 10 600

80 10 800

import java.util.Scanner; //to be able to read from the keyboard

public class Pay {

public static void main(String [] args) {

//create a Scanner object to read from the keyboard


Scanner keyboard = new Scanner(System.in);

//identifier declarations
double hours; //number of hours worked
double rate; //hourly pay rate
double pay; //gross pay

//display prompts and get input


System.out.print("How many hours did you work? ");
hours = keyboard.nextDouble();
System.out.print("How much do you get paid per hour? ");
rate = keyboard.nextDouble();

//calculations
if(hours > 40)
pay = hours * rate;
else
pay = (hours + 40) * (1.2 * rate) + 40 * rate;

//display results
System.out.println("You earned $" + pay);
}
}
This is make code

import java.util.Scanner;

public class lab3 {


public static void main(String [] args) {

//create a Scanner object to read from the keyboard


Scanner keyboard = new Scanner(System.in);

//identifier declarations
double hours; //number of hours worked
double rate; //hourly pay rate
double pay; //gross pay

//display prompts and get input


System.out.print("How many hours did you work? ");
hours = keyboard.nextDouble();
System.out.print("How much do you get paid per hour? ");
rate = keyboard.nextDouble();

//calculations
if(hours <= 40) {
pay = hours * rate;
}
else
pay = (hours - 40) * (1.5 * rate) + 40 * rate;

//display results
System.out.println("You earned $" + pay);
}
}

Exercise 2: String Manipulator

Write a program that ask the user to enter the name of his favorite dish. Use a String variable(object) to
store the input. The program will display the following:

1. The number of characters in name of the dish- Apple Pie


2. The name of the dish in all upper case letters- APPLE PIE
3. The name of the dish in all lowercase letters-apple pie
4. The first character in the name of the dish- A
5. The last character in the name of dish- e
Exercise 3:

 Last month Kareem purchased some stock in Alphabet. The details of the purchase are given
below:
 The number of shares that Kareem purchased was 1,000
 Kareem purchased stock with the share priced at $32.87 each
 Kareem paid the stock broker a commission equivalent to 2% of the amount paid for the stock

Two weeks later Kareen sold the stock. The details of the sale are provided below:

 The number of shares that Kareem sold was 1,000


 Kareem purchased stock with the share priced at $33.92 value each
 Kareem paid the stock broker a commission equivalent to 2% of the amount paid for the stock
 Kareem paid the stock broker another commission equivalent to 2% of the amount he received
for the stock

Write a program that displays the following:


The amount of money Kareen paid for the stock

The amount of commission Kareem paid his broker when he bought the stock

The amount of that Kareem sold his stock

The amount of commission Kareem paid his broker when he sold the stock

Print the amount of profit/loss that Kareem made after selling his stock and paying the two commissions
to his broker.

Deliverables by 9/5

 Source code with name of students.


 Add comments to your programs (10 points)
 Please upload solutions for exercises 1-3 on Blackboard (30 points for each exercise). Be sure to
write your name and ID number.

You might also like