程序代写 COMPSCI4039: Programming

COMPSCI4039: Programming

COMPSCI4039: Programming

Copyright By PowCoder代写 加微信 powcoder

Previously…
Introduced objects, and how to define them and make them
Introduced the concept of references
Saw what happens when one object has multiple references
Some useful methods to into your objects – equals() and toString()
Inheritance: one of the key strengths of OO programming

But first…remember that String == String thing?

public class Person {
// Two attributes
private String name; private int age;
// A constructor
public Person(String s,int a) {
public int getAge() { // getter for age
return age;
public String getName() {
return name;
public void setAge(int a) { // setter for age
public void setName(String n) {
// A method
public void sayHello() {
System.out.println(“Hi, I’m ” + name + ” and I am ” + age + ” years old.”);

== with objects
What happens if we test for equality of two references?
true if they refer to the same object
Multiple objects with the same values for their attributes are not the same object
public class PersonEquals {
public static void main(String[] args) {
Person personOne = new Person(“Bill”,56);
Person personTwo = personOne;
boolean test1 = (personOne == personTwo); // true!
Person personThree = new Person(“Bill”,56);
boolean test2 = (personOne == personThree); // false!

personThree

Strings are objects

public class StringComparison {
public static void main(String[] args) {
String a = “Hello”;
String b = “Hel”;
b += “lo”;
boolean test1 = (a == b); // false

String c = “Hello”;
boolean test2 = (a == c);
/* true because java is efficient and only
makes one object that a and c both refer to

We’ve seen String.equals
All objects have an equals(Object o) method that compares the current object with o
The default behaviour checks to see if both references refer to the same object
In Strings, the method has been overwritten (re-defined) to check the characters in the String
For our objects, we can overwrite it with our own method to define the behaviour we want
I.e. it’s up to us what equals means for our objects.

For example, maybe people are equal if they have the same age…
Note the type checking: equals always takes an argument of type Object. We have to check it is a Person and then cast.
If it is not a Person, they cannot be equal.
public class Person {
private String name;
private int age;
public Person(String n, int a) { name = n;age = a;}
public int getAge() { return age;}
public boolean equals(Object o) {
// First check type!
if(!(o instanceof Person)) {
return false;
Person other = (Person)o;
if(age == other.getAge()) {
return true;
return false;
public static void main(String[] args) {
Person a = new Person(“Fred”,12);
Person b = new Person(“Clare”,12);
System.out.println(a == b); // false! Different objects
System.out.println(a.equals(b)); // true! ages are equal

A useful function
What happens if:
System.out.println(personOne);
Output tells us it’s a reference to a Person object at a particular address.
Not much help…

Writing a toString() method in any object tells Java what to do should it be asked to interpret the object as a String (e.g. in a println statement)…

public class Person {
private String name;
private int age;
public Person(String s,int a) {
public int getAge() { // getter for age
return age;
public String getName() {
return name;
public void setAge(int a) { // setter for age
public void setName(String n) {
public void sayHello() {
System.out.println(“Hi, I’m ” + name +
” and I am ” + age + ” years old.”);
public String toString() {
return “I am ” + name + ” and I am ” + age;

public String toString() {
return “I am ” + name + ” and I am ” + age;

System.out.println(personOne);
I am Bill and I am 57
public String toString() {
return “I am ” + name + ” and I am ” + age;

COMPSCI4039: Programming

Inheritance

Objects are useful
Allow for code reuse:
Anyone who needs a person in their code, could use our Person class to make Person objects.
Provide encapsulation:
The people using our Person class never need to look inside the code. It’s self-contained.
Allow for inheritance:
We can make classes that inherit everything from another class and (optionally) add extra attributes and methods

Running example
public class Bin {
private String contents;
public Bin() {
contents = “”;
public void add(String thing) {
contents += thing + “, “;
public String toString() {
return “Bin contains: ” + contents;
public void empty() {
contents = “”;

public class BinTest {
public static void main(String[] args) {
Bin normalBin = new Bin();
normalBin.add(“banana skin”);
System.out.println(normalBin);
normalBin.add(“coke can”);
System.out.println(normalBin);
normalBin.empty()
System.out.println(normalBin);

Bin contains: banana skin,
Bin contains: banana skin, coke can,
Bin contains:

Recycling is good
Very similar to Bin
Only difference is in toString method
Annoying to write everything twice…
public class RecyclingBin {
private String contents;
public RecyclingBin() {
contents = “”;
public void add(String thing) {
contents += thing + “, “;
public String toString() {
return “Recycling bin contains: ” + contents;
public void empty() {
contents = “”;

Inheritance
Can make RecyclingBin inherit from Bin
Use extends in class declaration
RecyclingBin has all methods and attributes that Bin has
But a different toString method
super calls the constructor for the super class (Bin)
public class RecyclingBin extends Bin {
public RecyclingBin() {
public String toString() {
return “Recycling bin contains: ” + contents;

We had to make one change in Bin too
contents was private
Would not be accessible to RecyclingBin
Change to protected
protected attributes are visible to this class, any class that inherits from it, and anything in the same package
public class Bin {
protected String contents;
public Bin() {
contents = “”;
public void add(String thing) {
contents += thing + “, “;
public String toString() {
return “Bin contains: ” + contents;
public void empty() {
contents = “”;

The add and empty methods can be used with RecyclingBin
They are inherited from Bin
When we call toString (i.e. use println) it uses the new version defined in RecyclingBin
public class BinTest {
public static void main(String[] args) {
Bin normalBin = new Bin();
normalBin.add(“banana skin”);
normalBin.add(“coke can”);
System.out.println(normalBin);
RecyclingBin rBin = new RecyclingBin();
rBin.add(“paper”);
System.out.println(rBin);
rBin.empty();

Bin contains: banana skin, coke can,
Recycling bin contains: paper,

Adding things to RecyclingBin
We can add things to the new class (that won’t be visible to Bin)
E.g. a new attribute for the type of recycling this bin takes…
public class RecyclingBin extends Bin {
private String type;
public RecyclingBin(String t) {
public String toString() {
return “Recycling bin (” + type + “) contains: ” + contents;

public class RecyclingBin extends Bin {
private String type;
public RecyclingBin(String t) {
public String toString() {
return “Recycling bin (” + type + “) contains: ” + contents;

The RecyclingBin constructor

RecyclingBin can have an argument to the constructor even though Bin doesn’t.

We call the constructor for Bin using super() (it sets contents to an empty string).
Note: super() must be the first line of the constructor

public class BinTest {
public static void main(String[] args) {
Bin normalBin = new Bin();
normalBin.add(“banana skin”);
normalBin.add(“coke can”);
System.out.println(normalBin);

RecyclingBin rBin = new RecyclingBin(“glass”);
rBin.add(“bottle”);
System.out.println(rBin);
rBin.empty();

Bin contains: banana skin, coke can,
Recycling bin (glass) contains: bottle,

And we can add methods
Don’t worry about the particular method (the length of the String is obviously not the same as the weight of the stuff in the bin)…it’s not important
public class RecyclingBin extends Bin {
private String type;
public RecyclingBin(String t) {
public String toString() {
return “Recycling bin (” + type + “) contains: ” + contents;
public int weigh() {
return contents.length();

public class BinTest {
public static void main(String[] args) {
Bin normalBin = new Bin();
normalBin.add(“banana skin”);
normalBin.add(“coke can”);
System.out.println(normalBin);
RecyclingBin rBin = new RecyclingBin(“glass”);
rBin.add(“bottle”);
System.out.println(rBin);
int w = rBin.weigh(); // this is ok
rBin.empty();
int a = normalBin.weigh(); // not ok!

We can call weigh from RecyclingBin objects
But not from Bin objects
Inheritance is not symmetric
RecyclingBin gets all Bin methods, but not the other way around

Inheritance can go on forever…

public class Cardboard extends RecyclingBin {
public Cardboard() {
super(“cardboard”);

public class BinTest {
public static void main(String[] args) {
Cardboard c = new Cardboard();
c.add(“shoe box”);
System.out.println(c);
System.out.println(c.weigh());

Recycling bin (cardboard) contains: shoe box,
add comes all the way down from Bin
weigh comes from RecyclingBin

Keeping track
Large class hierarchies (things inheriting from things, inheriting from other things) can become hard to keep track of
UML diagrams are often used to visualise it

COMPSCI4039: Programming

Objects we know already
…and have mentioned
Integer (an object wrapped around an int)
Double (an object wrapped around a double)
Let’s look at Integer in a little more detail

Take a look in the Java API for Integer here https://docs.oracle.com/javase/7/docs/api/
It has lots of methods
Some are static
We call them with Integer. rather than from a particular Integer object.
E.g. Integer.parseInt(String) tries to convert a String into an Integer

public class Inty {
public static void main(String[] args) {
Integer a = new Integer(3);
Integer b = 3; // shorthand
double c = a.doubleValue();
// converts to double
Integer d = Integer.parseInt(“4”);

Immutable objects
[covered in more detail in Advanced Programming, semester 2]
Immutable objects cannot be changed once created
Instead, Java makes a new object, and points the reference to the new one
String is immutable
String b = “Hel”;
b += “lo”;

Empty references
We can make references without assigning an object to them
E.g. Person personOne;
Creates a reference that isn’t pointing to anything.
Will cause an error if we try and use it
Can also point it to an empty object using null
E.g. personOne = null;
Will no longer get an error if we try and print the object (will get output: null) but we will if we try and use any Person methods or access its attributes.

Another word on inheritance [AP; semester 2]
Abstract classes: In Java we can design classes that we cannot make objects from!
This can be very useful, we’ll see in AP
Interfaces: Interfaces define a set of methods. Any class that implements the interface must have these methods in it
In Java, any class can only extend one other class but your class can implement as many interfaces as you like
Polymorphism: We can store an object in a reference for its parent class
E.g. we could do:
Bin r = new RecyclingBin(“glass”); // a Bin reference, a RecyclingBin object
Why? Can be very useful when dealing with lots of related objects. Find out in AP!

Objects – what you need to know
What they are
How to define them
How to make and use them
What references are
How to pass references to methods
What happens when >1 reference refers to the same object
What public, private, protected, and static mean
How to make classes that extend (inherit from) other classes

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com