0% found this document useful (0 votes)
27 views

Topic 2 - Java Programming Basics (Part 2)

This document discusses Java programming basics including control structures, arrays, and packages. It covers selection statements like if-else and switch-case, loop structures like for, while, and do-while loops. It also explains how to declare and initialize arrays of primitives and variable-sized arrays. Finally, it provides an overview of Java packages - how they organize classes into namespaces and avoid naming collisions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Topic 2 - Java Programming Basics (Part 2)

This document discusses Java programming basics including control structures, arrays, and packages. It covers selection statements like if-else and switch-case, loop structures like for, while, and do-while loops. It also explains how to declare and initialize arrays of primitives and variable-sized arrays. Finally, it provides an overview of Java packages - how they organize classes into namespaces and avoid naming collisions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

TOPIC 2: JAVA PROGRAMMING BASICS (PART 2)

CSC 435 OBJECT ORIENTED PROGRAMMING

➢ Control Structures
➢ Array of Primitives
➢ Packages
CHAPTER OUTLINE

 Structured language VS OOP language


 Introduction to JAVA Application
 Data Types – Primitives and Objects
 Control Structures
 Array of Primitives
 Packages
CONTROL STRUCTURES
CONTROL STRUCTURES

 Most of the programming languages use control structures to control the flow of a program.
 Types of control structures:
 Selection statement
 If-else
 Switch-case
 Loop (needs loop entry, loop test, iteration and termination)
 for loop
 while loop
 do-while loop (suitable for menu selection)
CONTROL STRUCTURE - SELECTION
1. One Way Selection

Syntax:
 if (expression)
 statement

 Expression referred to as decision maker.


 Statement referred to as action statement.
Example:

if (boolean expression) if (bmi < 18.5)


{ {
statement or block;
} System.out.println ("You are
underweight");
}
CONTROL STRUCTURE - SELECTION
if (boolean expression)
2. Two Way Selection {
Syntax: statement or block;
}
 if (expression) else
{
 statement1 statement or block;
}
 else
 statement2
if (bmi < 18.5)
 else statement must be paired with an if. {
System.out.println ("You are
underweight");
}
else
if (bmi >= 30)
{
System.out.println ("obesity");
}
CONTROL STRUCTURE - SELECTION
3. Multiple Selection
if (score >= 90)
System.out.println (“Grade is A”);
Syntax:
 if (expression1) else if (score >=80 )
System.out.println (“Grade is B”);
 statement1
 else else if (score >=70 )
System.out.println (“Grade is C”);
 if (expression2)
else if (score >=60 )
 statement2
 else System.out.println (“Grade is D”);

 statement3 else System.out.println (“Grade is F”);


CONTROL STRUCTURE - SELECTION

4. Switch Statement Example:

switch (month)
{
switch (expr) case 1: monthString = "January";
{ break;
case option1: statements; case 2: monthString = "February";
break; break;
case option2: statements; case 3: monthString = "March";
break; break;
default: statements; case 4: monthString = "April";
break; break;
}
default: monthString = "Invalid month";

}
System.out.println(monthString);
CONTROL STRUCTURE - LOOP
Why Loop is Needed?

❑Looping statements allow you to


execute blocks of statements repeatedly.

❑The Java programming language


supports three types of loop constructs:
• for
• while
• do while
CONTROL STRUCTURE - LOOP

1. For Loop
for (initialize; boolean testexpr; increment)
• Its primary purpose is to simplify the
{
writing of counter-controlled loops.
statements or block;
• For this reason, the for loop is typically
}
called a counted or indexed for loop.

Example:

for(int i=0; i<10; i++)


{
System.out.println(“Count is: “ + i);
}
C
CONTROL STRUCTURE - LOOP

2. While Loop
• Statements must change value of
expression to false.
• A loop that continues to execute
endlessly is called an infinite loop.
(expression is always true)

Example:
while (boolean)
{ while (count < 11)
statements or block; {
} System.out.println("Count is: " + count);
count++;
}
CONTROL STRUCTURE - LOOP

3. Do While Loop do
• Statements are executed first and then {
expression is evaluated. statements or block;
• Statements are executed at least once } while (boolean test);
and then continued if expression is
true.

Example:

do
{
System.out.println("Count is: " +
count);
count++;
} while (count < 11);
ARRAY OF PRIMITIVES
BASIC ARRAY

 In the Java programming language arrays are object.


 An array object contains a number of variables.
 The number of variables may be zero, in which case the array is said to be empty.
 If an array has n components, we say n is the length of the array; the components
of the array are referenced using integer indices from 0 to n - 1, inclusive.
BASIC ARRAY
 Array Declaration
<data type> [ ] <variable> //variation 1
<data type> <variable>[ ] //variation 2

 Array Creation
<variable> = new <data type> [ <size> ]

 Example

Variation 1 Variation 2
double[ ] rainfall; double rainfall [ ];
rainfall = new double[12]; rainfall = new double[12];

An array is like an object!


ARRAY PROCESSING - SAMPLE 1
Scanner scanner = new Scanner(System.in);
double[] rainfall = new double[12]; The public constant
length returns the
capacity of an array.
double annualAverage,
sum = 0.0;

for (int i = 0; i < rainfall.length; i++) {


System.out.print("Rainfall for month " + (i+1));
rainfall[i] = scanner.nextDouble( );
sum += rainfall[i];
}

annualAverage = sum / rainfall.length;


ARRAY INITIALIZATION

 Like other data types, it is possible to declare and initialize an array at the same time.

int[] number = { 2, 4, 6, 8 };

double[] samplingData = { 2.443, 8.99, 12.3, 45.009, 18.2,


9.00, 3.123, 22.084, 18.08 };

String[] monthName = { "January", "February", "March",


"April", "May", "June", "July",
"August", "September", "October",
"November", "December" };

number.length 4
samplingData.length 9
monthName.length 12
VARIABLE – SIZE DECLARATION

 In Java, we are not limited to a fixed-size array declaration.


 The following code prompts the user for the size of an array and declares an array of designated size.

Scanner scanner = new Scanner(System.in);


int size;
int[] number;

System.out.print("Size of an array:"));
size= scanner.nextInt( );

number = new int[size];


PACKAGES
JAVA PACKAGE

 A Java package is a mechanism for organizing Java classes into


namespaces (like a folder).
 Java packages can be stored in compressed files called JAR files.
 Programmers typically use packages to organize classes belonging to the
same category or providing similar functionality.
 Packaging also help us to avoid class name collision when we use the
same class name as that of others.
JAVA PACKAGE

 files in one directory (or package) would have different functionality from those of
another directory
 For example,
 files in java.io package do something related to I/O, but
 files in java.net package give us the way to deal with the Network.
 In GUI applications, it's quite common for us to see a directory with a name "ui" (user interface),
meaning that this directory keeps files related to the presentation part of the application.
TO CREATE JAVA PACKAGE

 Suppose we have a file called HelloWorld.java, and we want to put this file in a package
world.
 Example:
// only comment can be here
package world;
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
 create a directory world and put our HelloWorld.java into it.
JAVA BASIC INPUT/OUTPUT (EXTRA)
STANDARD OUTPUT
 Using System.out, we can output multiple lines of text to the standard
output window.
 We use the print method to output a value to the standard output
window.
 Example:

 System.out.print( “Hello, Dr. Caffeine.” );


THE PRINTLN METHOD
• We use println instead of print to skip a line.

int x = 123, y = x + x;
System.out.println( "Hello, Dr. Caffeine.“ );
System.out.print( " x = “ );
System.out.println( x );
System.out.print( " x + x = “ );
System.out.println( y );
System.out.println( " THE END“ );
STANDARD INPUT

• The technique of using System.in to input data is called standard input.


• We can only input a single byte using System.in directly.
• To input primitives data values, we use the Scanner class (available from Java
version 1.5).

Scanner scanner;

scanner = new Scanner(System.in);

int num = scanner.nextInt();


COMMON SCANNER METHODS

Method Example

nextByte( ) byte b = scanner.nextByte( );


nextDouble( ) double d = scanner.nextDouble( );
nextFloat( ) float f = scanner.nextFloat( );
nextInt( ) int i = scanner.nextInt( );
nextLong( ) long l = scanner.nextLong( );
nextShort( ) short s = scanner.nextShort( );
next() String str = scanner.next();

You might also like