SlideShare a Scribd company logo
1 of 67
Chapter 25 - Beyond C & C++: Operators, Methods, and Arrays in Java Outline 25.1 Introduction 25.2 Primitive Data Types and Keywords 25.3 Logical Operators 25.4 Method Definitions 25.5 Java API Packages 25.6 Random Number Generation 25.7 Example: A Game of Chance 25.8 Methods of Class  JApplet 25.9 Defining and Allocating Arrays 25.10 Examples Using Arrays 25.11 References and Reference Parameters 25.12 Multiple-Subscripted Arrays
Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
25.1 Introduction ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
25.2 Primitive Data Types and Keywords ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
25.2 Primitive Data Types and Keywords (II)
25.2 Primitive Data Types and Keywords (III) ,[object Object],[object Object],[object Object]
25.3 Logical Operators ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
25.3 Logical Operators (II)
25.3 Logical Operators (III) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
25.3 Logical Operators (IV)
25.3 Logical Operators (V) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
25.3 Logical Operators (VI) ,[object Object],[object Object],[object Object]
Logical-Operators.java (Part 1 of 2)
Logical-Operators.java (Part 2 of 2)
Program Output
25.4 Method Definitions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
25.4 Method Definitions (II) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
25.4 Method Definitions (III) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
25.4 Method Definitions (IV) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
SquareInt.java (Part 1 of 2)
SquareInt.java (Part 2 of 2) Program Output
25.5 Java API Packages ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
25.5 Java API Packages (II)
25.5 Java API Packages (III)
25.5 Java API Packages (IV)
25.5 Java API Packages (V)
25.5 Java API Packages (VI)
25.5 Java API Packages (VII)
25.5 Java API Packages (VIII)
25.6 Random Number Generation ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
RandomInt.java Program Output
RollDie.java (Part 1 of 2)
RollDie.java (Part 1 of 2) Program Output
25.7 Example: A Game of Chance ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
25.7 Example: A Game of Chance (II) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
25.7 Example: A Game of Chance (III) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
25.7 Example: A Game of Chance (IV) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
25.7 Example: A Game of Chance (V) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Craps.java (Pat 1 of 5)
Craps.java (Pat 2 of 5)
Craps.java (Pat 3 of 5)
Craps.java (Pat 4 of 5)
Craps.java (Pat 5 of 5)
Program Output
25.8 Methods of Class  JApplet ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
25.8 Methods of Class  JApplet   (II) First line of  JApplet  methods (descriptions Fig. 25.14) public void init()   public void start()   public void paint( Graphics g )   public void stop()   public void destroy()
25.9 Defining and Allocating Arrays ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
25.10  Examples Using Arrays ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
InitArray.java
Program Output
InitArray.java
Program Output
InitArray.java (Part 1 of 2)
InitArray.java (Part 2 of 2) Program Output
SumArray.java Program Output
StudentPoll.java (Part 1 of 2)
StudentPoll.java (Part 2 of 2) Program Output
Histogram.java
Program Output
RollDie.java (Part 1 of 2)
RollDie.java (Part 2 of 2) Program Output
25.11 References and Reference Parameters ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
25.12  Multiple-Subscripted Arrays ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
25.12  Multiple-Subscripted Arrays (II) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],4 3 2 1
InitArray.java (Part 1 of 2)
InitArray.java (Part 2 of 2) Program Output
y given as a character which will be stored as it is in the char type variable c. Code of the program :  public class  conversion{    public static void  main(String[] args){      boolean  t =  true ;      byte  b = 2;      short  s = 100;      char  c = 'C';      int  i = 200;      long  l = 24000;      float  f = 3.14f;      double  d = 0.000000000000053;     String g = "string";     System.out.println("Value of all the variables like");     System.out.println("t = " + t );     System.out.println("b = " + b );     System.out.println("s = " + s );     System.out.println("c = " + c );     System.out.println("i = " + i );     System.out.println("l = " + l );     System.out.println("f = " + f );     System.out.println("d = " + d );     System.out.println("g = " + g );     System.out.println();     //Convert from boolean to byte.     b = ( byte )(t?1:0);     System.out.println("Value of b after conversion : " + b);     //Convert from boolean to short.     s = ( short )(t?1:0);     System.out.println("Value of s after conversion : " + s);     //Convert from boolean to int.     i = ( int )(t?1:0);     System.out.println("Value of i after conversion : " + i);     //Convert from boolean to char.     c = ( char )(t?'1':'0');     System.out.println("Value of c after conversion : " + c);     c = ( char )(t?1:0);     System.out.println("Value of c after conversion in unicode : " + c);     //Convert from boolean to long.     l = ( long )(t?1:0);     System.out.println("Value of l after conversion : " + l);     //Convert from boolean to float.     f = ( float )(t?1:0);     System.out.println("Value of f after conversion : " + f);     //Convert from boolean to double.     d = ( double )(t?1:0);     System.out.println("Value of d after conversion : " + d);     //Convert from boolean to String.     g = String.valueOf(t);     System.out.println("Value of g after conversion : " + g);     g = (String)(t?"1":"0");     System.out.println("Value of g after conversion : " + g);      int  sum = ( int )(b + i + l + d + f);     System.out.println("Value of sum after conversion : " + sum);   } }

More Related Content

What's hot

Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsPhilip Schwarz
 
Collection frame work
Collection  frame workCollection  frame work
Collection frame workRahul Kolluri
 
Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]Palak Sanghani
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshuSidd Singh
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With ScalaKnoldus Inc.
 
Comp 328 final guide
Comp 328 final guideComp 328 final guide
Comp 328 final guidekrtioplal
 
Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...Philip Schwarz
 
Multi dimensional array
Multi dimensional arrayMulti dimensional array
Multi dimensional arrayRajendran
 
Java Foundations: Objects and Classes
Java Foundations: Objects and ClassesJava Foundations: Objects and Classes
Java Foundations: Objects and ClassesSvetlin Nakov
 

What's hot (20)

Python array
Python arrayPython array
Python array
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
09. haskell Context
09. haskell Context09. haskell Context
09. haskell Context
 
Net (f#) array
Net (f#)  arrayNet (f#)  array
Net (f#) array
 
Array lecture
Array lectureArray lecture
Array lecture
 
Collection frame work
Collection  frame workCollection  frame work
Collection frame work
 
Quality Python Homework Help
Quality Python Homework HelpQuality Python Homework Help
Quality Python Homework Help
 
Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
1.2 matlab numerical data
1.2  matlab numerical data1.2  matlab numerical data
1.2 matlab numerical data
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshu
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
Comp 328 final guide
Comp 328 final guideComp 328 final guide
Comp 328 final guide
 
Arrays
ArraysArrays
Arrays
 
ScalaTrainings
ScalaTrainingsScalaTrainings
ScalaTrainings
 
Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...
 
Multi dimensional array
Multi dimensional arrayMulti dimensional array
Multi dimensional array
 
Java Foundations: Objects and Classes
Java Foundations: Objects and ClassesJava Foundations: Objects and Classes
Java Foundations: Objects and Classes
 
2D Plot Matlab
2D Plot Matlab2D Plot Matlab
2D Plot Matlab
 

Similar to Java Operators, Methods, Arrays

Csphtp1 06
Csphtp1 06Csphtp1 06
Csphtp1 06HUST
 
Assignment Java Programming 2
Assignment Java Programming 2Assignment Java Programming 2
Assignment Java Programming 2Kaela Johnson
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsMuhammadTalha436
 
Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Palak Sanghani
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Palak Sanghani
 
Csphtp1 07
Csphtp1 07Csphtp1 07
Csphtp1 07HUST
 
Microsoft dynamics ax 2012 development introduction part 2/3
Microsoft dynamics ax 2012 development introduction part 2/3Microsoft dynamics ax 2012 development introduction part 2/3
Microsoft dynamics ax 2012 development introduction part 2/3Ali Raza Zaidi
 
The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88Mahmoud Samir Fayed
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstractionIntro C# Book
 
08review (1)
08review (1)08review (1)
08review (1)IIUM
 

Similar to Java Operators, Methods, Arrays (20)

CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
06 procedures
06 procedures06 procedures
06 procedures
 
Csphtp1 06
Csphtp1 06Csphtp1 06
Csphtp1 06
 
Assignment Java Programming 2
Assignment Java Programming 2Assignment Java Programming 2
Assignment Java Programming 2
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
 
Csphtp1 07
Csphtp1 07Csphtp1 07
Csphtp1 07
 
Microsoft dynamics ax 2012 development introduction part 2/3
Microsoft dynamics ax 2012 development introduction part 2/3Microsoft dynamics ax 2012 development introduction part 2/3
Microsoft dynamics ax 2012 development introduction part 2/3
 
Java Generics
Java GenericsJava Generics
Java Generics
 
The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
 
CS2309 JAVA LAB MANUAL
CS2309 JAVA LAB MANUALCS2309 JAVA LAB MANUAL
CS2309 JAVA LAB MANUAL
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
 
08review (1)
08review (1)08review (1)
08review (1)
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 

Java Operators, Methods, Arrays

  • 1. Chapter 25 - Beyond C & C++: Operators, Methods, and Arrays in Java Outline 25.1 Introduction 25.2 Primitive Data Types and Keywords 25.3 Logical Operators 25.4 Method Definitions 25.5 Java API Packages 25.6 Random Number Generation 25.7 Example: A Game of Chance 25.8 Methods of Class JApplet 25.9 Defining and Allocating Arrays 25.10 Examples Using Arrays 25.11 References and Reference Parameters 25.12 Multiple-Subscripted Arrays
  • 2.
  • 3.
  • 4.
  • 5. 25.2 Primitive Data Types and Keywords (II)
  • 6.
  • 7.
  • 9.
  • 11.
  • 12.
  • 16.
  • 17.
  • 18.
  • 19.
  • 21. SquareInt.java (Part 2 of 2) Program Output
  • 22.
  • 23. 25.5 Java API Packages (II)
  • 24. 25.5 Java API Packages (III)
  • 25. 25.5 Java API Packages (IV)
  • 26. 25.5 Java API Packages (V)
  • 27. 25.5 Java API Packages (VI)
  • 28. 25.5 Java API Packages (VII)
  • 29. 25.5 Java API Packages (VIII)
  • 30.
  • 33. RollDie.java (Part 1 of 2) Program Output
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 45.
  • 46. 25.8 Methods of Class JApplet (II) First line of JApplet methods (descriptions Fig. 25.14) public void init() public void start() public void paint( Graphics g ) public void stop() public void destroy()
  • 47.
  • 48.
  • 54. InitArray.java (Part 2 of 2) Program Output
  • 57. StudentPoll.java (Part 2 of 2) Program Output
  • 61. RollDie.java (Part 2 of 2) Program Output
  • 62.
  • 63.
  • 64.
  • 66. InitArray.java (Part 2 of 2) Program Output
  • 67. y given as a character which will be stored as it is in the char type variable c. Code of the program : public class  conversion{    public static void  main(String[] args){      boolean  t =  true ;      byte  b = 2;      short  s = 100;      char  c = 'C';      int  i = 200;      long  l = 24000;      float  f = 3.14f;      double  d = 0.000000000000053;     String g = "string";     System.out.println("Value of all the variables like");     System.out.println("t = " + t );     System.out.println("b = " + b );     System.out.println("s = " + s );     System.out.println("c = " + c );     System.out.println("i = " + i );     System.out.println("l = " + l );     System.out.println("f = " + f );     System.out.println("d = " + d );     System.out.println("g = " + g );     System.out.println();     //Convert from boolean to byte.     b = ( byte )(t?1:0);     System.out.println("Value of b after conversion : " + b);     //Convert from boolean to short.     s = ( short )(t?1:0);     System.out.println("Value of s after conversion : " + s);     //Convert from boolean to int.     i = ( int )(t?1:0);     System.out.println("Value of i after conversion : " + i);     //Convert from boolean to char.     c = ( char )(t?'1':'0');     System.out.println("Value of c after conversion : " + c);     c = ( char )(t?1:0);     System.out.println("Value of c after conversion in unicode : " + c);     //Convert from boolean to long.     l = ( long )(t?1:0);     System.out.println("Value of l after conversion : " + l);     //Convert from boolean to float.     f = ( float )(t?1:0);     System.out.println("Value of f after conversion : " + f);     //Convert from boolean to double.     d = ( double )(t?1:0);     System.out.println("Value of d after conversion : " + d);     //Convert from boolean to String.     g = String.valueOf(t);     System.out.println("Value of g after conversion : " + g);     g = (String)(t?"1":"0");     System.out.println("Value of g after conversion : " + g);      int  sum = ( int )(b + i + l + d + f);     System.out.println("Value of sum after conversion : " + sum);   } }