You are on page 1of 3

School of Engineering and Technology

LAB #4
OBJECTIVES
After this lab, students have the chance to practice on the following:
-Using if, if-else, if-else-if statement
-How to write the constructor, toString method
-How to declare an object and use the object to access the method in data type class
-How to manage the output with a number of decimal digits by using DecimalFormat

PART 1 Answer the following questions on the Word document and save it with the
name lab4_YourLastName.docx
Write the Java statements of the following situations:
1. Write an if statement that assign 24 to x when y is equal to 5
2. Write an if-else statement that assign 15 to x when y is equal to 1, otherwise it
should assign 20 to x
3. Using the following chart, write an if-else-if statement that assigns .06, .10, or .
12 to commission rate, depending on the value in sales
Sales
Up to $25,000
$25,000 to $45,000
Over $45,000

Commission Rate
6%
10%
12%

4. Write nested if statement that performs the following tests: If amount1 is greater
than 10 and amount 2 is less than 20, display the greater of them.
5. Write an if statement that prints the message The number is valid if the
variable grade is within the range 0 through 100
6. Write an if statement that prints the message The number is valid if the
variable temperature is within the range -50 through 150
7. Write an if-else statement that displays the String object title1 and title2 in
alphabetical order
8. Convert the following if-else-if statement into a switch statement
if (choice == 1)
{
System.out.println(You selected 1.);
}
else if (choice == 2 || choice == 3)
{
System.out.println(You selected 2 or 3.);
}
else if (choice == 4)
{
System.out.println(You selected 4.);
}
else
{
System.out.println(Select again please.);

9. Match the conditional expression with the if-else statement that performs the
same operation
a). q = x < y ? a + b : x * 2;
b). q = x < y ? x * 2 : a + b;
c). x < y ? q = 0 : q = 1;

1.
if (x < y)
q = 0;
else
q = 1;

3.
if (x < y)
q = x * 2;
else
q = a + b;

2.
if (x < y)
q = a + b;;
else
q = x * 2;

10.Assume that the double variable number holds the value 0.04535. What format
pattern would you use with the DecimalFormat class to display the number as
0.04?
PART 2 (Write the Java program)
Create a project named FA2015_LAB4_PART2 then add two files named
Vendor_YourLastName (data type class) and FA2015_LAB4_SalaryOfVendor (driver
class).
The application allows users do the following tasks:
-Read the following information from the keyboard: vendorName (string), vendorID
(string), baseSalary (int), and saleAmount (int)
-The salary of vendor will be calculated by applying the following rules: The vendor
must have the sale at least $25000 to get the base salary. If the sale is greater than
$25000, the vendor can get 8% on extra sale for commission. Also if the total sale
of items is greater than $40000, the vendor can get extra bonus 5% more on top of
the commission for any sales beyond $40000. The rule is summarized as follows:
RULE
Vendor must sell $25000 worth of items
per month to get base salary
When Sale is greater than $25000
When Sale is greater than $40000

Commission
0

Bonus
0

8% on extra
sales
8% on extra
sales

0
5% on top of normal
commission for any
sales beyond $40000

-After calculation, the salary will be displayed on the screen in the following format:
.
Vendor Name: Smith Crystal
Vendor ID:
12345
Total sale:
$42000
Base Salary:
$2700
Commission:
$1360
Bonus:
$100
Total Amount made: $4160

HOW TO DO THE LAB 4 PART2


To do this lab, you should write two classes: a data type class named Vendor and a
controlling class named SalaryOfVendor.
Class Vendor_YourLastName-The data type class needs:
-data members to maintain vendorName (String), vendorID (int), baseSalary
(double) and saleAmount (double).

-no-argument constructor, parameter constructor


-all the methods that are needed to calculate the commission, bonus, total of salary,
etc.
-the method toString to build the requested format String to display the result
Class SalaryOfVendor_yourLastName the controlling class named needs:
-Define variables to store all the input information
-Display the message to ask users to provide all the information and read input from
the keyboard
-Create the object of Vendor_YourLastName and pass all the information needed
-display the result in the format as requested.
HOW TO GRADE THE LAB 4:
ITEM
PART1
PART2
class names: Vendor_YourLastName
Class Vendor_YourLastName with data members, all data members are
independent
No-argument Constructor parameter constructor
mutator, accessor methods
toString correct output format
All methods to calculate the total money the vendor can make if else if
statement correct
Comment in class Vendor_YourLastName
Class name: FA2015_LAB4_SalaryOfVendor_YourlasName
Define variables - Read input
Define the object of Vendor_YourLastName
Display the result by calling toString
Compile success with all requirements
Comment
LAB4 scores
HOW TO TURN IN THE LAB 4:
Submit on the eCampus the following source files:
1. lab4_YourLastName.docx (part1)
2. Vendor_YourLastName.java
3. FA2015_LAB4_SalaryOfVendor_YourLastName.java
4. Vendor_YourLastName.class
5. FA2015_LAB4_SalaryOfVendor_YourLastName.class

SCOR
ES
10
0.5
1
1
2
2
3
1
0.5
2
2
1
3
1
30

You might also like