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

Slides:



Advertisements
Similar presentations
IS Programming Fundamentals 1 (Lec) Date: September 14, Array.
Advertisements

Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Introduction to Programming Java Lab 1: My First Program 11 January JavaLab1.ppt Ping Brennan
Introduction to Programming Overview of Week 2 25 January Ping Brennan
Introduction to Programming Java Lab 7: Loops 22 February JavaLab7 lecture slides.ppt Ping Brennan
22 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
18 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming
1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming Java Lab 4: Formatted Output and Strings 1 February JavaLab4 lecture slides.ppt Ping Brennan
Introduction to Programming
Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt Ping Brennan
8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Introduction to Programming
25 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world.
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
1 Review Quisioner Kendala: Kurang paham materi. Praktikum Pengaruh teman.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Review BP Dari slide pak cahyo pertemuan 7 dan pertemuan 2 dengan sedikit modifikasi.
Computer Programming Lab(7).
Java Math Class. What is the Math Class? The Math Class is another class that is prepared by Java for us to use We use this class for mathematical operations.
Building Java Programs Interactive Programs w/ Scanner What is a class? What is an object? What is the difference between primitive and object variables?
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
1 MATH METHODS THAT RETURN VALUES. 2 JAVA'S MATH CLASS.
Computer Programming Lab 8.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
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;
Computer Programming Lab(4).
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
More While Loop Examples CS303E: Elements of Computers and Programming.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
习 题 4.23 编写一个 applet ,读取一个矩形的边长,然后 用在 paint 方法中使用 drawString 方法画出用星组成 的空心矩形。程序应能画出边长从 1 到 20 的任何矩 形。
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Mixing integer and floating point numbers in an arithmetic operation.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Romelia Plesa, Alan Williams, Sylvia Boyd, Daniel Amyot, Diana Inkpen, Gilbert Arbez, Mohamad Eid ITI 1120 Lab #3.
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 Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
Introduction to Programming
Introduction to Programming
Introduction to Programming
CSC111 Quick Revision.
Introduction to Programming
Introduction to Programming
Data types, Expressions and assignment, Input from User
TK1114 Computer Programming
SELECTION STATEMENTS (1)
Something about Java Introduction to Problem Solving and Programming 1.
INPUT STATEMENTS GC 201.
Control Statement Examples
Computers & Programming Languages
Introduction to Classes and Methods
Introduction to Programming
Fundamentals 2.
Chapter 2: Java Fundamentals
Introduction to Programming
Introduction to Programming
Introduction to Programming
Building Java Programs
Building Java Programs
Names of variables, functions, classes
Introduction to Programming
Presentation transcript:

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

Java Project Project Name: JavaLab3 2 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 to i.e Applying –Read keyboard input using Scanner class 3 +, –, *, /,

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

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 ;

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

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

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