Question
A program, written using object-oriented programming, stores pictures as objects.
The program stores the dimensions of the picture (width and height), the colour of the frame (e.g. black), and a description of the picture (e.g. flowers).
The class has the following attributes and methods.
Picture
Description : STRING // stores a description of the picture
Width : INTEGER // stores the width e.g. 30
Height : INTEGER // stores the height e.g. 40
FrameColour : STRING // stores the colour e.g. black
Constructor() // takes all four values as parameters and sets them to the private attributes
GetDescription() // returns the description of the picture
GetHeight() // returns the height
GetWidth() // returns the width
GetColour() // returns the frame colour
SetDescription() // takes the new description as a parameter and writes the value to description
(a) The constructor takes the picture description, frame colour, height, and width as parameters and sets these to the private attributes.
Write the program code to declare the class Picture and its constructor.
Do not write any other methods.
Use your language appropriate constructor. All attributes should be private.
If you are writing in Python programming language, include attribute declarations using comments.
Save your program as question 2.
Copy and paste the program code into part 2(a) in the evidence document.
[5]
(b) The four get methods return the associated attribute, for example, GetDescription() returns the description of the picture.
Write the four get methods.
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[3]
(c) The method SetDescription() takes a new description as a parameter, and writes this value to the appropriate attribute.
Write the method SetDescription().
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[2]
(d) Write program code to declare an array of type Picture with 100 elements.
Save your program.
Copy and paste the program code into part 2(d) in the evidence document.
[1]
(e) The text file Pictures.txt stores the data for the pictures in the order: description, width, height, colour.
For example, for the first picture in the text file:
Flowers is the description
45 is the width
50 is the height black is the frame colour.
The data read into the program from the text file is stored in an array of type Picture.
The main program and the function will need to access the array data.
The function ReadData():
• opens the file Pictures.txt
• reads the data from the file
• creates a new object of type Picture for each picture
• writes each object to the array
• raises an exception if the file cannot be found
• counts and returns the number of pictures in the array.
Write program code for the function ReadData().
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
[8]
(f) The main program calls the function ReadData().
Write the main program.
Save your program.
Copy and paste the program code into part 2(f) in the evidence document.
[2]
(g) The main program needs to ask the user to input their requirements for a picture. The user will enter the colour of the frame, the maximum width, and the maximum height of the picture.
The program will then search the array of pictures, and output the picture description, the width, and the height of any picture that meets the user’s requirements.
The program should allow the user to input the colour in any case (e.g. Silver, silver, or
SILVER), and still output the correct results.
Edit the main program to perform the described actions.
Save your program.
Copy and paste the program code into part 2(g) in the evidence document.
[7]
(h) Test your program by inputting the following search criteria:
• BLACK, 100, 100
• silver, 25, 25
Take screenshots to show the output for both search criteria.
Copy and paste the screenshots into part 2(h) in the evidence document.
[2]
Mark scheme
2(a) 1 mark per bullet point 5
• class declared (with appropriate close) with identifier Picture
• correct attribute declarations with Data types (Description, Frame colour =
string, Width, Height = integer.)
• …as private
• correct constructor (with appropriate close) with four parameters…
• …parameters assigned to attributes
Example program code:
Python
class Picture:
def __init__(self, DescriptionP, WidthSizeP,
HeightSizeP, FrameColourP):
self.__Description = DescriptionP # string
self.__Width = int(WidthSizeP) #integer
self.__Height = int(HeightSizeP) #integer
self.__FrameColour = FrameColourP #string
Java
class Picture{
private String Description;
private Integer Width;
private Integer Height;
private String FrameColour;
public Picture(String DescriptionP, Integer WidthP,
Integer HeightP, String FrameColourP){
Description = DescriptionP;
Width = WidthP;
Height = HeightP;
FrameColour = FrameColourP;
}
}
2(a) VB.NET
Class Picture
Private Description As String
Private Width As Integer
Private Height As Integer
Private FrameColour As String
Public Sub New(DescriptionP, WidthP, HeightP,FrameColourP)
Description = DescriptionP
Width = WidthP
Height = HeightP
FrameColour = FrameColourP
End Sub
End Class
2(b) 1 mark per bullet point 3
• 1 Get method taking no parameter…
• …returning correct attribute
• remaining 3 correct methods
Example program code:
Python
def GetDescription(self):
return self.__Description
def GetWidth(self):
return self.__Width
def GetHeight(self):
return self.__Height
def GetColour(self):
return self.__FrameColour
Java
public String GetDescription(){
return Description;
}
public Integer GetWidth(){
return Width;
}
public Integer GetHeight(){
return Height;
}
public String GetFrameColour(){
return FrameColour;
}
VB.NET
Function GetDescription()
Return Description
End Function
Function GetWidth()
Return Width
End Function
Function GetHeight()
Return Height
End Function
Function GetFrameColour()
Return FrameColour
End Function
2(c) 1 mark per bullet point 2
• Set method (procedure) taking parameter (no return) …
• …assigning parameter to correct attribute
Example program code:
Python
def SetDescription(self, DescriptionP):
self.__Description = DescriptionP
Java
public void SetDescription(String DescriptionP){
Description = DescriptionP;
}
VB.NET
Public Sub SetDescription(DescriptionP)
Description = DescriptionP
End Sub
2(d) 1 mark for declaring array of type Picture with 100 elements 1
Example program code:
Python
PictureArray = []
for i in range(100):
PictureArray.append(Picture("",0,0,""))
Java
public static void main(String[] args){
Picture[] PictureArray = new Picture[100];}
VB.NET
Dim PictureArray(0 to 99) As Picture
2(e) 1 mark per bullet point: 8
• Exception with opening the file inside...
• ..appropriate catch and output
1 mark per bullet point to Max 7
• Function/procedure declared with correct name (and close, passing array by
reference or global array declared)
• opening Pictures.txt for Read
• looping until EOF / or equivalent
• …reading each set of 4 lines from the file within loop
• creating object of type Picture
• …with Description, Width, Height, Frame colour from File as parameters
• ..adding to next array element/appending
• closing the File (in an appropriate place)
• counts and returns number of pictures in array
Example program code:
Python
def ReadData(PictureArray):
Filename = "Pictures.txt"
Counter = 0
try:
File = open(Filename,"r")
Description = (File.readline()).strip().lower()
while(Description != ""):
Width = int((File.readline()).strip())
Height = int((File.readline()).strip())
Frame = ((File.readline()).strip()).lower()
PictureArray[Counter] = Picture(Description,
Width, Height, Frame)
Description =((File.readline()).strip()).lower()
Counter = Counter + 1
File.close()
except IOError:
print("Could not find File")
return Counter, PictureArray
2(e) VB.NET
Function ReadData(ByRef PictureArray, ByRef NumberPictures)
As Integer
Dim Counter As Integer = 0
Try
Dim Filename As String = "Pictures.txt"
Dim FileReader As New System.IO.StreamReader(Filename)
Dim Description, FrameColour As String
Dim Height, Width As Integer
While FileReader.Peek <> -1
Description = FileReader.ReadLine()
Width = FileReader.ReadLine()
Height = FileReader.ReadLine()
FrameColour = FileReader.ReadLine()
PictureArray(NumberPictures) =
New Picture(Description, Width, Height, FrameColour)
NumberPictures = NumberPictures + 1
Counter = Counter + 1
End While
FileReader.Close()
Catch ex As Exception
Console.WriteLine("Invalid File")
End Try
Return Counter
End Function
2(e) Java
public static Integer ReadData(Picture[] PictureArray){
String Filename = "Pictures.txt";
String DataRead;
String Description;
String Width;
String Height;
String FrameColour;
Integer NumberPictures = 0;
try{
FileReader f = new FileReader(Filename);
BufferedReader Reader = new BufferedReader(f);
DataRead = Reader.readLine();
while(DataRead != null){
Description = DataRead;
Width = Reader.readLine();
Height = Reader.readLine();
FrameColour = Reader.readLine();
PictureArray[NumberPictures] =
new Picture(Description, Integer.parseInt(Width),
Integer.parseInt(Height), FrameColour);
NumberPictures++;
DataRead = Reader.readLine();
}
Reader.close();
}
catch(FileNotFoundException ex){
System.out.println("No File found");
}
catch(IOException ex){
System.out.println("No File found");
}
return NumberPictures;
}
2(f) 1 mark per bullet point 2
• calling function ReadData() …
• …store/use the Number of elements returned/by reference based on answer
to part 2e
Example program code:
Python
NumberPicturesInArray, PictureArray = ReadData(PictureArray)
Java
Integer NumberPicturesInArray = ReadData(PictureArray);
VB.NET
Dim NumberPicturesInArray As Integer = ReadData()
2(g) 1 mark per bullet point to Max 7 7
• taking as input all three values (colour, width, height)
• … converting colour to lowercase // uppercase
• looping through array …
• …using returned value from part 2(f) as max index
• …within loop, checking if Array[index].FrameColour matches input
• …and checking if Array[index].FrameWidth <= input Width
• …and checking if Array[index].FrameHeight <= input Height
• …all using Get methods
• outputting the Picture Description, Width and Height for all/any matching
Pictures
Example program code:
Python
FrameColour = input("Input the Frame colour ").lower()
MaxWidth = int(input("Input the Maximum Width "))
MaxHeight = int(input("Input the Maximum Height "))
print("Matches Frames shown")
for Z in range(0, NumberPicturesInArray):
if PictureArray[Z].GetColour() == FrameColour:
if(PictureArray[Z].GetWidth() <= MaxWidth):
if (PictureArray[Z].GetHeight() <= MaxHeight):
print(PictureArray[Z].GetDescription(), " " ,
str(PictureArray[Z].GetWidth()), " ",
str(PictureArray[Z].GetHeight()))
VB.NET
Sub Main()
Dim PictureArray(0 To 99) As Picture
Dim NumberPictures As Integer = 0
Dim FrameColour As String
Dim MaxWidth, MaxHeight As Integer
ReadData(PictureArray, NumberPictures)
Console.WriteLine("Input the Frame colour")
FrameColour = (Console.ReadLine()).ToLower()
Console.WriteLine("Input the Maximum Width")
MaxWidth = Console.ReadLine()
Console.WriteLine("Input the Maximum Height")
MaxHeight = Console.ReadLine()
Console.WriteLine("Matching Frames shown")
For X = 0 To NumberPictures - 1
If PictureArray(X).GetFrameColour() = FrameColour And
PictureArray(X).GetWidth <= MaxWidth And
PictureArray(X).GetHeight <= MaxHeight Then
2(g) Console.WriteLine(PictureArray(X).GetDescription() & " "
& PictureArray(X).GetWidth() & " " &
PictureArray(X).GetHeight)
End If
Next
Console.ReadLine()
End Sub
Java
public static void main(String[] args){
Picture[] PictureArray = new Picture[100];
Integer NumberPicturesInArray = ReadData(PictureArray);
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the Frame colour");
String FrameColour = scanner.nextLine();
System.out.println("Enter the Maximum Width");
Integer MaxWidth = Integer.parseInt(scanner.nextLine());
System.out.println("Enter the Maximum Height");
Integer MaxHeight = Integer.parseInt(scanner.nextLine());
FrameColour = FrameColour.toLowerCase();
for(int X = 0; X < NumberPicturesInArray; X++){
if(PictureArray[X].GetFrameColour().equals(FrameColour) &&
PictureArray[X].GetWidth() <= MaxWidth &&
PictureArray[X].GetHeight() <= MaxHeight){
System.out.println(PictureArray[X].GetDescription() +
" " + PictureArray[X].GetWidth() + " " +
PictureArray[X].GetHeight());
}
}
}
2(h) 1 mark for screenshot showing output for BLACK, 100, 100 2
1 mark for showing no outputs for silver, 25, 25
Input the Frame colour BLACK
Input the Maximum Width 100
Input the Maximum Height 100
Matches Frames shown
flowers 45 50
people 20 20
landscape 30 45
landscape 25 37
people 50 40
Input the Frame colour silver
Input the Maximum Width 25
Input the Maximum Height 25
Matches Frames shown