Object Oriented Programming. Java-Lecture 1

Similar documents
Welcome1.java // Fig. 2.1: Welcome1.java // Text-printing program.

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Introduction to Classes and Objects

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Lecture 7: Classes and Objects CS2301

Basic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of

Introduction to Classes and Objects

Introduction to Classes and Objects

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Fundamentals of Programming Data Types & Methods

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

CS111: PROGRAMMING LANGUAGE II

Introduction to Java Applications

Programming with Java

Reading Input from Text File

JAVA- PROGRAMMING CH2-EX1

Object Oriented Programming. Java-Lecture 6 - Arrays

Section 2.2 Your First Program in Java: Printing a Line of Text

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Data Conversion & Scanner Class

Section 2.2 Your First Program in Java: Printing a Line of Text

H212 Introduction to Software Systems Honors

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

CS141 Programming Assignment #6

Dr.Ammar Almomani. Dept. of Information Technology, Al-Huson University College, Al- Balqa Applied University. Dr. Ammar Almomani-2017-BAU Page 1

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

Introduction to Java Unit 1. Using BlueJ to Write Programs

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

AP Computer Science Unit 1. Programs

Full file at

Computational Expression

AP Computer Science Unit 1. Writing Programs Using BlueJ

Introduction to Java Applications; Input/Output and Operators

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Elementary Programming. CSE 114, Computer Science 1 Stony Brook University

CS 211: Existing Classes in the Java Library

Lecture 8 " INPUT " Instructor: Craig Duckett

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

AP Computer Science Unit 1. Writing Programs Using BlueJ

Formatting Output & Enumerated Types & Wrapper Classes

Introduction to Computer Science I

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

A Quick and Dirty Overview of Java and. Java Programming

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Elementary Programming

CSCI 1103: File I/O, Scanner, PrintWriter

CS 112 Introduction to Programming

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

CSCI 1103: File I/O, Scanner, PrintWriter

Chapter 2: Basic Elements of Java

CS 106 Introduction to Computer Science I

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Building Java Programs

JAVA Ch. 4. Variables and Constants Lawrenceville Press

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

ECE 161 WEEK 4 Introduction to Programing in Java

Chapter 2 ELEMENTARY PROGRAMMING

Building Java Programs

CMSC131. Introduction to your Introduction to Java. Why Java?

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

ing execution. That way, new results can be computed each time the Class The Scanner

download instant at

Course Outline. Introduction to java

2.5 Another Application: Adding Integers

CS 106 Introduction to Computer Science I

Tutorial # 4. Q1. Evaluate the logical (Boolean) expression in the following exercise

Lecture Set 2: Starting Java

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

Lecture Set 2: Starting Java

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

Introduction to Software Development (ISD) David Weston and Igor Razgon

4 WORKING WITH DATA TYPES AND OPERATIONS

by Pearson Education, Inc. All Rights Reserved. 2

Control Statements: Part 1

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

Lab1 Solution. Lab2 Solution. MathTrick.java. CoinFlip.java

Project 1. Java Data types and input/output 1/17/2014. Primitive data types (2) Primitive data types in Java

MIDTERM REVIEW. midterminformation.htm

Chap. 3. Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L,

I. Variables and Data Type week 3

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Programming in Java

Introduction to Java & Fundamental Data Types

Transcription:

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.