L16 - Computer Science 1 1 SS C Lecture 16 Contents! 2!...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CompSci 101 - Lecture 16/17 1 Computer Science 1 1 SS C Lecture 16 Contents Practice defning a class - ForSale class this null, equals() method Equality o± objects Coursebook: §13 2 ForSale - instance variables Consider defning a class to represent items which are ±or sale on a website. iDNumber 2314 description —1 9 7 6 D i a r y - Hardly used“ —0 2 1 - 4 5 6 3 4 5 6 “ askingPrice 23 false iDNumber 2315 description —Dire Straits - Brothers in Arms LP“ contact —0 2 7 - 4 5 3 9 7 6 “ askingPrice 56 isSold false Below is a visualisation o± two typical instances o± the ForSale class. 3 1 2 3 4 5 6 7 ForSale - instance variables The instance variables represent the in±ormation which all instances o± the ForSale class will contain. public class ForSale { } private int iDNumber; private String description; private String contactDetails; private int askingPrice; private boolean isSold; 4 ForSale - constructor 1 2 3 4 5 6 7 8 9 10 11 12 13 public class ForSale { } public ForSale( int iD, String contact, int price, String info ) { } private int iDNumber; private String description; private String contact; private int askingPrice; private boolean isSold; iDNumber description askingPrice contactDetails isSold = false; = iD; = info; = price; = contact; 5 ForSale - instance methods Decide which operations can be per±ormed? setIsSold() : change the value stored in the boolean variable, isSold. getIsSold() : obtain the value stored in the boolean variable, isSold. setAskingPrice() : change the value stored in the int variable, askingPrice. getAskingPrice() : obtain the value stored in the int variable, askingPrice. toString() : obtain a String description o± the object. 6 ForSale - instance methods 1 2 - 6 7 - 13 14 15 16 public class ForSale { public ForSale( int iD, String contact, int price, String info ) { iDNumber = iD; description = info; askingPrice = price; contactDetails = contact; isSold = false; } private int iDNumber; private String description; private String contact; private int askingPrice; private boolean isSold; public boolean getIsSold() { return isSold; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

L16 - Computer Science 1 1 SS C Lecture 16 Contents! 2!...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online