Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt Ping Brennan

Slides:



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

I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
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
8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming
Introduction to Programming
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 Java Lab 3: Variables and Number types 25 January JavaLab3.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.
Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming.
Lecture 10 Methods COMP1681 / SE15 Introduction to Programming.
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world.
Chapter 8: Arrays.
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
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.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Computer Programming Lab(7).
Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
CS110 Programming Language I
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Java Programming Strings Chapter 7.
1 Various Methods of Populating Arrays Randomly generated integers.
Computer Programming Lab 8.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Lecture 5 Strings and more I/O COMP1681 / SE15 Introduction to Programming.
Computer Programming Lab(4).
Integer Division What is x? int x = 1729 / 100; Answer: 17.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
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 Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
Introduction to Programming Python Lab 5: Strings and Output 05 February PythonLab5 lecture slides.ppt Ping Brennan
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Introduction to Programming Python Lab 2: Variables 15 January PythonLab2 lecture slides.ppt Ping Brennan
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
Introduction to Programming
Introduction to Programming
Introduction to Programming
INPUT STATEMENTS GC 201.
Introduction to Programming
An Introduction to Java – Part I, language basics
Introduction to Classes and Methods
Introduction to Programming
Introduction to Programming
Chapter 2: Java Fundamentals
Introduction to Programming
Presentation transcript:

Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt Ping Brennan

Two Java Projects Project Name: JavaLab2a 2 Project Name: JavaLab2b Volume1 Purchase Price (fixed prices) Purchase Price using keyboard input (input from Keyboard) SwapCharacters

JavaLab2a: Class Volume1 Compute the volume in litres of a six-pack of soda cans. Objective –declare variables in the method main using the two statements below. int cansPerPack = 6; double canVolume = 0.355; Note: All variables in Java programs must be declared. 3

JavaLab2a: Class Volume1 (2) Data types –integer The variable cansPerPack is of type int and the value of the variable is set to 6. –double (floating point) The variable canVolume can take non-integer values and is declared as double. Use the formula below inside a System.out.print() statement to calculate the total volume. 4 cansPerPack * canVolume

JavaLab2a: Class Volume1 (3) Type in the following code for the class Volume1 5 /** * This program computes the volume (in litres) of a six-pack of soda cans. */ public class Volume1 { public static void main(String[] args) { int cansPerPack = 6; double canVolume = 0.355; // Litres in a 12-ounce can System.out.print("A six-pack of 12-ounce cans contains "); System.out.print( cansPerPack * canVolume ); System.out.println(" litres."); }

JavaLab2a: Calculate total purchase price Revise the method main in class Volume1 to calculate total purchase price using fixed variables. Objective –declare two new variables: unitPrice which is of type double; and quantity which is of type int. –Use reasonable initial values for the two variables, e.g. int quantity = 6; Use the formula below to calculate total purchase price 6 unitPrice * quantity ;

JavaLab2a: Input from Key Board Revise the method main in class Volume1 to calculate total purchase price using input data from keyboard. Objectives –declare variables –read data from keyboard Use the formula below to calculate total purchase price 7 unitPrice * quantity ;

JavaLab2a: Input from Key Board (2) The following lines of code are required. Note: input from the key board is made in the BlueJ: Terminal Window – JavaLab2a. You must first create this window using a print or println statement (as shown above) before attempting to enter input from the key board. 8 import java.util.Scanner; // the first line of program file // The following code must be written inside the method main Scanner in = new Scanner(System.in); /* create an object in of type Scanner */ System.out.print("Enter the quantity: "); // create terminal window int quantity = in.nextInt() ; /* read an integer and store it in the variable quantity. */ /* The object in contains the method in.nextInt() for reading integers from the key board. */

JavaLab2b: Class SwapCharacters String exercise on swapping two letters in a string. Objectives –String type –Concatenation –Strings and characters –Substrings Exercises –Concatenate strings –Working with substrings 9

String operations String str = "Ja"; // Declare the string variable str. Set it to "Ja" str = str + "va"; // + specifies string concatenation. // str is set to "Java" String greeting = "H & S"; int n = greeting.length() ; /* The method greeting.length returns the length of the string, greeting. In this case n is set to 5 */ 10

String operations (2) String str = "Sally"; String str2 = str.substring(1, 4) ; /* Extract from str the substring starting at position 1 and ending immediately before position 4. Set str2 equal to this substring (i.e. str2 now has the value "all"). Note that string indexing begins with 0. */ String str = "Sally"; String str3 = str.substring(1) ; /* str3 now has the value "ally" */ 11

Swapping two letters in a string 1.Given a string, for example, String str ="Harry Potter"; 2.Input position i = 2 (so str.substring(i,i+1) returns the character r) 3.Input position j = 8 (so str.substring(j,j+1) returns t) 4. String first = str.substring(0, i); /* returns Ha */ 5. String middle = str.substring(i+1, j); /* returns ry Po */ 6. String last = str.substring(j+1); /* returns ter */ 7.Concatenate five strings: first + str.substring(j,j+1) + middle + str.substring(i,i+1) + last which returns Hatry Porter 12 HarryPotter index

Swapping two letters in a string (alternative solution) 1.Given a string, for example, String str ="Harry Potter"; 2.Input position i = 2 (so str.charAt(i) returns the character r) 3.Input position j = 8 (so str.charAt(j) returns t) 4. String first = str.substring(0, i); /* returns Ha */ 5. String middle = str.substring(i+1, j); /* returns ry Po */ 6. String last = str.substring(j+1); /* returns ter */ 7.Concatenate five strings: first + str.charAt(j) + middle + str.charAt(i) + last which returns Hatry Porter 13 HarryPotter index