Docsity

Exercises for Variables, Arithmetic, Expression, Output | CSCI 1301, Lab Reports of Computer Science

Toggle sidebar
bg1
CSCI 1301 – Lab #3
The purpose of this exercise is to apply your knowledge about variables, arithmetic
expressions, output statements and the Scanner class discussed during the lectures.
Exercise #1
Write a java program that contains a class called Earnings with a main method that
performs the following:
• Declare a variable of data type double called grossEarnings.
• Declare a constant of data type double called taxPercent whose value is 35.0.
• Declare a variable of data type double called tax.
• Assign the value 4 million (4000000.00) to grossEarnings.
• Compute the tax by multiplying the grossEarnings by the taxPercent and divide that
result by 100 and assign this value to the variable tax.
• Declare a variable of data type double called netEarnings.
• Compute the netEarnings by subtracting the tax from the grossEarnings.
• Display the grossEarnings, taxPercent, tax and netEarnings on the screen.
The output of your program should look similar this:
Gross Earnings: 4000000.0
Tax Percent: 35.0
Tax: 1400000.0
Net Earnings: 2600000.0
You should format the output text in columns using the '\t' escape sequence as discussed
during class. Compile and run your program.
Now, edit your program and change the value assigned to the variables grossEarnings
and taxPercent to 3500000.00 and 28.5 respectively. Compile and run your program
again.
Exercise #2
Modify the java program you wrote in exercise to ask the user for the gross earnings of
the company. To do this, modify your program as follows:
• At the very top of the file before the declaration of your class, add the line
import java.util.Scanner;
This statement tells the compiler that you will be using objects and methods from the
Scanner class.
pf2

Partial preview of the text

Download Exercises for Variables, Arithmetic, Expression, Output | CSCI 1301 and more Lab Reports Computer Science in PDF only on Docsity!

CSCI 1301 – Lab

The purpose of this exercise is to apply your knowledge about variables, arithmetic expressions, output statements and the Scanner class discussed during the lectures.

Exercise

Write a java program that contains a class called Earnings with a main method that performs the following:

  • Declare a variable of data type double called grossEarnings.
  • Declare a constant of data type double called taxPercent whose value is 35.0.
  • Declare a variable of data type double called tax.
  • Assign the value 4 million (4000000.00) to grossEarnings.
  • Compute the tax by multiplying the grossEarnings by the taxPercent and divide that result by 100 and assign this value to the variable tax.
  • Declare a variable of data type double called netEarnings.
  • Compute the netEarnings by subtracting the tax from the grossEarnings.
  • Display the grossEarnings, taxPercent, tax and netEarnings on the screen.

The output of your program should look similar this:

Gross Earnings: 4000000. Tax Percent: 35. Tax: 1400000. Net Earnings: 2600000.

You should format the output text in columns using the '\t' escape sequence as discussed during class. Compile and run your program.

Now, edit your program and change the value assigned to the variables grossEarnings and taxPercent to 3500000.00 and 28.5 respectively. Compile and run your program again.

Exercise

Modify the java program you wrote in exercise to ask the user for the gross earnings of the company. To do this, modify your program as follows:

  • At the very top of the file before the declaration of your class, add the line

import java.util.Scanner;

This statement tells the compiler that you will be using objects and methods from the Scanner class.

  • In the main method create a Scanner object called keyboard to read from System.in just after the declaration of the variables.
  • Add a print statement to prompt the user for the gross earnings of the company.
  • Afterward, add a read statement that reads in the gross earnings value of the company and assigns this value to the variable grossEarnings. The Scanner object keyboard should use the nextDouble method of the Scanner class to read such a value since grossEarnings is a double.
  • Check the example Listing 2.5 (pp. 87) of your textbook for more insights on how to request input from a user.

Compile and run your program. A run of your program should look like this:

Please enter the gross earnings: 560890. Gross Earnings: 560890. Tax Percent: 35. Tax: 196311. Net Earnings: 364578.

WebCT Submission

After you have completed both exercises in this lab, upload the file Earnings.java in WebCT and submit it to receive credit.