Object Oriented Programming Java-Lecture 1
Standard output System.out is known as the standard output object Methods to display text onto the standard output System.out.print prints text onto the screen cursor will not move its place after the text is print Example: System.out.print( Hello World ); System.out.println prints text followed by a new line onto the screen cursor will move onto the next line Example: System.out.println( Hello World ); System.out.printf displays formatted data its like C printf statement Example: System.out.printf( %s %s, Hello, World );
Standard output printing data items public class Sum public static void main(string[] args) int x = 10; int y = 20; int sum; sum = x + y; System.out.print("x = "+x+" y = "+y+" sum = "+sum+"\n"); System.out.println("x = "+x+" y = "+y+" sum = "+sum); System.out.printf("x = %d y = %d sum = %d", x, y, sum); Output: x = 10 y = 20 sum = 30 x = 10 y = 20 sum = 30 x = 10 y = 20 sum = 30 + Concatenation operator
Standard input We can use java Scanner class to get input from the standard input We can create object of the scanner class and use its available methods to read input data from the standard input Some of the Scanner methods nextint() - To read integer number from the standard input nextfloat() To read decimal number from the standard input and available as a float nextdouble() -To read decimal number from the standard input and available as a double nextline() to read one line of text from the standard input next() To read one word from the standard input
Standard input - Example import java.util.scanner; // Scanner class is available in java.utils public class Sum public static void main(string[] args) int x; int y; int sum; Scanner input = new Scanner(System.in);//Creating Scanner object //which extract data from standard input x = input.nextint(); // reading integer data from the standard input y = input.nextint(); sum = x + y; System.out.printf("x = %d y = %d sum = %d", x, y, sum);
Importing java packages By default package java.lang is imported in every java program Thus classes in java.lang does not require an import declaration All other classes requires import declaration When you don t want to use import you need use fully qualified class name when creating an object of the class Usually import declaration is preferred over fully qualified class name
Using Fully qualified class name //no import statement required public class Sum public static void main(string[] args) int x; int y; int sum; java.util.scanner input = new java.util.scanner(system.in); x = input.nextint(); y = input.nextint(); sum = x + y; System.out.printf("x = %d y = %d sum = %d", x, y, sum);
Arithmetic and Relational operators Arithmetic operators Multiplication * Division / Modulus % Addition + Subtraction - Relational operator Greater than > Less than < Greater than or equals to >= Less than or equals to <= Equals to == Not equals to!= Assignment operator =
Order of precedence Operators Association * / % Left to Right + - Left to Right < <= > >= Left to Right ==!= Left to Right = Right to Left
Building Grade Book Class GradeBook class with one method that simply displays a welcome message when it is called Modify the GradeBook class method to take the course name as an argument and displays the name as part of the welcome message Modify the GradeBook class to include variable for a course name and we will design methods to store the course name and obtain the course name Modify the GradeBook class to initialize the grade book object when it is created. We will also simultaneously develop GradeBookTest class to test the GradeBook class
GradeBook/GradeBookTest Classes GradeBook GradeBookTest +displaymessage() +main() + states that method is public
GradeBook Class public class GradeBook public void displaymessage() System.out.println("Welcome to the Grade Book!");
GradeBookTest Class public class GradeBookTest public static void main(string[] args) GradeBook oopsgradebook = new GradeBook(); oopsgradebook.displaymessage();
Declaring Method with parameter GradeBook +displaymessage(coursename: String)
Updated GradeBook class public class GradeBook public void displaymessage(string coursename) System.out.printf("Welcome to the Grade Book for \n%s\n", coursename);
Updated GradeBookTest import java.util.scanner; public class GradeBookTest public static void main(string[] args) Scanner input = new Scanner(System.in); GradeBook oopsgradebook = new GradeBook(); System.out.println("Please Enter the course name:"); String nameofcourse = input.nextline(); System.out.println(); oopsgradebook.displaymessage(nameofcourse);
Instance Variable, set and get methods GradeBook - coursename: String +setcoursename( name : String ) +getcoursename() : String +displaymessage( ) - states that instance variable is private
Updated GradeBook Class public class GradeBook private String coursename; public void setcoursename( String name ) coursename = name; public String getcoursename() return coursename; public void displaymessage() System.out.printf("Welcome to the Grade Book for \n%s\n", getcoursename());
Updated GradeBookTest Class import java.util.scanner; public class GradeBookTest public static void main(string[] args) Scanner input = new Scanner(System.in); GradeBook oopsgradebook = new GradeBook(); System.out.printf("Initial course name is: %s\n\n", oopsgradebook.getcoursename()); System.out.println("Please Enter the course name:"); String nameofcourse = input.nextline(); oopsgradebook.setcoursename(nameofcourse); System.out.println(); oopsgradebook.displaymessage();
Initializing Object With Constructor GradeBook - coursename: String <<constructor>> GradeBook( name : String) +setcoursename( name : String ) +getcoursename() : String +displaymessage( )
Updated GradeBook Class public class GradeBook private String coursename; public GradeBook(String name ) coursename = name; public void setcoursename( String name ) coursename = name; public String getcoursename() return coursename; public void displaymessage() System.out.printf("Welcome to the Grade Book for \n%s\n", getcoursename());
Updated GradeBookTest Class import java.util.scanner; public class GradeBookTest public static void main(string[] args) Scanner input = new Scanner(System.in); System.out.println("Please Enter the course name:"); String nameofcourse = input.nextline(); GradeBook oopsgradebook = new GradeBook(nameOfCourse); System.out.printf("Initial course name is: %s\n\n", oopsgradebook.getcoursename()); System.out.println("Please Enter the new course name:"); String nameofnewcourse = input.nextline(); oopsgradebook.setcoursename(nameofnewcourse); System.out.printf("New course name is: %s\n\n", oopsgradebook.getcoursename()); System.out.println(); oopsgradebook.displaymessage();
Lab Exercise 1. Modify the GradeBook class as follows Include a string instance variable that represents the name of the course instructor Provide a set method to change the instructor s name and a get method to retrieve it. Modify the constructor to specify two parameters one for the course name and one for the instructor s name Modify the method displaymessage to output the welcome message and course name followed by Instructor of the Course is: and the instructor s name 2. Create a Class called Employee that includes Two instance variables Name and monthly salary. Write a constructor that initializes the two instance variables. Provide set and get method for each instance variable. Write a test application named EmployeeTest that demonstrates class Employee capabilities. Create two Employee objects and display each object s yearly salary. Then give each employee a 10% raise and display each employees yearly salary again.