Day 2 : Intermediate Concepts 1 Examples

Similar documents
Java Professional Certificate Day 1- Bridge Session

Selenium Class 9 - Java Operators

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

JAVA OPERATORS GENERAL

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

Prof. Navrati Saxena TA: Rochak Sachan

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

AP COMPUTER SCIENCE A

Lara Technologies Special-Six Test

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

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

Programming Basics. Digital Urban Visualization. People as Flows. ia

Java Programming for Selenium

Operators Questions

Java Simple Data Types

CONDITIONAL EXECUTION

University of Palestine. Mid Exam Total Grade: 100

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Final. Your Name CS Fall 2014 December 13, points total Your Instructor and Section

Java Basic Programming Constructs

Key Java Simple Data Types

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Java Simple Data Types

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

CS 101 Exam 2 Spring Id Name

/* Solve f(x) = x*x*x-5 = 0 f'(x) = 3x^2 */

1. Short circuit AND (&&) = if first condition is false then the second is never evaluated

CompSci 125 Lecture 11

CMPS 12A - Winter 2002 Midterm 2 March 5, Name: ID:

Eng. Mohammed S. Abdualal

Tutorial 06. Conditional statement: if then, if else, switch

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

CMP 326 Midterm Fall 2015

PROGRAM: import java.util.scanner;

CSC 240 Computer Science III Spring 2018 Midterm Exam. Name

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Decision making with if Statement : - Control Statements. Introduction: -

Section 003 Fall CS 170 Exam 2. Name (print): Instructions:

You must declare all variables before they can be used. Following is the basic form of a variable declaration:

Zheng-Liang Lu Java Programming 45 / 79

CS180. Exam 1 Review

d. If a is false and b is false then the output is "ELSE" Answer?

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

CIS October 16, 2018

CSE 142, Spring 2009, Sample Final Exam #2. Good luck!

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

CS 101 Fall 2005 Midterm 2 Name: ID:

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

Roll Number. Common to II Year B.E.CSE & EIE INTERNAL ASSESSMENT TEST - I. Credit 3 R-2017

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Guru Gobind Singh Public School Sector V,B Bokaro Steel City Annual IP Assignment Class 11

Building Java Programs

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lectures Control Structures

King Saud University College of Computer and Information Sciences Computer Science Department

Loops. CSE 114, Computer Science 1 Stony Brook University

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

Repetition, Looping. While Loop

Building Java Programs

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

CS 231 Data Structures and Algorithms Fall Event Based Programming Lecture 06 - September 17, Prof. Zadia Codabux

CS212 Midterm. 1. Read the following code fragments and answer the questions.

CIS October 19, 2017

DM503 Programming B. Peter Schneider-Kamp.

CSE 142, Summer 2013 Midterm Exam, Friday, July 26, 2013

Building Java Programs

King Saud University College of Computer and Information Systems Department of Computer Science CSC 113: Java Programming-II, Spring 2016

Java Identifiers, Data Types & Variables

Some Practice Midterm Problems

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

16. Give a detailed algorithm for making a peanut butter and jelly sandwich.

Building Java Programs

Selections. CSE 114, Computer Science 1 Stony Brook University

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

University of Massachusetts Amherst, Electrical and Computer Engineering

CT 229 Java Syntax Continued

Java Review. Java Program Structure // comments about the class public class MyProgram { Variables

CMBUkTI4 XøaepÞógpÞat;lkçx½NÐ 3

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

CIS November 14, 2017

CSE 142, Summer 2014

CIS March 1, 2018

Full file at

2. What are the two main components to the CPU and what do each of them do? 3. What is the difference between a compiler and an interpreter?

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Midterm Examination (MTA)

COMP-202: Foundations of Programming. Lecture 10: Method Overloading and Passing Objects to Methods. Sandeep Manjanna, Summer 2015

I. True/False: (2 points each)

Question: Total Points: Score:

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

DM550 Introduction to Programming part 2. Jan Baumbach.

CSC240 Static method, array, and loop.

Top-down programming design

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Full file at

Transcription:

Example1 Day 2 : Intermediate Concepts 1 Examples public class Example1 public static void main(string[] args) int a= 5, b = 10, c = 15, d= 20; Assignment int x = a++; a is assigned to x and then increment System.out.println(x); End result: x = 5, a = 6 System.out.println(a); int x = ++b; b is incremented and then assigned to x System.out.println(x); End result: x = 5, a = 6 System.out.println(b); End result: b = 11, x = 11 int x = c--; c assigned to x and then decremented System.out.println(x); End result: x = 5, a = 6 System.out.println(c); End result: x= 15, c = 14 int x = --d ; End result d = 19, x= 19 System.out.println(x); End result: x = 5, a = 6 System.out.println(d); int x= -a; x = -5 System.out.println(x); End result: x = 5, a = 6 System.out.println(a); Example 2 Example 3 public class Example2 public static void main(string[] args) System.out.println(4.5<=10); System.out.println(4.5<-10); System.out.println(-35>=0); System.out.println(10<=4+6); System.out.println(35==35); System.out.println(22!=10); public class Example3 public static void main(string[] args) int x= 10, y = 20; boolean z ; z = x>y; System.out.println(z); Prints false

Example 4 public class Example4 public static void main(string[] args) int a = 1, b= 2; if(a>b) a=0; else b=0; System.out.println("a : "+a); System.out.println("b : "+b); Example 5 public class Example5 public static void main(string[] args) int a =8; if( a>0) if(a%2==0) System.out.println("Even Positive Number"); else System.out.println("Positive Odd Number"); else System.out.println("Negative Number"); System.out.println("Success! Exiting "); Example 6 public class Example6 public static void main(string[] args) int marks = 60; String grade; if (marks > 79) grade = "Honours"; else if(marks> 59) grade = "First Division"; else if(marks > 49) grade = "Second Division"; else if(marks > 39) grade = "Third Division"; else grade = "Fail"; System.out.println("Grade is : "+grade);

Example 7 public class Example7 public static void main(string[] args) int month=4; String season; switch(month) case 1: case 2: case 12: season = "Winter"; break; case 3: case 4: case 5: season = "Spring"; break; case 6: case 7: case 8: season = "Summer"; break; default: season = "Invalid Month"; System.out.println("April is in the "+season); Example 8 public class Example8 public static void main(string[] args) int x= 1; while(x<=5) System.out.println("The value of x is :" +x); x++; Example 9 public class Example9 public static void main(string[] args) int x= 1; do System.out.println("The value of x is :" +x); x++;

while(x<=5); Example 10 public class Example10 public static void main(string[] args) int x; for( x=1; x<=5;x++) System.out.println("Count is :" +x); System.out.println("Statement outside loop"); Example 11 public class Example11 public static void main(string[] args) boolean t = true; first: second: System.out.println(" Before the break"); if(t) break second; System.out.println("This won't execute"); System.out.println("After second block"); Example 12 public class Example12 public static void main(string[] args) int i; for(i=0;i<10;i++) if(i%2==0) continue; System.out.println(i + " "); Example 13 public class Example13 public static void main(string[] args) boolean t= true; System.out.println("Before the return"); if(t) return; System.out.println("After the return");

Example 14 public class Employee int empid; public static void main(string[] args) Employee emp1= new Employee(); emp1.empid = 001; emp1.name = "John"; emp1.dept = "Production"; System.out.println("Employee Id : " +emp1.empid); System.out.println("Name : " +emp1.name); System.out.println("Department : " +emp1.dept); Example 15 class Employee int empid; void display( ) System.out.println("Employee Id: " + empid); System.out.println("Name : " + name); System.out.println("Department : " + dept); public static void main(string[] args) Employee emp1= new Employee(); emp1.empid = 001; emp1.name = "John"; emp1.dept = "Production"; emp1.display( ); Example 16 Employee emp2= new Employee(); emp2.empid = 002; emp2.name = "Mary"; emp2.dept = "Testing"; emp2.display( );

Example 17 class Employee int empid; void accept(int id, String n, String d) Formal Parameters empid = id; name = n; dept = d; void display( ) System.out.println("Employee Id: " + empid); System.out.println("Name : " + name); System.out.println("Department : " + dept); public static void main(string[] args) Employee emp1= new Employee(); Employee emp2= new Employee(); emp1.accept(001, "John ", "Production "); emp2.accept(002, " Mary ", "Testing "); emp1.display( ); emp2.display( ); Example 18 class Employee int empid; void accept(int id, String n, String d) empid = id; name = n; dept = d; void accept(int id, String n) empid = id; name = n; void display( ) System.out.println("Employee Id: " + empid);

System.out.println("Name : " + name); System.out.println("Department : " + dept); public static void main(string[] args) Employee emp1= new Employee(); Employee emp2= new Employee(); emp1.accept(001, "John ", "Production"); emp2.accept(002, " Mary " ); emp1.display( ); emp2.display( ); Example 19 class Employee int empid; void accept(int id, String n, String d) empid = id; name = n; dept = d; void accept(int id, String n) empid = id; name = n; void change_basic (int a) a = 12000; void display( ) System.out.println("Employee Id: " + empid); System.out.println("Name : " + name); System.out.println("Department : " + dept); public static void main(string[] args) int basic_sal=15000; Employee emp1= new Employee(); emp1.accept(001, "John", "Production"); emp1.display( );

System.out.println("Basic Salary before method call :"+basic_sal); emp1.change_basic(basic_sal); System.out.println("Basic Salary after method call :"+basic_sal); Example 20 class Employee int empid; int basic_sal=10000; void accept(int id, String n, String d) empid = id; name = n; dept = d; void accept(int id, String n) empid = id; dept = d; void change_basic(employee ob) ob.basic_sal = 12000; void display( ) System.out.println("Employee Id: " + empid); System.out.println("Name : " + name); System.out.println("Department : " + dept); EXAMPLE 20.1 public static void main(string[] args) Employee emp1= new Employee(); emp1.accept(001, "John", "Production"); emp1.display( ); System.out.println("Basic Salary before method call: "+emp1.basic_sal); emp1.change_basic(emp1); System.out.println("Basic Salary after method call :"+emp1.basic_sal);

Example 22 public class Employee static int empid; static String name; static String dept; static void accept(int id, String n, String d) empid = id; name = n; dept = d; static void display() System.out.println("Employee Id : " +empid); System.out.println("Name : " +name); System.out.println("Department : " +dept); public static void main(string[] args) Employee emp1= new Employee(); emp1.accept(001, " John", "Production"); Employee.accept(001, "John", "Production"); classname.method() System.out.println("Name of the employee :"+Employee.name); System.out.println("Employee Id:" +Employee.empid); Example 23 public class Employee int empid; public Employee() Constructor empid =0; name =" "; dept =" "; public static void main(string[] args) Employee e2=new Employee();

Example 24 public class Employee int empid; private Employee() Constructor empid =0; name =" "; dept =" "; public static void main(string[] args) Employee e2=new Employee(); Invalid Example 25 public class Employee int empid; private Employee() Constructor empid =0; name =" "; dept =" "; void main() Employee e1= new Employee(); EXAMPLE 25.1 public static void main(string[] args) Employee e1=new Employee(001, "John", "Production"); Employee e2=new Employee(); Invalid e2.display(); Example 26 class Employee int empid; public Employee(int i, String n, String d)

empid = i; name = n; dept = d; void display() System.out.println("Employee Id : " +empid); System.out.println("Name : " +name); System.out.println("Department : " +dept); public static void main(string[] args) Employee e1=new Employee(001, "John", "Production"); Parameterized Employee e2=new Employee(); Non-parameterized e1.display(); e2.display(); Example 27 public class Employee int empid; public Employee() Constructor empid =0; name ="ABC"; dept ="TESTING "; void display() System.out.println("Employee Id : " +empid); System.out.println("Name : " +name); System.out.println("Department : " +dept); public static void main(string[] args) Employee e1=new Employee();

Example 28 public class Employee int empid; public Employee() empid=0; name="null"; dept="null"; public Employee(int i, String n, String d) empid=i; name=n; dept=d; void display( ) System.out.println("Employee Id: " + empid); System.out.println("Name : " + name); System.out.println("Department : " + dept); public static void main(string[] args) Employee e1=new Employee(001, "John", "Production"); Parameterized Employee e2=new Employee(); Non-parameterized e1.display(); e2.display();