University of Cape Town Department of Computer Science Computer Science CSC1017F

Similar documents
University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions

MIT AITI Swing Event Model Lecture 17

Theory Test 3A. University of Cape Town ~ Department of Computer Science. Computer Science 1016S ~ For Official Use

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2015

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

JAVA NOTES GRAPHICAL USER INTERFACES

RAIK 183H Examination 2 Solution. November 10, 2014

University of Cape Town Department of Computer Science. Computer Science CSC115F

GUI Forms and Events, Part II

CMP 326 Midterm Fall 2015

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Midterm assessment - MAKEUP Fall 2010

RAIK 183H Examination 2 Solution. November 11, 2013

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

2.4 Input and Output. Input and Output. Standard Output. Standard Output Abstraction. Input devices. Output devices. Our approach.

University of Cape Town ~ Department of Computer Science. Computer Science 1016S / 1011H ~ January Exam

We are on the GUI fast track path

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Attempt FOUR questions Marking Scheme Time: 120 mins

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

University of Cape Town ~ Department of Computer Science. Computer Science 1016S / 1011H ~ November Exam

Object-Oriented Programming: Revision. Revision / Graphics / Subversion. Ewan Klein. Inf1 :: 2008/09

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall (total 7 pages)

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

AP CS Unit 11: Graphics and Events

CSE Lab 8 Assignment Note: This is the last lab for CSE 1341

Original GUIs. IntroGUI 1

Points Missed on Page page 1 of 8

H212 Introduction to Software Systems Honors

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

University of Cape Town Department of Computer Science. Computer Science CSC115F. June 2003/2004 Supplementary Exam

Programming Languages and Techniques (CIS120)

University of Cape Town Department of Computer Science. Computer Science CSC115F. June Exam 2002

Systems Programming Graphical User Interfaces

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

Starting Out with Java: From Control Structures Through Objects Sixth Edition

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

The Islamic University Gaza Department of Electrical & Computer Engineering. Midterm Exam Spring 2012 Computer Programming II (Java) ECOM 2324

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2012

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Graphical User Interfaces. Swing. Jose Jesus García Rueda

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Examination - December 15, 2003

CSIS 10A Assignment 7 SOLUTIONS

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Introduction to the JAVA UI classes Advanced HCI IAT351

2.2 Input and Output. Input and Output. Digital Michelangelo Project. Terminal. Input devices. Output devices.

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

Window Interfaces Using Swing Objects

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Midterm Test II Object Oriented Programming in Java Computer Science, University of Windsor Fall 2014 Time 2 hours. Answer all questions

University of Cape Town ~ Department of Computer Science. Computer Science 1011H/1016S ~ January Exam

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ June Exam

Agenda. Container and Component

Window Interfaces Using Swing Objects

University of Cape Town Department of Computer Science. Computer Science CSC115F

First Name: AITI 2004: Exam 2 July 19, 2004

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

Graphical User Interfaces. Comp 152

Swing from A to Z Some Simple Components. Preface

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

First Name: AITI 2004: Exam 2 July 19, 2004

CSC 1214: Object-Oriented Programming

1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Examination - December 15, 2003

Week 9. Abstract Classes

JLayeredPane. Depth Constants in JLayeredPane

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Packages: Putting Classes Together

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Class 16: The Swing Event Model

To gain experience using GUI components and listeners.

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

Part 3: Graphical User Interface (GUI) & Java Applets

1 Looping Constructs (4 minutes, 2 points)

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015

import javax.swing.*; import java.awt.*; import java.awt.event.*;

COMPUTER APPLICATIONS

CSCI 201L Written Exam #1 - SOLUTION Fall % of course grade

Dr. Hikmat A. M. AbdelJaber

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

Chapter 13 Lab Advanced GUI Applications

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

5. In JAVA, is exception handling implicit or explicit or both. Explain with the help of example java programs. [16]

Final Examination Semester 2 / Year 2012

Final Exam CS 251, Intermediate Programming December 13, 2017

C10: Garbage Collection and Constructors

Transcription:

First Name: Last Name: Student Number: University of Cape Town Department of Computer Science Computer Science CSC1017F Class Test 4 - Solutions Wednesday, 17 May 2006 Marks: 40 Time: 40 Minutes Approximate marks per question are shown in brackets The use of calculators is permitted COURSE CODE: CSC117F This paper consists of 3 questions and 12 pages (including this cover page). Mark Allocation Question Marks Internal External Quest Marks Internal External 1 [20] 2 [15] 3 [5] Total Internal Examiner: Total Grand Total Final Mark External Examiner: 1

Question 1: Inheritance [20 marks] The classes listed below are used in writing an application for a Pet Shop. Examine the classes closely and answer the questions that follow. class Cat extends Mammal String Breed; boolean neutered; public Cat() Breed="mongrel"; neutered=false; public Cat(String ID, int sex, int agemonths, String Breed, boolean neutered) super(id,sex,agemonths); this.breed=breed; this.neutered = neutered; public abstract class Mammal extends Pet public Mammal() public Mammal(String ID, int sex, int agemonths) super(id,sex,agemonths); abstract class Pet protected String ID; protected int sex; protected int agemonths; public Pet() ID=""; sex=0; agemonths=0; public Pet(String ID, int sex, int agemonths) this.id=id; this.sex=sex; this.agemonths=agemonths; public abstract void MakeNoise(int NoOfNoises); 2

public abstract class Rodent extends Mammal public Rodent() public class TestPet public static void main(string[] args) Pet mypet = new Pet("Tibbles", 1, 14); a) Draw inheritance diagram for the classes listed above. [2] Pet Mammal Cat Rodent 3

b) What is an abstract class? [1] Class containing one or more abstract methods (i.e. no method implementation) c) What is wrong with the following code? [1] public class TestPet public static void main(string[] args) Pet mypet = new Pet("Tibbles", 1, 14); Pet is abstract cannot be instantiated. d) What is the keyword this used for? [1] to refer to the current object from inside the class. e) Give and example of a has a (aggregation) relationship from the classes above. [1] Pet String f) The class Rodent as written is incomplete. Write the code for a parameterized constructor for the class Rodent. [3] public Rodent(String ID, int sex, int agemonths) super(id,sex,agemonths); g) Class Cat is missing a MakeNoise method. Why does it have to have this method? [1] Cat extends Pet through Mammal which has an abstract method MakeNoise. Since Cat is not abstract it must implement MakeNoise 4

h) Show how you would create a one year old, female, neutered, Persian cat called Fluffball in the TestPet class. [2] //assume 0=female, 1=male Cat cat = new Cat("Fluffball", 0, 12, "Persian", true); i) Give an example of a method that the Cat class inherits from the Object class.[1] tostring j) You now want to include a Rat class. Write down the code for this class. Note that Rats have a breed variable and boolean variable indicating if they have long hair or not. Include a default and a parameterized constructor for your class and any other methods (see (g)) you have to define. [7] public class Rat extends Rodent String breed; boolean longhair; public Rat() 5

breed = "Brown"; longhair = false; public Rat(String ID, int sex, int agemonths, String breed, boolean longhair) super(id,sex,agemonths); this.breed = breed; this.longhair = longhair; public void MakeNoise(int NoOfNoises) System.out.println(NoOfNoises); 6

Question 2 : GUI s. [15 marks] Examine the program below and answer the questions that follow. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FirstGUI2 extends JFrame JButton all, reset; JCheckBox [] chboxes = new JCheckBox("Cheese"), new JCheckBox("Tomato"), new JCheckBox("Guacamole"), new JCheckBox("Chicken"); JPanel panel,panel2; butlistener bs; public FirstGUI2() all = new JButton("All"); reset = new JButton("Reset"); all.addactionlistener(new alllistener()); reset.addactionlistener(new butlistener()); panel= new JPanel(); panel.setlayout(new GridLayout(4,1)); for(int i=0;i<4;i++) panel.add(chboxes[i]); panel2= new JPanel(); panel2.add(all); panel2.add(reset); getcontentpane ().setlayout(new GridLayout(2,1)); getcontentpane ().add(panel); getcontentpane ().add(panel2); pack(); class butlistener implements ActionListener public void actionperformed(actionevent e) class alllistener implements ActionListener public void actionperformed(actionevent e) for(int i=0;i<4;i++) chboxes[i].setselected(true); 7

a) What is a top-level component? [1] A component that cannot be added to other containers b) Given an example of a top-level component from the program above [1] JFrame c) What is an event? [1] It is an action that can be taken (by a user) such as typing in a text box, resizing a window, pressing a button etc d) Give an example of an event from the program above. [1] Pressing a button e) What does the implements keyword mean? [1] Used to indicate that a particular class conforms to the specified interface 8

f) Draw a diagram of the component hierarchy for the program above. [3] JFrame (First GUI) JPanel (panel) JPanel (panel2) JCheckBox (checkbox[])* JButton (all) JButton (reset) g) The program above produces a window like this: 9

How would you alter it so that it looks like the picture below? Just rewrite the altered line(s) of code. [2] panel.setlayout(new GridLayout(2,0)); h) What happens when the user clicks on the All button? [2] All the checkboxes are selected. i) We want button Reset to reset the GUI to its initial state. Explain how you would alter the program so that it would do that, writing down code where necessary. [3] Add the following lines of code to the actionperformed method in the butlistener class for(int i=0;i<4;i++) 10

chboxes[i].setselected(false); Question 3: Input and Output. [5 marks] Examine the program below: public class CommandLine3 public static void main(string args[]) if (args.length!=3) System.out.println("Incorrect number of command line parameters!"); else System.out.print(args[0] + " + " + args[1] + " - " +args[2] + " = "); int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); int k = Integer.parseInt(args[2]); int sum = i+j-k; System.out.println(sum); a) What are command-line arguments? [1] Input passed into a program by a user when running the program via a console (command line) can be accessed via the args array of the main method b) Write down the exact output if the user types the following line into the DOS window: [2] java CL 4 6 7 4 + 6-7 = 3 11

c) Write down the exact output if the user types the following line into the DOS window: [2] java CL 4 6 3 6 7 Incorrect number of command line parameters! 12