Java Tutorial For Beginner
Java Tutorial For Beginner
Section 2
Lecture 5 : A Hello World Program
System.out.println("Hello World!");
System.out.println(myNumber);
System.out.println(myShort);
System.out.println(myLong);
System.out.println(myDouble);
System.out.println(myFloat);
System.out.println(myChar);
System.out.println(myBoolean);
System.out.println(myByte);
int myInt = 7;
System.out.println(greeting);
int value = 0;
value = value + 1;
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Lecture 9: For Loops
public class Application {
public static void main(String[] args) {
Lecture 10: If
public class Application {
public static void main(String[] args) {
while(true) {
System.out.println("Looping: " + loop);
if(loop == 3) {
break;
}
loop++;
System.out.println("Running");
}
}
}
true
true
true
false
Looping: 0
Running
Looping: 1
Running
Looping: 2
Running
Looping: 3
/*
System.out.println("Enter a number: ");
int value = scanner.nextInt();
while(value != 5) {
System.out.println("Enter a number: ");
value = scanner.nextInt();
}
*/
int value = 0;
do {
System.out.println("Enter a number: ");
value = scanner.nextInt();
}
while(value != 5);
System.out.println("Got 5!");
}
}
switch (text) {
case "start":
System.out.println("Machine started!");
break;
case "stop":
System.out.println("Machine stopped.");
break;
default:
}
}
int value = 7;
int[] values;
values = new int[3];
System.out.println(values[0]);
values[0] = 10;
values[1] = 20;
values[2] = 30;
System.out.println(values[0]);
System.out.println(values[1]);
System.out.println(values[2]);
0
10
20
30
10
20
30
5
6
7
System.out.println(text);
you
apple
banana
pear
kiwi
null
null
// 1D array
int[] values = {3, 5, 2343};
System.out.println(texts[0][1]);
System.out.println();
}
System.out.println(words[0][1]);
}
}
2343
4
2343
Hello there
3 5 2343
2 4
1 2 3 4
null
hi there
class Person {
// 1. Data
// 2. Subroutines (methods)
}
System.out.println(person1.name);
Joe Bloggs
// 1. Data
// 2. Subroutines (methods)
void speak() {
for(int i=0; i<3; i++) {
System.out.println("My name is: " + name + " and I am " + age + " years o
ld ");
}
}
void sayHello() {
System.out.println("Hello there!");
}
}
System.out.println(person1.name);
}
}
void speak() {
System.out.println("My name is: " + name);
}
int calculateYearsToRetirement() {
int yearsLeft = 65 - age;
return yearsLeft;
}
int getAge() {
return age;
}
String getName() {
return name;
}
}
person1.name = "Joe";
person1.age = 25;
// person1.speak();
sam.move("West", 12.2);
Hi I'm Sam.
Jumping: 7
Moving 12.2 metres in direction West
Hello there.
Jumping: 14
//frog1.name = "Bertie";
//frog1.age = 1;
frog1.setName("Bertie");
frog1.setAge(1);
System.out.println(frog1.getName());
}
Bertie
public Machine() {
this("Arnie", 0);
System.out.println("Constructor running!");
}
public Thing() {
id = count;
count++;
}
Thing.showInfo();
thing1.name = "Bob";
thing2.name = "Sue";
thing1.showName();
thing2.showName();
System.out.println(Math.PI);
System.out.println(Thing.LUCKY_NUMBER);
}
I am a thing
Before creating objects, count is: 0
After creating objects, count is: 2
Object id: 0, I am a thing: Bob
Object id: 1, I am a thing: Sue
3.141592653589793
7
// Inefficient
String info = "";
System.out.println(info);
// More efficient.
StringBuilder sb = new StringBuilder("");
sb.append("My name is Sue.");
sb.append(" ");
sb.append("I am a lion tamer.");
System.out.println(sb.toString());
System.out.println(s.toString());
// Formatting integers
// %-10d means: output an integer in a space ten characters wide,
// padding with space and left-aligning (%10d would right-align)
System.out.printf("Total cost %-10d; quantity is %dn", 5, 120);
// You can also use the String.format() method if you want to retrieve
// a formatted string.
String formatted = String.format("This is a floating-point value: %.3f", 5.12
345);
System.out.println(formatted);
return sb.toString();
*/
}
}
System.out.println(frog1);
System.out.println(frog2);
}
}
7 : Freddy
5 : Roger
mach1.start();
mach1.stop();
Car car1 = new Car();
car1.start();
car1.wipeWindShield();
car1.showInfo();
car1.stop();
Machine.java:
Car.java:
public class Car extends Machine {
@Override
public void start() {
System.out.println("Car started");
}
Machine started.
Machine stopped.
Car started
Wiping windshield
Car name: Machine Type 1
Machine stopped.
Fish.java:
package ocean;
Algae.java:
package ocean.plants;
}
Seaweed.java:
package ocean.plants;
Aquarium.java:
package com.caveofprogramming.oceangame;
System.out.println();
outputInfo(mach1);
outputInfo(person1);
}
Machine.java:
private int id = 7;
Person.java:
@Override
public void showInfo() {
System.out.println("Person name is: " + name);
}
}
Info.java:
public interface Info {
public void showInfo();
}
IStartable.java:
Machine started.
Hello there.
Machine ID is: 7
Person name is: Bob
Machine ID is: 7
Person name is: Bob
import world.Plant;
/*
* private --- only within same class
* public --- from anywhere
* protected -- same class, subclass, and same package
* no modifier -- same package only
*/
System.out.println(plant.name);
System.out.println(plant.ID);
// Won't work; App and Plant in different packages, height has package-level
visibility.
//System.out.println(plant.height);
Grass.java:
import world.Plant;
// Won't work --- Grass not in same package as plant, even though it's a subc
lass
// System.out.println(this.height);
}
}
Field.java:
package world;
public Field() {
Oak.java:
package world;
public Oak() {
Plant.java:
package world;
class Something {
int height;
public Plant() {
this.name = "Freddy";
this.type = "plant";
this.size = "medium";
this.height = 8;
}
}
// The type of the reference decided what methods you can actually call;
// we need a Tree-type reference to call tree-specific methods.
tree.shedLeaves();
Plant.java:
@Override
public void grow() {
System.out.println("Tree growing");
}
class Plant {
return data;
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(Byte.MAX_VALUE);
intValue = (int)longValue;
System.out.println(intValue);
doubleValue = intValue;
System.out.println(doubleValue);
intValue = (int)floatValue;
System.out.println(intValue);
class Machine {
System.out.println("Machine started.");
System.out.println("Camera started.");
machine1.start();
camera1.start();
camera1.snap();
// Upcasting
machine2.start();
// error: machine2.snap();
// Downcasting
camera2.start();
camera2.snap();
// camera3.start();
// camera3.snap();
import java.util.ArrayList;
import java.util.HashMap;
class Animal {
list.add("apple");
list.add("banana");
list.add("orange");
String fruit = (String)list.get(1);
System.out.println(fruit);
strings.add("cat");
strings.add("dog");
strings.add("alligator");
System.out.println(animal);
class Machine {
@Override
public String toString() {
return "I am a machine";
}
list2.add(new Camera());
list2.add(new Camera());
showList(list2);
showList2(list1);
showList3(list1);
}
class Machine {
public void start() {
System.out.println("Starting machine ...");
}
}
interface Plant {
public void grow();
}
machine1.start();
// This is equivalent to creating a class that "implements"
// the Plant interface
Plant plant1 = new Plant() {
@Override
public void grow() {
System.out.println("Plant growing");
}
};
plant1.grow();
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
in.nextLine();
int count = 2;
while(in.hasNextLine()) {
String line = in.nextLine();
in.close();
}
package demo1;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
demo2/App.java:
package demo2;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
try {
FileReader fr = new FileReader(file);
System.out.println("Finished.");
}
demo3/App.java:
package demo3;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
}
}
Test.java:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
}
}
System.out.println(text.length());
try {
System.out.println(texts[3]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.toString());
}
}
}
car1.run();
Machine.java:
Camera.java:
@Override
public void start() {
System.out.println("Starting camera.");
}
@Override
public void doStuff() {
System.out.println("Taking a photo");
@Override
public void shutdown() {
System.out.println("Shutting down the camera.");
Car.java:
public class Car extends Machine {
@Override
public void start() {
System.out.println("Starting ignition...");
@Override
public void doStuff() {
System.out.println("Driving...");
}
@Override
public void shutdown() {
System.out.println("Switch off ignition.");
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
BufferedReader br = null;
try {
br = new BufferedReader(fr);
String line;
System.out.println(line);
} catch (FileNotFoundException e) {
} catch (IOException e) {
finally {
try {
br.close();
} catch (IOException e) {
catch(NullPointerException ex) {
@Override
public void close() throws Exception {
System.out.println("Closing!");
throw new Exception("oh no!");
}
}
public class App {
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
App2.java:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
Robot.java:
switch(animal) {
case CAT:
System.out.println("Cat");
break;
case DOG:
System.out.println("Dog");
break;
case MOUSE:
break;
default:
break;
System.out.println(Animal.DOG);
System.out.println("Enum name as a string: " + Animal.DOG.name());
System.out.println(Animal.DOG.getClass());
System.out.println(Animal.DOG instanceof Enum);
System.out.println(Animal.MOUSE.getName());
System.out.println(animal2);
}
Animal.java:
Animal(String name) {
this.name = name;
}
System.out.println(factorial(5));
}
if(value == 1) {
return 1;
}
import java.io.Serializable;
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
}
// www.caveofprogramming.com
WriteObjects.java:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class WriteObjects {
System.out.println(mike);
System.out.println(sue);
os.writeObject(mike);
os.writeObject(sue);
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
os.close();
System.out.println(person1);
System.out.println(person2);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
import java.io.Serializable;
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
}
// www.caveofprogramming.com
WriteObjects.java:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Person[] people = {new Person(1, "Sue"), new Person(99, "Mike"), new Person(7
, "Bob")};
// Write arraylist
os.writeObject(peopleList);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
WriteObjects.java:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ReadObjects {
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
import java.io.Serializable;
public Person() {
System.out.println("Default constructor");
}
System.out.println("Two-argument constructor");
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "] Count is: " + count;
}
}
// www.caveofprogramming.com
WriteObjects.java:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ReadObjects.java:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}