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

2 - Introduction To Java

Uploaded by

keyk1098
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

2 - Introduction To Java

Uploaded by

keyk1098
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

COMPX201

Data Structures
& Algorithms
Introduction to Java
Overview

▸ Data types
▸ Terminal vs. IDE
▸ Passing Arguments
▸ Object-Oriented Java
▸ C# vs. Java Examples
Java

▸ Java is a statically-typed programming


language.
▸ Object-oriented.
▹ Encapsulation.
▹ Abstraction.
▹ Inheritance.
▹ Polymorphism.
▸ Imperative.
▸ Functional (using streams).
▸ Open-source.
Data Types

▸ Java has primitive and non-primitive data types.


▸ Being statically-typed, variables are declared in
much the same way as C#:
int number = 10;
String test = “Hello World”;
boolean isLightOn = false;
Primitive Data Types

W3Schools. (n.d). Java Data Types.


https://www.w3schools.com/java/java_data_types.asp
Recall from COMPX102

Primitive Data Types

▸ A value type holds a data value within


its own memory space.
i
e.g. int i = 10;

0x283892

10

RAM
Non-primitive data types

▸ Strings, Arrays, Classes, Interfaces etc.


▸ Can be created by the programmer.
▸ Can be null.
▸ Starts with uppercase letter.
▸ Sometimes referred to as reference
types (remember passing by reference).
▸ All the same size.
Recall from COMPX102

Non-primitive Data Types

▸ A reference type stores the address of where a value


is being stored i.e. it contains a pointer to the
location that holds the data.
e.g. String hello = “Hello World!”;
hello

0x858594 0x383822

0x383822 Hello World!

RAM
IDE vs. Terminal

Terminal and editor for programming e.g.


Linux Terminal with Bash Shell and Text
Editor.
IDE vs. Terminal

Integrated Development Environment (IDE)


e.g. Visual Studio.
IDE vs. Terminal

▸ Should be using Linux with Terminal


approach for this paper i.e. it will be
easier to take this approach.
▸ If you don’t have a Linux machine you
can ssh into the Linux machines in
Hamilton.
▸ Alternatively you can use an IDE, I would
recommend Eclipse
(https://www.eclipse.org/).
Passing Arguments

▸ One thing we haven’t used before is the ability to


pass arguments to our program from the
command line.
▸ Passing arguments allows us to take user input
and change program behaviour based on that
input.
▸ For example:
java PrintName Bob Kevin Stuart
java SortItems 1000
Passing Arguments

public class PrintName {


public static void main(String [] args){
System.out.println("The names are:");
for(int i = 0; i < args.length; i++){
System.out.println(args[i]);
}
}
}
Object-Oriented Java: Encapsulation
public class Pet {
private String favouriteToy;
public String getFavouriteToy() {
return favouriteToy;
}
public void setFavouriteToy(String toy) {
this.favouriteToy = toy;
}
}

public class Main {


public static void main(String[] args) {
Pet pet = new Pet();
pet.favouriteToy = “Ball”;
}
}
Object-Oriented Java: Encapsulation
public class Pet {
private String favouriteToy;
public String getFavouriteToy() {
return favouriteToy;
}
public void setFavouriteToy(String toy) {
this.favouriteToy = toy;
}
}

public class Main {


public static void main(String[] args) {
Pet pet = new Pet();
pet.favouriteToy = “Ball”;
}
}
Object-Oriented Java: Encapsulation
public class Pet {
private String favouriteToy;
public String getFavouriteToy() {
return favouriteToy;
}
public void setFavouriteToy(String toy) {
this.favouriteToy = toy;
}
}

public class Main {


public static void main(String[] args) {
Pet pet = new Pet();
pet.setFavouriteToy(“Ball”);
}
}
Object-Oriented Java: Inheritance
public class Pet {
private String favouriteToy;
public String getFavouriteToy() {
return favouriteToy;
}
public void setFavouriteToy(String toy) {
this.favouriteToy = toy;
}
}

public class Dog extends Pet {


private String type;

public String bark(){


return “Bark!”;
}
Object-Oriented Java:
Inheritance

▸ Remember that protected keyword can be used for


variables that are accessible by the class and those
that inherit from it.
e.g. protected String favouriteFood;
▸ In Java you can also use the keyword final to prevent
other classes from inheriting from that class.
e.g. final class Pet { … }

Note: like C# Java does not support multiple inheritance.


Object-Oriented Java:
Polymorphism
public class Pet {
public String sound(){
return “This Pet makes a sound”;
}
}

public class Dog extends Pet { public class Pig extends Pet {
public String sound(){ public String sound(){
return “Bark!”; return “Oink!”;
} }
} }
Object-Oriented Java:
Polymorphism

▸ Useful to have collections of objects of same type


e.g. a list of Pets that includes Dog and Pig
objects.
▸ Remember the direction of Polymorphism (a dog
is a pet but a pet is NOT a dog).
Dog d = new Pet();
Main.java:14: error: incompatible types: Pet
cannot be converted to Dog
Dog d = new Pet();
^
1 error
Object-Oriented Java:
Polymorphism
public class Pet {
public String sound(){
return “This Pet makes a sound”;
}
public String eat(){
Return “Yum”;
}
}

public class Dog extends Pet { public class Pig extends Pet {
public String sound(){ public String sound(){
return “Bark!”; return “Oink!”;
} }
} }
Object Oriented Java:
Abstraction

▸ Abstract class cannot be instantiated.


▸ Abstract method does not have a
definition, it is defined in the
subclasses that inherit the abstract
class.
▸ An abstract class may have abstract
methods as well as normal ones.
Object-Oriented Java:
Abstraction with Interface

interface Pet {
public abstract String sound();
public String eat();
}

public class Dog implements Pet {


public String sound(){
return “Bark!”;
}
public String eat(){
return “Yum”;
}
}
Object-Oriented Java:
Abstraction with Interface

▸ Interface is a completely abstract class.


▸ Cannot be instantiated.
▸ Does not have normal methods.
▸ Must use “implement” keyword as opposed to
“extends”.
▸ You must override all interface methods.
▸ Interface methods are abstract and public.
▸ Interface attributes are public, static and final.
▸ You may implement multiple interfaces.
e.g. public class Dog implements Pet,
Mammal { … }
C# vs. Java Examples: Read a File
public static void readFile(string filepath) {
StreamReader sr = File.OpenText(filepath);
string line = "";
while((line = sr.ReadLine()) != null) {
Console.WriteLine(line);
}
sr.Close();
}

public static void readFile(String filepath) {


try {
BufferedReader br = new BufferedReader(new FileReader(filepath));
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}
C# vs. Java Examples: Write a File

public static void writeFile(string filepath, string [] output) {


StreamWriter sw = new StreamWriter(filepath);
foreach(string o in output) {
sw.WriteLine(o);
}
sw.Close();
}

public static void writeFile(String filepath, String [] output) {


try {
BufferedWriter bw = new BufferedWriter(new FileWriter(filepath);
for(String o : output){
bw.write(o+”\n”);
}
bw.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}

You might also like