Download presentation
Presentation is loading. Please wait.
Published byRainer Berg Modified over 5 years ago
2
Lecture Notes - Week 2 Lecture-1
3
Previous lecture Introduction Programming languages
How to write a simple program
4
Outline of Lecture Primitive data types Variables Constants
Assignments Expressions Operators Input and output To do list
5
A Simple Java Program First Java program that will evolve gradually into a number of more complicated programs that incorporate various new features of Java as we learn them. Listing 1.1 // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }
6
Creating, Compiling and Running Programs
7
Classes and Class Names
// This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } Every Java program must have at least one class. Each class has a class name, which starts with an uppercase letter.
8
Main Methods // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } Each program must have a main method in a class in order to run the program. The program is executed from the main method. 8
9
Statements // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } Each class must have at least one statement that describes an action or a sequence of actions to be executed. Every statement ends with a semicolon (;), called statement terminator. 9 9
10
Class Blocks // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); System.out.println("Learning Java is" + "exciting!”); } Each class has a class block enclosed in a pair of braces. 10 10 10
11
Method Blocks // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); System.out.println("Learning Java is" + "exciting!”); } Each method has a method block enclosed in a pair of braces, which can contain more or more statement. 11 11 11 11
12
Reserved Words or Keywords
// This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); System.out.println("Learning Java is" + "exciting!”); } A reserved word or keyword has a specific meaning to the compiler and cannot be used for other purposes in the program. 12 12 12 12 12
13
Comments // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); System.out.println("Learning Java is" + "exciting!”); } A line comment starts with double forward slashes (//). Comments are for program readability and ignored by the compiler. Use a pair of /* and */ for multi-lines of comments. 13 13 13 13 13 13
14
Variables and Primitive Data Types
// 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 * ; //Display result (i.e. the value of radius) System.out.println(”The area for the” + ”circle of radius” + radius + ”is” + area); } radius and area are variables of double data type Each variable represents a value stored in the computer. Each variable needs to be declared with a data type (e.g. double). A variable starts with a lowercase letter by convention. 14 14 14 14 14 14 14
15
Primitive Data Types 15
16
Assignments and Expressions
// 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 * ; //Display result (i.e. the value of radius) System.out.println(”The area for the” + ”circle of radius” + radius + ”is” + area); } An assignment statement assigns a value to a variable. An assignment statement assigns the value of an expression to a variable. The syntax for assignment statements is: variable = expression; 16 16 16 16 16 16 16 16
17
Operators 17
18
Named Constants Named constants
// This program prints Welcome to Java! public class ComputerArea { //main method public static void main(String[] args) { final double PI = ; 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; 18 18 18 18 18 18 18 18 18
19
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 * ; 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. 19 19 19 19 19 19 19 19 19 19
20
Read Different Data Types
22
Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.