Exercise 1: CT044-3-1-IOOP Lab 8 - Writing Methods 1 of 2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

CT044-3-1-IOOP Lab 8 – Writing Methods Page 1 of 2

Exercise 1

A nutritionist who works for a fitness club helps members by evaluating their diets. As part of
her evaluation, she asks members for the number of fat grams and carbohydrate grams that they
consume in a day. Then, she calculates the number of calories that result from the fat using the
following formula:

Calories from fat = fat grams X 9

Next, she calculates the number of calories that result from the carbohydrates using the following
formula:
Calories from carbs = carbs grams X 4

Create an application that will make these calculations. In the application, you should have the
following methods:

 FatCalories-This method accept a number of fat grams as an argument and return the
number of calories from that amount of fat.

 CarbCalories-This method should accept a number of carbohydrate grams as an argument


and return the number of calories from that amount of carbohydrates.

Exercise 2

A prime number is a number that can be evenly divided by only itself and 1. For example, the
number 5 is a prime number because it can be evenly divided by only 1 and 5. The number 6,
however, is not prime because it can be evenly divided by 1, 2, 3 and 6. Write a Boolean
function named IsPrime that takes an integer as an argument and returns true if the argument is a
prime number or false otherwise. Use the function in an application that lets the user enter a
number and then displays a message indicating whether the number is prime.

Exercise 3

Create an application that calculates the total cost of a hospital stay. The daily base charge is
$350. The hospital also charges for medication, surgical fees, lab fees, and physical rehab. The
application should accept the following input:

 The number of days spent in the hospital


 The amount of medication charges
 The amount of surgical charges
 The amount of lab fees
 The amount of physical rehabilitation charges

Create and use the following value-returning methods in the application:

Level 1 Asia Pacific University of Technology and Innovation


CT044-3-1-IOOP Lab 8 – Writing Methods Page 2 of 2

 CalcStayCharges-Calculates and returns the base charges for the hospital stay. This is
computed as RM350 times the number of days in the hospital.
 CalcMiscCharges-Calculates and returns the total of the medication, surgical, lab, and
physical rehabilitation charges.
 CalcTotalCharges-Calculates and returns the total charges.

Level 1 Asia Pacific University of Technology and Innovation

You might also like