80 likes | 132 Views
Introduction to Programming. Java Lab 3: Variables and Number types. JavaLab3 lecture slides.ppt Ping Brennan ( Ping.Brennan@gmail.com ). 24 January 2014. Java Project. Project Name : JavaLab3. ExerciseWithIntegers. OrderPrice. SeparateDigits. Class ExerciseWithIntegers. Objectives
E N D
Introduction to Programming Java Lab 3: Variables and Number types JavaLab3 lecture slides.ppt Ping Brennan (Ping.Brennan@gmail.com) 24 January 2014
Java Project Project Name: JavaLab3 ExerciseWithIntegers OrderPrice SeparateDigits
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 -231 to 231-1 i.e. -2147483648 ... 2147483647 • Applying • Read keyboard input using Scanner class
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 intnum1 = 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. */ } } Tip: use the methods Math.abs(x), Math.min(a, b) and Math.max(a, b) from the Math class (java.lang.Math)
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 tax = totalBookPrice * 0.075; shippingCharge = noOfBooks * 2.00; orderPrice= totalBookPrice + tax + shippingCharge;
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 } }
Class SeparateDigits • Reads in a five digit positive integer and prints out the individual digits, separated by spaces. For example, the input 16348 is printed out as: 1 6 3 4 8 • Objective • Understand the arithmetic operators: % (modulus) and / (division). • Formulae • Applying • Read input using Scanner class tenThousand = posNumber/10000; thousand = (posNumber% 10000) / 1000; hundred = (posNumber% 1000) / 100; // continue to write own formulae to calculate ten and unit
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 } }