Data types, Expressions and assignment, Input from User

Slides:



Advertisements
Similar presentations
Reading Input from the Console eval function. variable = input("Enter a value: ") The value entered is a string. You can use the function eval to evaluate.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Java Overview CS2336: Computer Science II1. First Program public class HelloWorld { public static void main(String args[]) { System.out.println("Hello.
LAB 10.
Computer Programming Lab(4).
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
Computer Programming Lab(5).
Fundamental concepts in Java. Lesson plan Variable declaration, assign statement & practice Design document & practice.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
VARIABLES Introduction to Computer Science 1- COMP 1005, 1405 Instructor : Behnam Hajian
Java Programming: From the Ground Up
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Lecture 2: Classes and Objects, using Scanner and String.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
Chapter 2 Elementary Programming
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Assignment statements using the same variable in LHS and RHS.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
Announcements Quiz 1 Next Monday. int : Integer Range of Typically –2,147,483,648 to 2,147,483,647 (machine and compiler dependent) float : Real Number.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Introduction to programming in java
Chapter 2 Elementary Programming
Topic 2 Elementary Programming
Chapter 2 Clarifications
Software Development I/O and Numbers
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Elementary Programming
Chapter 2 Elementary Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 2 Elementary Programming
Something about Java Introduction to Problem Solving and Programming 1.
INPUT STATEMENTS GC 201.
While Statement.
Computers & Programming Languages
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Java Variables, Types, and Math Getting Started
CSE 1020:Programming by Delegation
Java so far Week 7.
Chapter 3 Numerical Data
IFS410 Advanced Analysis and Design
Chapter 2 Elementary Programming
Lecture Notes – Week 3 Lecture-1
Lecture Notes – Week 2 Lecture-2
Week 4 Lecture-2 Chapter 6 (Methods).
Introduction to Java Brief history of Java Sample Java Program
CS2011 Introduction to Programming I Elementary Programming
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Presentation transcript:

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?