






























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to the java programming language, covering its background, the imperative programming style, data types, declarations, selection statements, methods and method calls, dynamic and static memory allocation, defining classes, inheritance, and the java api. It covers key concepts such as objects, classes, interfaces, and inheritance, as well as the syntax and structure of java programs. Intended to serve as a comprehensive overview of the fundamental aspects of java for students or learners new to the language.
Typology: Lab Reports
1 / 38
This page cannot be seen from the preview
Don't miss anything!































Java developed in the 1990s by Sun Microsystems:
Note that we are going to use Java 5 or later. Although most of what we shall study here applies to earlier versions of Java too, the following features denitely will not work in earlier versions of Java:
Some Java-Python differences!
2 Object-orientation and programming-in-the-large
Software crisis Cost of software typically by far most expensive part of any system Complexity grows Complexity of program superlinear in size of pro- gram:
Modularity and Code re-use seen as possible solutions Program is made up of a number of units called classes. Good programmers need to:
3 Basic Components of a Java Program
A Java program must contain one public class.
When the program starts to run, the main methods starts executing. It can call other methods. Slightly different rules apply to applets. Our rst example
public class TrivialApplication { public static void main(String args[]) { System.out.println(”Hello World!”); } }
4 The imperative style
The underlying model of computation of Java is the imperative program- ming style:
int i, j; float f; .. .. i = i+10; j = Math.abs(j-2*i); i = Math.ceil(f)+i; .. ..
5 Variables
The type of the variable not only describes the values the variable can take, but operations that can be performed. Need to be type-consistent.
int a, apple; int j33, banana; int cherry = 3, data = 0, k;
a = 3; apple = 2; banana = 1; a = a + 1; apple = apple + a;
int i, j; char j, answer; long rainfall; float radius, r3;
By convention, variable declarations given rst:
Other modiers possible: e.g. static, nal etc. We’ll look at modiers later Variables have four important features:
7 Flow of control
When a program starts to run, the main method starts executing. Statements are executed sequentially in order except when altered by:
A statements in a Java program can be a:
The most common form of the assignment is
varname = value;
On the left is a variable, on the right a value that is type compatible:
boolean b; ... x = b? y : z; <--- if (b) x=y; else x=z; ...
This is not a complete list of all the operators. For example, there are integer bit-wise operators for doing bit-wise and, or and complementation. For example 325&7 represented in binary is 0101000101&0000000111 which is 101. We have done a bit-wise and of the bits in the number.
8 Input
Scanner To read an integer from the keyboard into a variable x:
import java.util.Scanner; public class Test{ public static void main(String args[]){ Scanner in = new Scanner(System.in); int x = in.nextInt(); } }
The Scanner class provides a number of useful functions to read input:
Note that there are some complications involved, so in a lot of cases it makes sense to use nextLine() wherever possible. This can be accom- plished by converting the returned String into the data type required. For instance, if you needed to read in an int value, it could be done in the following way:
Scanner in = new Scanner(System.in); String r = in.nextLine(); int val = Integer.parseInt(r);
9 Selection
selection if The if statement is the most important method of selection: General form 1
if (condition) statement;
General form 2
if (condition) statement; else statement;
The switch statement is the other Java selection method:
switch (value) { case v1 : sequence-of-statements; case v2 : sequence-of-statements;
... case vn : sequence-of-statements; default : sequence-of-statements; };
Variable value is evaluated.
switch (day) { case 0 : rate = 2.0; break; case 1 : rate = 0.8; break; case 2 : rate = 1.0; break; case 3 : case 4 : rate = 1.2; break; case 5 : case 6 : rate = 1.8; break; }
Using the switch statement:
The switch statement is very useful, but used too often can lead to complex code:
These latter approaches are more compact, often more efcient, easier to read and to maintain.
10 Loops
Three loops in Java, while , for , and do.
General form of the while loop is:
while (cond) statement
Evaluate the condition; if it’s true the statement is executed and then we repeat...
General format is
for (init; cond; change) statement;
Semantics are:
In 1.5 there is a construct analogous to Python’s for statement. We’ll look at it after doing arrays.
for (i = 1; i <= 10; i++) { n = n + i − 3 ∗ n; System.out.println (i + ′′^ computer and its mouse went to mow a meadow” ); };
for (i = 10; i >= 1; i--)...
for (i = 0; i < 10; i++)...
Real computer scientists count from zero!!!! Example
static void compfn () { int x; double y1, y2; for (x=0; x<100; x=x+10) { y1 = 3.1xx - 38x + 15; y2 = 2xx + 2x + 1; System.out.print("x = "+x+"; y1= "+y1+"; ‘‘+ "y2="+y2+"; "); System.out.println(y1>y2?"+":"-"); } }
Similar to the while loop except the condition testing is done at the end.
do {
... } while (cond); - Can use break and cont; - Take care in indentation.
Any loop can be coded as any of the three!
11 Reference/Complex data types and references
The use of references is one of the basic features of the semantics of Java. For primitive types, when a variable is declared,
Why do a and b end up with different values?
String s1, s2; boolean a, b;
s1 = new String (”Wits”); s2 = new String (”Wits”);
a = s1.equals(s2); b = s1 == s2;
An array is an homogeneous collection of objects, the individual elements of which can be accessed by index.
Type array name[]; array name = new Type[n];
int rainfall[], sum; rainfall = new int [12]; sum = 0; for (int m = 0; m < 12; m + +) {
rainfall[m] = in.nextInt(); sum+ = rainfall[m]; }
int rainfall[][]; rainfall = new int [12][31];
For more complex objects, may need to take care of memory allocation:
Animal pets[]; String d; pets = new Animal[12]; for (int m = 0; m < 12; m + +) { d = in.nextInt(); pets[m] = new Animal(d); }
There is a form of the for loop analogous to the for loop in Python.
for(variable : collection) statement
Note: