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

Similar documents
University of Cape Town Department of Computer Science Computer Science CSC1017F

MIT AITI Swing Event Model Lecture 17

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

SINGLE EVENT HANDLING

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

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

AP CS Unit 11: Graphics and Events

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

Agenda. Container and Component

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

Part I: Learn Common Graphics Components

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

CS 251 Intermediate Programming GUIs: Event Listeners

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

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

PIC 20A GUI with swing

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

Chapter 13 Lab Advanced GUI Applications

Final Examination Semester 2 / Year 2011

Introduction. Introduction

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

H212 Introduction to Software Systems Honors

Midterm assessment - MAKEUP Fall 2010

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Lecture 5: Java Graphics

Class 16: The Swing Event Model

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

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

Window Interfaces Using Swing Objects

JAVA NOTES GRAPHICAL USER INTERFACES

OLLSCOIL NA heireann THE NATIONAL UNIVERSITY OF IRELAND, CORK. COLAISTE NA hollscoile, CORCAIGH UNIVERSITY COLLEGE, CORK

CS 251 Intermediate Programming GUIs: Components and Layout

CS 180 Fall 2006 Exam II

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

HW#1: Pencil Me In Status!? How was Homework #1? Reminder: Handouts. Homework #2: Java Draw Demo. 3 Handout for today! Lecture-Homework mapping.

Packages: Putting Classes Together

Swing from A to Z Some Simple Components. Preface

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

Systems Programming Graphical User Interfaces

Window Interfaces Using Swing Objects

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

CS 180 Final Exam Review 12/(11, 12)/08

Graphical User Interfaces. Comp 152

CS 2113 Software Engineering

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!

Homework 6 part 2: Turtle Etch-A-Sketch (40pts)

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

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

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

Name Section. CS 21a Introduction to Computing I 1 st Semester Final Exam

Final Exam CS 251, Intermediate Programming December 13, 2017

Graphics User Defined Forms, Part I

CS Exam 3 - Spring 2010

GUI Forms and Events, Part II

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

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

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

CS Exam 3 - Spring 2010

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

RAIK 183H Examination 2 Solution. November 11, 2013

Original GUIs. IntroGUI 1

Do not turn to the next page until the start of the exam.

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

CSIS 10A Assignment 7 SOLUTIONS

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

CS 1316 Exam 1 Summer 2009

CS Exam 1 Review Suggestions

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

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

Final Examination Semester 2 / Year 2010

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

Java, Swing, and Eclipse: The Calculator Lab.

RAIK 183H Examination 2 Solution. November 10, 2014

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

CSC 1214: Object-Oriented Programming

Programming Languages and Techniques (CIS120)

Introduction to the JAVA UI classes Advanced HCI IAT351

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

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

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

CS180 Spring 2010 Exam 2 Solutions, 29 March, 2010 Prof. Chris Clifton

Example: Building a Java GUI

Points Missed on Page page 1 of 8

Programming Language Concepts: Lecture 8

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

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

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

ANSWER KEY Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

ITEC 120 4/14/11. Review. Need. Objectives. GUI basics JFrame JPanel JLabel, JButton, JTextField Layout managers. Lecture 38 GUI Interactivity

Graphical User Interfaces

Window Interfaces Using Swing. Chapter 12

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

Transcription:

University of Cape Town Department of Computer Science Computer Science CSC117F Solutions Class Test 4 Wednesday 14 May 2003 Marks: 40 Approximate marks per question are shown in brackets Time: 40 minutes The use of calculators is permitted NAME: Surname Initials STUDENT NO: COURSE CODE: CSC This paper consists of 7 questions and 11 pages (including this cover page). Mark Allocation Question Marks Internal External Quest Marks Internal External 1 [11] 5 [7] 2 [5] 6 [4] 3 [3] 7 [6] 4 [4] Total Internal Examiner: Total Grand Total Final Mark External Examiner: 1

Question 1. [11 marks] Consider the Person class below public class Person private String name; public void setname (String n) name = n; public void print () System.out.println (name); a) Write a Java class Student using the following information: 1. The Student class must inherit the data member and the methods of the Person class 2. The Student class must have the following: student number (a String) as data member setstudentno method that sets the student number print method that calls the print method of the Person class to print the name of the student and prints the student number as well public class Student extends Person private String studentno; public void setstudentno (String s) studentno = s; super.print (); System.out.println (studentno); [7] 2

b) Write a main method that tests the above classes. It must do the following: Create a new student Set the name of the student to Jill Hope Set the student number to HPXJIL003 Print the student s name and student number Use the structure below as a template for your answer. public class UseStudent public static void main (String [] args) Student s = new Student (); s.setname ("Jill Hope"); s.setstudentno ("HPXJIL003"); s.print (); 3

a) Consider the following statement Rat r = new Rat(); What output will this statement produce? Why? Making a mammal Making a rodent Making a rat by the compiler [4] b) Insert a statement into the Rat class that calls the superclass constructor explicitly // The "Rat" class. class Rat extends Rodent public Rat () 4

Question 3. [3 marks] a) Complete the FarmyardAnimal class by filling in the missing statement so that all classes that inherit from this class will be forced to define a makenoise method as in the Duck and Pig classes below. abstract class FarmyardAnimal protected int positionx, positiony; public abstract void makenoise(); public void move (int newx, int newy) positionx = newx; positiony = newy; class Duck extends FarmyardAnimal public void makenoise () // code to go quack quack... class Pig extends FarmyardAnimal public void makenoise () // code to go oink oink... b) What do we call a method such as the makenoise method? Abstract Method c) What do we call a class that contains at least one method such as the makenoise method? Abstract Class 5

Question 4. [4 marks] Consider the classes below. Study the comments for each class, and complete the statements that define the classes class FarmyardAnimal protected int positionx, positiony; public void move (int newx, int newy) positionx = newx; positiony = newy; interface CanMakeNoise public abstract void makenoise (); /* The Duck class inherits from the FarmyardAnimal class and conforms to the CanMakeNoise interface */ class Duck extends FarmyardAnimal implements CanMakeNoise public void makenoise () /* code to go quack quack */... /* The Rabbit class inherits from the FarmyardAnimal class (it makes no noise )*/ class Rabbit extends FarmyardAnimal public void wriggleears ()...... /* The Windmill class conforms to the CanMakeNoise interface */ class Windmill implements CanMakeNoise public void makenoise () /* code to go squeak squeak */ [4] 6

Question 5. [7 marks] a) What is inheritance? Inheritance is a relationship between classes where one class is a more specialized version of another class b) What is aggregation? Aggregation is a relationship between classes where one class is part of another class c) What is an abstract class? An abstract class is a class which contains at least one abstract method (a method without a body). Abstract classes establish a basic form to which all subclasses must conform. A interface is a pure abstract class i.e. it contains no data variables or method bodies only constants and method headers [2] e) Complete the following sentence: Every class in Java is a subclass of class Object 7

Question 6. [4 marks] Examine the program below: import java.awt.event.*; import javax.swing.*; public class testbuttoneg extends JFrame private JPanel background = new JPanel (); private JButton button1 = new JButton ("One"); private JButton button2 = new JButton ("Two"); private JLabel thetext = new JLabel ("Nothing"); public testbuttoneg () button1.addactionlistener (new ButtonListener ("Pressed one")); button2.addactionlistener (new ButtonListener ("Pressed two")); background.add (button1); background.add (button2); background.add (thetext); getcontentpane ().add (background); setdefaultcloseoperation (JFrame.EXIT_ON_CLOSE); pack (); class ButtonListener implements ActionListener public void actionperformed (ActionEvent e) thetext.settext (buttonstring); pack (); buttonstring = buttonstring + " again"; public ButtonListener (String newstring) buttonstring = newstring; private String buttonstring = ""; public static void main (String [] args) testbuttoneg frame = new testbuttoneg (); frame.setvisible (true); 8

a) This program creates a window containing three atomic components (two JButtons and a JLabel). Draw a diagram to show what this window looks like when the program starts, including all text that is displayed. b) What happens when the user clicks the right mouse button on button1 for the first time? [2] The display text (the JLabel object thetext ) changes from Nothing to Pressed one c) What happens when the user clicks the right mouse button on button1 for the second time? The display text (the JLabel object thetext ) changes from Pressed one to Pressed one again 9

Question 7. [6 marks] a) What are layout managers used for in java? Layout managers are used to control the size and position of components in containers 10

PackedFrame frame = new PackedFrame (); frame.setvisible (true); c) Write down the program lines that you would have to add to create the following layout of the buttons in the main window (just add the missing code): //added code panelone.setlayout(new BorderLayout()); panelone.add(button1,borderlayout.north); panelone.add(button2,borderlayout.west); panelone.add(button3,borderlayout.center); panelone.add(button4,borderlayout.east); panelone.add(button5,borderlayout.south); paneltwo.add(button6); paneltwo.add(button7); paneltwo.add(button8); //end added code (it is not necessary to specify the layout for paneltwo, as FlowLayout is the default. However, it is not incorrect to do so) 11