Chapter 2.pptx
Document Details
Tags
Full Transcript
CHAPTER 2: DATA TYPES AND OPERATORS Variables, Identifiers & Data Types Variables and Identifiers Variables: location in the computer memory where a value can be stored for use by a program. Used for data that change during program execution. Identifiers: The names assigned to...
CHAPTER 2: DATA TYPES AND OPERATORS Variables, Identifiers & Data Types Variables and Identifiers Variables: location in the computer memory where a value can be stored for use by a program. Used for data that change during program execution. Identifiers: The names assigned to variables. An Identifier must be unique within a scope of the Java program. Local variables: Variables declared inside of a block or method that need to be initialized before use. Only visible to the methods in which they are declared; they are not accessible from the rest of the class. Example: int count = 0; Variables, Identifiers & Data Types Variables and Identifiers Instance variables: values are unique to each instance of a class (to each object, in other words); eg. the currentSpeed of one bicycle is independent from the currentSpeed of another. Class variables: any field declared with the static modifier. there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. Example; static int numGears = 6; Variables, Identifiers & Data Types Data Types Indicates the kind of value they can store or indicates the attributes of the variable, such as the range of values that can be stored and the operators that can be used to manipulate the variable. 4 main primitive data types: Integer: byte, short, int, and long. Floating Point: float and double Character: char Boolean: variable with a value of true or false. Declarations Exampl es int i = 7; Declare and initialize double x = 10.5; char aChar = 'S'; boolean aBoolean = true; String str1 = “My name is bob”; char name; Declare Access Modifier To set level of access for classes, fields or variables, methods and constructors. TYPE S PRIVAT PROTECT DEFAUL PUBLIC E ED T Access Modifier PRIVA Most restrictive access TE level. Accessible within declared class. Class and interfaces PUBLI cannot be private. Accessible from any other C class in the java program. Access Modifier Variables, methods and PROTECT ED constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class. cannot be applied to class and interfaces. Declare without access modifier. DEFAUL Available to any other class in the T same package. Operators Assignment = Operators Arithmetic - + * / % ++ -- Operators Relational > < >= > >>> Operator Compound += -= *= /= %= Assignment Operators Example Arithmetic Compound Assignment Operators: int c = 3 c += 7 c=c+7 c = 10 c%= 17 c= c%17 c= (remainder) Increment and Decrement Operators ++a a++ Increase or -- b decrease by 1 b-- Reading Keyboard Input import java.util.Scanner; public class InputExp { Metho public static void main(String[] args) { d1 String name; int age; Scanner in = new Scanner(System.in); // Reads a single line from the console // and stores into name variable name = in.nextLine(); // Reads a integer from the console // and stores into age variable age=in.nextInt(); in.close(); // Prints name and age to the console System.out.println("Name :"+name); System.out.println("Age :"+age); }} Reading Keyboard Input import java.io.*; Metho public class Main { d2 public static void main(String[] args) throws IOException{ BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); int mark; System.out.print("Enter Mark: "); mark = Integer.parseInt(stdin.readLine()); Reading Keyboard Input using Dialog Box import javax.swing.JOptionPane; public class Addition { public static void main(String[] args) { String firstNumber; String secondNumber; int num1, num2, sum; firstNumber = JOptionPane.showInputDialog("Enter first integer"); secondNumber = JOptionPane.showInputDialog("Enter second integer"); num1 = Integer.parseInt(firstNumber); num2 = Integer.parseInt(secondNumber); sum = num1 + num2; JOptionPane.showMessageDialog(null, "The sum is "+sum, "Results", JOptionPane.PLAIN_MESSAGE); System.exit(0); } } Task for today() 1 Write an interactive java program with class “ArithmeticProg” , define two integers and use the five arithmetic operators to perform different arithmetic operations. 2 Write a java program to display the user’s input (Name, age, hobby, favorite food). 3 Write an interactive Java program to calculate people’s age. Identify the input and display the output. Task for today() 4 Write an interactive java program to calculate the average marks for a student. Input: Name, ID, Subject, Test1(/100), Test2(/100). Output: Name, ID, Subject, Average mark. 5 Write an interactive java program to calculate the price of an item after discount. Input: Item name, price, quantity, discount rate. Output: Item name, price, quantity, total price, discount rate, price after discount. Tasks for today 6 Write an interactive Java program that calculate the user’s BMI (Body Mass Index). Identify the input data and display the output. 7 Write a java program with class “CompoundProg” , to explain the difference between the four Increment and Decrement Operators. Formatting floating points Exampl e: String Output; Output = String.format("Hi %s. Your BMI is %.2f", first,i/(j*j)); Reading character char operator = input.charAt(0);