Wj 1102a Module1 Exercise2

  • Uploaded by: Rajani Ramsagar
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Wj 1102a Module1 Exercise2 as PDF for free.

More details

  • Words: 459
  • Pages: 2
Exercise 2: Using Operators and Type Casting The objective of this exercise is to give you practice using operators and type casting.

Task 1 – Calculating Ages Using Operators In this task, you use operators to calculate age in minutes and milliseconds. Follow these steps to create a Person class that calculates various ages: 1. Go to the data_types directory. 2. Enter the Person class from this module. Note – A copy of this code is provided at the end of these instructions. 3. Add the following code to your Person class: a. Add a line to calculate and print your age in minutes. b. Add a line to calculate and print your age in milliseconds (1/1000 of a second). 4. Test the program using the PersonTest class with the ages of 1, 24, and 100.

Task 2 – Using Casting to Prevent Data Loss

In this task, you use casting to ensure that data loss does not occur in your programs. Follow these steps to create an Order class: 5. Write a class called Order that contains two variables of type int called int1 and int2 and a method called calculateTotal. 6. Write a calculateTotal method that multiplies the two integer values and prints the result. 7. Test the program using the OrderTest class. Test the program with: a. One-digit int types b. Five-digit int types c. Nine-digit int types Ensure that you get the same result with the program as you do when doing each calculation manually (or using a calculator). Note – Remember to cast the two integers so that the temporary container is large enough to hold the result of multiplying two 5-digit or 9-digit int types. See the caution message earlier in this module.

Task 3 – Creating a Temperature Program In this task, you write a program called Temperature containing a temperature in Fahrenheit and a method called calculateCelsius. Follow these steps to create a Temperature class: 8. Write a calculateCelsius method that converts a Fahrenheit value to a Celsius value and prints the Celsius value. Use the following information to convert Farenheit values to Celsius values: • To convert from Fahrenheit to Celsius, subtract 32, multiply by 5, and divide by 9. •

Ensure that you get the same result with the program as you do when doing the calculation manually (or using a calculator)

9. Test the program using the TemperatureTest class.

Person.java .

public class Person { public int ageYears = 32; public void calculateAge() { int ageDays = ageYears * 365; long ageSeconds = ageYears * 365 * 24L * 60 * 60; System.out.println("You are " + ageDays + " days old."); System.out.println("You are " + ageSeconds + " seconds old."); } // end calculateAge } // end class

Related Documents


More Documents from "Rajani Ramsagar"