Professional Documents
Culture Documents
AND UML
CSC 417
By
Dr. Salamudeen Alhassan
March 9, 2022
Introduction to Java
Content Overview
Java Strings
Java Arrays
Files and I/O
Exceptions
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 2 / 27
The String class
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 3 / 27
The String class cont.
Constructing a String
String message = "Welcome to Java!"
String message = new String("Welcome to Java!“);
char[] sMessage = { ’W’, ’e’, ’l’, ’c’, ’o’, ’m’, ’e’, ’ ’, ’t’, ’o’, ’ ’,
’J’, ’a’, ’v’, ’a’, ’!’ };
String s = new String(sMessage);
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 4 / 27
The String class cont.
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 5 / 27
The String class cont.
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 6 / 27
The StringBuffer Class
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 7 / 27
The StringBuffer Class
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 8 / 27
Listing: StringBuffer Example
StringBuffer strBuf = new StringBuffer () ;
strBuf . append (" Welcome ") ;
strBuf . append ( ’ ’) ;
strBuf . append (" to ") ;
strBuf . append ( ’ ’) ;
strBuf . append (" Java ") ;
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 9 / 27
Java Arrays
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 9 / 27
Java Arrays Cont.
Creating Arrays
You can create an array by using the new keyword.
arrayName = new datatype[arraySize];
e.g. myList = new double[10];
Or in on step
datatype[] arrayname = new datatype[arraySize];
e.g. double[] myList = new double[10];
myList[0] references the first element in the array.
myList[9] references the last element in the array.
The Length of Arrays
arrayname.length;
NB: Once an array is created, its size is fixed. It cannot be
changed.
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 10 / 27
Java Arrays Cont.
Initializing Arrays
Using a loop
for (int i = 0; i < myList.length; i++)
myList[i] = i;
Declaring, creating, initializing in one step
double[] myList = 1.9, 2.9, 3.4, 3.5;
NB: This syntax must be in one statement.
Or
double[] myList = new double[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 12 / 27
Listing: Array Example 1
public class months {
public static void main ( String [] args ) {
String [] months = {" Jan " , " Feb " , " Mar " , " Apr " , " May " , " Jun " , "
July " , " Aug " , " Sep " , " Oct " , " Nov " , " Dec "};
int [] monthsDay = {31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 ,
31};
System . out . println ("\ nNo . of months in a year is " + months .
length ) ;
for ( int i = 0; i < months . length ; i ++) {
System . out . println ( months [ i ] + " has " + monthsDay [ i ] + " days
") ;
}
}
}
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 13 / 27
Listing: Array Example 2
public class months {
public static void main ( String [] args ) {
String [] weekdays = {" Sunday " , " Monday " , " Tuesday " , " Wednesday " ,
" Thursday " , " Friday " , " Saturday "};
System . out . println (" No . of days in a week is " + weekdays . length
);
System . out . println (" The days of the week are :") ;
for ( String day : weekdays ) {
System . out . println ( day ) ;
}
}
}
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 13 / 27
Java Arrays Cont.
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 13 / 27
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 13 / 27
Passing Arrays to Methods
double [][] x ;
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 13 / 27
Multidimensional Arrays
Is equivalent to :
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 13 / 27
Returning an Arrays from a Methods
java.io package contains all the classes needed for all input and
output.
A stream is a sequence of data.
Kinds of streams
InPutStream for reading data from a source.
OutPutStream for writing data to a destination.
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 14 / 27
Files and I/O Cont.
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 15 / 27
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 15 / 27
Listing: Byte Streams
import java . io .*;
public class CopyFile {// this program copies the content of a
input file to an output file .
public static void main ( String args []) throws IOException {
F il eI np u tS tr ea m fin = null ; // input file
F i l e O u t p u t S t re a m fout = null ; // output file
try {
fin = new Fi l eI np ut S tr ea m (" sampleinput . txt ") ;
fout = new F i l e O u t p u t S t r e am (" output . txt ") ;
int c ;
while (( c = fin . read () ) != -1) {
fout . write ( c ) ;
}
}
finally {
if ( fin != null ) {
fin . close () ;
}
if ( fout != null ) {
fout . close () ;
}
}
}
} ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 15 / 27
Files and I/O Cont.
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 16 / 27
Files and I/O Cont.
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 17 / 27
Listing: Standard Streams
import java . io .*;
public class ReadConsole {
try {
cin = new I n p u t S t r e a m R e a d e r ( System . in ) ;
System . out . println (" Enter characters , ’q ’ to quit .") ;
char c ;
do {
c = ( char ) cin . read () ;
System . out . print ( c ) ;
} while ( c != ’q ’) ;
} finally {
if ( cin != null ) {
cin . close () ;
}
}
}
}
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 18 / 27
Files and I/O Cont.
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 18 / 27
Exceptions
Java Exceptions
An Exception is a problem that arises during the execution of a
program.
Java throws an exception in such cases.
A program will terminate abnormally when an exception occurs
and not handled appropriately.
Some scenarios which can lead to exceptions.
Coding errors made by the programmer
A user has entered an invalid data.
A file that needs to be opened cannot be found.
A network connection has been lost in the middle of
communications or the JVM has run out of memory.
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 19 / 27
Exceptions
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 20 / 27
Listing: Checked exception
import java . io . File ;
import java . io . FileReader ;
public class Ch e ck ed _E xa m pl e {
// F i l e N o t F o u n d E x c e p t i o n will occur
public static void main ( String args []) {
File file = new File (" D :// myfile . txt ") ;
FileReader fr = new FileReader ( file ) ;
}
}
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 21 / 27
Exceptions
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 21 / 27
Listing: UnChecked exception
public class U n c h e c k e d _ E x a m p l e {
// A r r a y I n d e x O u t O f B o u n d s E x c e p t i o n e x c e p t i o n
public static void main ( String args []) {
String [] weekdays = {" Sunday " , " Monday " , " Tuesday " , " Wednesday " ,
" Thursday " , " Friday " , " Saturday "};
System . out . println ( weekdays [7]) ;
}
}
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 22 / 27
Exceptions
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 22 / 27
Exceptions
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 23 / 27
Exceptions
Catching Exceptions
Catch exceptions by using the try / catch block.
Code within a try/catch block is referred to as protected code
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 24 / 27
Try / catch syntax
try {
// Protected code
}
catch ( ExceptionName e1 ) {
// Catch block
}
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 25 / 27
Try / catch Example
public class Unchecked_Example {
// A r r a y I n d e x O u tOf Bou nds Exc ept ion exc ept ion
public static void main ( String args []) {
String [] weekdays = {" Sunday " , " Monday " , "
Tuesday " , " Wednesday " , " Thursday " , "
Friday " , " Saturday "};
try {
System . out . println ( weekdays [7]) ;
}
catch { A rr a yI ndexOutOfBoundsException e }{
System . out . println (" Exception thrown
:" + e ) ;
}
}
}
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 25 / 27
Exceptions
Finally
The finally statement lets you execute code, after try...catch,
regardless of the result
A finally block appears at the end of the catch blocks.
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 25 / 27
Try / catch finally syntax
try {
// Protected code
} catch ( ExceptionType1 e1 ) {
// Catch block
} catch ( ExceptionType2 e2 ) {
// Catch block
} catch ( ExceptionType3 e3 ) {
// Catch block
} finally {
// The finally block always executes .
}
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 26 / 27
Try Question
Try Question 1
Write a program in Java that reads student scores (int) from the
keyboard, get the best score, and then assign grades based on the
following scheme:
Grade is A if score is >= best–10;
Grade is B if score is >= best–20;
Grade is C if score is >= best–30;
Grade is D if score is >= best–40;
Grade is F otherwise.
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 26 / 27
Try Question
Try Question 2
Write a program in Java that reads in two square matrices A and B.
Your program should be able to add these matrices in to a matrix C
and display the results.
ByDr. Salamudeen Alhassan ADVANCED PROGRAMMING WITH JAVA AND UML March 9, 2022 27 / 27