Object Oriented Programming: Week 2
Object Oriented Programming: Week 2
Week 2
Comments on Homework 1
• It is not possible to miss a homework. To pass
this course, you need an average of 75%. If
you have a good reason not to do your work
on time, then make sure I know it.
• Homeworks need to be named with your
name so that I don’t have to search through
my emails. Call your files something like
“ChrisPayneHmwk1.java”
• Use two-space formatting and camel format.
Primitive Variables
Defining Variables
All variable names must be defined in advance of use.
int thisInteger;
boolean finished = false;
double size = 56.2345;
byte b = 34;
char = ‘@’;
float valueFloat;
short s = 334;
long veryBig = 13452678;
String s1, s2 = “This is a string”, s3;
System.in.read
public class StringIn1{
public static void main(String[] args) throws Exception{
int i = System.in.read();
System.out.println(i);
}
or
Type promotion
Type casting
byte b;
char c = (char) b;
int anInteger = (int) aDouble;
Selection statements – two forms
if (logical statement) {
.................
}
if (logical statement) {
.................
}
else
{
.................
}
The switch statement
switch (someVariable){
case (value1):
/* statement sequence ending with */
break;
................... // multiple case and break statements
case (valueN):
/* statement sequence ending with */
break;
try {
/*successful operation code*/
}
catch(Exception sE){
/*exception handling code*/
}
2. do {
..........
}
while (logical statement is true)
3. for(initialization,condition,iteration) {
..........
}
Arithmetic and Logical Operators
Arithmetic operators
import java.io.InputStreamReader;
import java.io.Buffered Reader;
Alternatively
import java.io.*;
Input and double a number – Strings and
wrappers
import java.lang.*;
import java.io.*;
public class ManipulatingNumbers {
public static void main(String[] args) {
String sNumber,outString;
int twiceNumber;
try{
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader keyboard = new BufferedReader(reader);
Input and double a number – Strings and
wrappers (cont.)
System.out.print(“Enter a number: );
sNumber = keyboard.readLine();
twiceNumber = 2*Integer.parseInt(sNumber);
outString = Integer.toString(twiceNumber);
System.out.println();
System.out.println (“The number doubled is : “ + outString);
}
catch(IOException e) {
System.exit(1);
}
}
}
Constructors and static variables
• If all the useful part of the program is contained within the main()
method, then the global variables may also be defined inside main().
• We must trap the exception in case the input format is not recognised as
in a double form.
• Character
• Boolean
• Byte
• Short
• Integer
• Long
• Float
• Double
The Wrapper Classes
Number formatting
import java.text.*;
public class NumberFormatting {
public static void main(String[] args) {
double d1 = 1.23456;
NumberFormat form2D =
NumberFormat.getNumberInstance();
form2D.setMaximumFractionDigits(2);
form2D.setMinimumFractionDigits(2);
System.out.println(form2D.format(d1));
}
}
}
The NumberFormat class
Using the command line String arguments
import java.lang.*;
import java.io.*;
public class StringMethods {
public static void main(String[] args) {
String inString;
String outString = ““;
int stringLength;
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader keyboard = new BufferedReader(reader);
try{
System.out.println(“Enter a long string more than 20 characters long...”);
inString = keyboard.readLine();
stringLength = inString.length();
System.out.println(“The string”s length = “ + stringLength);
System.out.println(“Extract a substring “ + “of characters 12 to 24 ...”);
outString = outString + inString.substring(12,24);
A String application (Part 2)
System.out.println(outString);
outString = ““;
System.out.println(“The odd numbered characters” + “ in the string are : - “);
for (int i = 0; i < stringLength; i = i + 2){
outString = outString + inString.charAt(i);
}
System.out.println(outString);
System.out.println( “The ASCII values of the”+“ last six characters in the string...”);
for (int i = stringLength-6; i< stringLength; i++){
System.out.print((byte) inString.charAt(i) + “ “);
}
}
catch(IOException iE){....}
}
}
Using the String class