Download presentation

Presentation is loading. Please wait.

Published byChristopher Farley Modified over 3 years ago

1
Introduction to Programming Java Lab 3: Variables and Number types 25 January 2013 1 JavaLab3.ppt Ping Brennan (p.brennan@dcs.bbk.ac.uk)

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 -2 31 to 2 31 -1 i.e. -2147 483648... 2147483647 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 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 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

Similar presentations

OK

Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.

Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on wireless networks Ppt on grasslands of the world Ppt on l&t finance ltd Ppt on blood stain pattern analysis jobs Ppt on taj hotel mumbai Ppt on db2 introduction Ppt on chromosomes and genes and alleles Ppt on construction of bridges in hilly areas Ppt on solar energy system Ppt on english grammar tenses