Abstracting

 

Abstracting means to ignore or hide everything except essential data that we require for the given problem or situation.

The London tube map is a great example of an abstraction. We don't need info about shops, house addresses or precise directions. We just need station names linked together.

Non-essential information is ignored as we develop an abstraction.

Objectives

Students will be able to:

  • define a class file storing properties
  • define a constructor method
  • create an instance of an object(s) in a computer program

Gathering Requirements

Designing a new system involves various stages:

  • Gathering requirements from stakeholders eg end users
  • Designing the system
  • Developing and Testing the system
  • Evaluating the system to determine whether it met the requirements.

Gathering requirements is critical. If the design team does not fully understand the requirements, then the new system may not be exactly what the client needs.

At worst, this may result in the system being scrapped, wasting time and money.

At best, this may result in a system redesign, which also wastes time and money.

Constructor methods

A constructor method is called when an object needs to be constructed in a computer program.

Its purpose is usually to initialise object properties. Here is an example:

public class Cow{

String name;

int weight;

//define the constructor method

public Cow(String name){

this.name = name;

this.weight = 100;

}

}

Now, in another class eg Main.java, we can construct Cow objects!!

public static void main(String[] args){

Cow c1 = new Cow("Harold!");

Cow c2 = new Cow("Harriet!");

System.out.println(c1.weight);

System.out.println(c2.weight);

}

What will we see when we run the above Main program?

Object Oriented Programming

More Constructor methods

 

A hotel, Hotel Fandango in Malibu, USA, wants to digitise its checking-in system.

It needs a system that will print a ticket for every guest who checks in.

The software engineer has been tasked with solving this problem.

The engineer had a long meeting with the hotel manager in order to .

She will design the Ticket class to manage information about tickets.

According to the requirements, the Ticket abstraction will only include:

  • the name of the guest
  • the room number

Task 1

Open the worksheet and follow the instructions.

The two files you will need are available below:

Concept check!

A constructor method is often used to assign initial data to class properties. It can be super simple.

However, if you need it to do some extra processing, it is quite capable of doing it!

Exercises

Click the exercises button and see if you can complete the challenges.

Glossary

object

property

method

create an object/instance of a class

instantiate an object

encapsulate

access

mutate

abstraction

Tags

idesyntax file class instantiate constructor type propertymethod