Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming Java Lab 3: Variables and Number types 25 January 2013 1 JavaLab3.ppt Ping Brennan

Similar presentations


Presentation on theme: "Introduction to Programming Java Lab 3: Variables and Number types 25 January 2013 1 JavaLab3.ppt Ping Brennan"— Presentation transcript:

1 Introduction to Programming Java Lab 3: Variables and Number types 25 January JavaLab3.ppt Ping Brennan

2 Java Project Project Name: JavaLab3 2 ExerciseWithIntegers OrderPrice SeparateDigits

3 Class ExerciseWithIntegers Objectives –Perform integer calculations using the arithmetic operators and Math methods. –Understanding integer as a data type. Computation Math.abs(x), Math.min(a, b), Math.max(a, b) where x, a, b, are integers (or expressions) of type int. Data type integer range to i.e Applying –Read keyboard input using Scanner class 3 +, –, *, /,

4 Anatomy of Class ExerciseWithIntegers import java.util.Scanner; // first line in the program public class ExerciseWithIntegers { public static void main(String[] args) { // create a Scanner object in to read keyboard input Scanner in = new Scanner(System.in); System.out.print("Enter two integers: "); // prompt user to input int num1 = in.nextInt(); // reads in first integer input /* Write code similar to the above statement to declare a new variable num2 of type int to read in the second integer input. */ /* Next write separate Java statements to print: the sum, the difference, the product, the average, the absolute value of the difference, the maximum and the minimum. */ } Hint: use the methods Math.abs(x), Math.min(a,b) and Math.max(a,b). 4

5 Class OrderPrice Calculates the price of an order from the total price and number of books ordered. Objectives –Using arithmetic operators : +, –, *, / –Understanding arithmetic expressions Formulae Applying –Read keyboard input using Scanner class –Declaring variables of data types: int and double 5 tax = totalBookPrice * 0.075; shippingCharge = noOfBooks * 2.00; orderPrice = totalBookPrice + tax + shippingCharge ;

6 Anatomy of Class OrderPrice import java.util.Scanner; // first line in the program public class OrderPrice { public static void main(String[] args) { // Write separate Java statements to do the following: // a) Read in the total book price and the number of books // b) Compute the tax (i.e. 7.5% of the total book price) // c) Compute the shipping charge ($2 per book) // d) Price of order = total book price + tax + shipping charge // e) Print the price of the order } 6

7 Class SeparateDigits Reads in a five digit positive integer and prints out the individual digits, separated by spaces. For example, the input is printed out as: Objective –Understand the arithmetic operators: % (modulus) and / (division). Formulae Applying –Read input using Scanner class 7 tenThousand = posNumber /10000; thousand = ( posNumber % 10000) / 1000; hundred = ( posNumber % 1000) / 100; // continue to write own formulae to calculate ten and unit

8 Anatomy of Class SeparateDigits import java.util.Scanner; // first line in the program public class SeparateDigits { public static void main(String[] args) { /* Declare a variable posNumber of type int and store a five digit positive number input in the variable. */ // firstly declare all relevant variables below before using them. tenThousand = posNumber /10000; thousand = ( posNumber % 10000) / 1000; hundred = ( posNumber % 1000) / 100; // write Java statements to calculate ten and unit // write code to print out the individual digits, separated by spaces } 8


Download ppt "Introduction to Programming Java Lab 3: Variables and Number types 25 January 2013 1 JavaLab3.ppt Ping Brennan"

Similar presentations


Ads by Google