Data types, Expressions and assignment, Input from User Lab-Week 2 Data types, Expressions and assignment, Input from User
Variables and Primitive Data Types With expression and assignment // This program prints Welcome to Java! public class ComputerArea { //main method public static void main(String[] args) { double radius; double area; //assign a value to radius radius = 20; //Computer the area given a radius and //assign the result to area area = radius * radius * 3.1419; //Display result (i.e. the value of radius) System.out.println(”The area for the” + ”circle of radius” + radius + ”is” + area); } 2 2 2 2 2 2 2
Using Constants Named constants // This program prints Welcome to Java! public class ComputerArea { //main method public static void main(String[] args) { final double PI = 3.14159; double radius; double area; //assign a value to radius radius = 20; //Computer the area given a radius and //assign the result to area area = radius * radius * PI; //Display result (i.e. the value of radius) System.out.println(”The area for the” + ”circle of radius” + radius + ”is” + area); } Named constants A named constant is declared to represent a fixed value. A named constant is used in an expression. The syntax for named constants is: final datatype CONSTANTNAME = value; 3 3 3 3 3 3 3 3 3
Input from Keyboard A comment across lines can be enclosed in a pair of /* and */. /* This program reads a radius from keyboard and displays the area on monitor. */ import java.util.Scanner; public class ComputerAreaWithConsoleInput { public static void main(String[] args) { Scanner input = new Scanner(System.in); double radius, area; //Prompt the user to enter a radius System.out.print("Enter a number for radius: "); double radius = input.nextDouble(); area = radius * radius * 3.14159; System.out.println(”The area for the” + ”circle of radius” + radius + ”is” + area); } Import scanner class from java.util package. Create a scanner object for the keyboard. Read a number entered by the user system.in and system.out refer to the keyboard and the monitor by default. System.out is a predefined object but system.in is not so an object representing system.in needs to be defined. More than one variable of the same data type can be declared in a statement. Input can be from other sources, e.g., a file if the scanner object is linked to the file. 4 4 4 4 4 4 4 4 4 4
Read Different Data Types
Case Study In this case study, we learn how to design a solution for a given problem, and how to write, compile, run and test a program to implement the designed solution. 6
Problem Specification The problem is to design and implement a program that lets the user enter the interest rate, number of years, and loan amount, and computes and displays monthly payment and total payment. 7 7 7
Top-level Design Prompt the user to enter the annual interest rate, the number of years, and the loan amount. Compute the monthly interest rate Compute the payments Display the payments 8 8 8
Design Refinement (1. Prompt the user to enter the annual interest rate, the number of years, and the loan amount.) is refined by: 1.1. Prompt the user to enter the annual interest rate 1.2. Prompt the user to the number of years 1.4. Prompt the user to enter the loan amount 9 9 9 9
Design Refinement (2. Compute the monthly interest rate) is replaced by: 2.1 Calculate monthly interest rate with monthlyInterestRate = annualInterestRate/12 10 10 10 10 10
Design Refinement (3. Compute the payments) is replaced by: 3.1 Compute the monthly payment using (1) 3.2 Compute the total payment (2) 11 11 11 11 11 11
Design Refinement (4. Display the payments) 4.1. Display the monthly payment 4.2 Display the total payments 12 12 12 12 12 12
Final Design 1.1. Prompt the user to enter the annual interest rate 1.2. Prompt the user to the number of years 1.4. Prompt the user to enter the loan amount 2.1 Calculate monthly interest rate with monthlyInterestRate = annualInterestRate/12 3.1 Compute the monthly payment using formula (1) 3.2 Compute the total payment using formula (2) 4.1. Display the monthly payment 4.2 Display the total payments 13 13 13 13 13 13 13
Implementation ComputeLoan.java import java.util.Scanner; public class ComputeLoan { public static void main(String[] args) { //Create a Scanner Scanner input = new Scanner(System.in); //Prompt the user to enter annual interest rate in percentage System.out.print("Enter annual interest rate, e.g., 7.25%: "); double annualInterestRate = input.nextDouble(); /Prompt the user to enter number of years System.out.print("Enter number of years as an integer, e.g., 5: "); int numberOfYears = input.nextInt(); // Prompt the user to enter loan amount System.out.print("Enter loan amount, e.g., 120000.95: "); double loanAmount = input.nextDouble(); 14
Implementation //Calculate monthly interest rate double monthlyInterestRate = annualInterestRate /1200; // Calculate monthly payment double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12)); // Calculate total payment double totalPayment = monthlyPayment * numberOfYears * 12; // Display monthly payment System.out.println("The monthly payment is £" + (int)(monthlyPayment * 100) / 100.0); // Display total payment System.out.println("The total payment is £" + (int)(totalPayment * 100) / 100.0); } 15 15 15 15
Questions?