CMP-326 Exam 2 Spring 2018 Total 120 Points Version 1

Similar documents
CMP-326 Exam 2 Spring 2018 Solutions Question 1. Version 1. Version 2

CMP 326 Midterm Fall 2015

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

Final Examination Semester 3 / Year 2008

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

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

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

JAVA OPERATORS GENERAL

Java Simple Data Types

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

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

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

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

Chapter 6: Inheritance

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

CS 113 PRACTICE FINAL

Key Java Simple Data Types

Java Simple Data Types

protected void printserial() { System.out.println("> NO." + this.serialno); this.serialno++; }

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

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

RAIK 183H Examination 2 Solution. November 11, 2013

CS 1301 Ch 8, Handout 2

Selenium Class 9 - Java Operators

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

Distributed Systems Recitation 1. Tamim Jabban

University of Palestine. Mid Exam Total Grade: 100

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Java GUI Test #1 Solutions 7/10/2015

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Prof. Navrati Saxena TA: Rochak Sachan

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Distributed Systems Recitation 1. Tamim Jabban

BM214E Object Oriented Programming Lecture 6. Classes and Objects

Final Examination Semester 2 / Year 2010

CS 101 Exam 2 Spring Id Name

Final Exam 90 minutes Eng. Mohammed S. F. Abdual Al

RAIK 183H Examination 2 Solution. November 10, 2014

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

CSCI 201L Midterm Written Summer % of course grade

DM550 Introduction to Programming part 2. Jan Baumbach.

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

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

CS 209 Programming in Java #10 Exception Handling

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

Implementing non-static features

Software and Programming I. Classes and Arrays. Roman Kontchakov / Carsten Fuhs. Birkbeck, University of London

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

CS1083 Week 2: Arrays, ArrayList

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

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

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

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

CMPS 11 Intermediate Programming Midterm 2 Review Problems

CSC 240 Computer Science III Spring 2018 Midterm Exam. Name

CS 1331 Exam 1 ANSWER KEY

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

Name:... ID:... class A { public A() { System.out.println( "The default constructor of A is invoked"); } }

Final Exam Practice Questions

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

2. [20] Suppose we start declaring a Rectangle class as follows:

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

Graphics User Defined Forms, Part I

Page 1 / 3. Page 2 / 18. Page 3 / 8. Page 4 / 21. Page 5 / 15. Page 6 / 20. Page 7 / 15. Total / 100. Pledge:

JAVA WRAPPER CLASSES

Selected Questions from by Nageshwara Rao

Objects and Classes (1)

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

Object-Oriented Programming and Software Engineering CITS1001 MID-SEMESTER TEST

CSCI 201L Midterm Written SOLUTION Fall % of course grade

coe318 Lab 2 ComplexNumber objects

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

CSE 143 Lecture 20. Circle

Chapter 6 Lab Classes and Objects

Fundamentals of Object Oriented Programming

CS 1302 Chapter 9 (Review) Object & Classes

CS 302 Week 9. Jim Williams

Chapter 6 Class and Method

University of Massachusetts Amherst, Electrical and Computer Engineering

if (i % 2 == 0) { return 4 + method(i + 5) } if (i % 2 == 1) { return 2 + method(i + 3) } public static int method( int i )

THE UNIVERSITY OF WESTERN AUSTRALIA. School of Computer Science & Software Engineering CITS1001 OBJECT-ORIENTED PROGRAMMING AND SOFTWARE ENGINEERING

Some Sample AP Computer Science A Questions - Solutions

CMP 326 Final Spring There is a blank page at the end of the exam if you need more room to answer a question.

Interfaces. An interface defines a set of methods. An interface declaration contains signatures, but no implementations.

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

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

This exam is open book. Each question is worth 3 points.

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013

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

Keyword this. Can be used by any object to refer to itself in any class method Typically used to

CSE 142, Spring 2009, Sample Final Exam #2. Good luck!

CMSC131 Final Exam Practice Questions

CS180. Exam 1 Review

CMPS 12A Introduction to Programming Midterm 2 Review Problems

CS 113 MIDTERM EXAM 2 SPRING 2013

Window Interfaces Using Swing Objects

Transcription:

Version 1 5. (10 Points) What is the output of the following code: int total = 0; int i = 0; while( total < 90 ) { switch( i ) { case 0: total += 30; i = 1; break; case 1: i = 2; total -= 15; case 2: i = 0; total += 15; break; System.out.println(total); 5. (30 Points) The Java class named Song has four private instance variables: title, which is a String representing the title of the song. artist, which is a String representing the performer of the song. length, which is a int representing the length of the song in seconds. composer, which is a String representing the composer of the song. a. (10 Points) Write a contructor for the class that accepts parameters for all intance variables and intantiates an object Song with those values:

Version 1 b. (15 Points) Write the public boolean equals(object obj) method to compare Songs for equality. (You can assume that all instance variable have getters): c. (5 Points) Write the public String tostring() method to return a String containing all the information about a Song: 5. (10 Points) Write a static method named swap that swaps two elements in an array of int values. The method should accept the following parameters: An int named index1 representing the index of one element being swapped. An int named index2 representing the index of the other element being swapped. An int array named array containing the values being swapped. The method should not return anything.

Version 1 5. (40 Points) Write a class named SumDif that extends JFrame and contains the following elements: Two JTextField s (both start with a 0 as their text) Two JButton s (one called Sum the other called Diff ) One JLabel The user should be able to use your GUI to enter two numbers using the JTextField s. If the user presses the Sum button, your GUI should add the two numbers and display the result in the JLabel. If the user presses the Diff button, your GUI should subtract one number from the other and display the result in the JLabel. Please note that you can convert a String s to an int like this: int num = new Integer(s).intValue(); Also, note that if s does not contain a parsable integer, the above Integer constructor would throw a NumberFormatException. Your code should be able to handle this exception without crashing.

Version 1

Version 1 5. (30 Points) Given the following classes, show the output from running the Q4_1 class public class A1 { private String color,name; private int length; public A1(String color, int length) { this.color = color; this.length = length; this.name = "A1"; public void setname(string name) { this.name = name; public int getlength() { return length; public String tostring() { return "A1 [name = " + name + " color = " + color + " length = " + length + "]"; public class B1 extends A1 { private int width; public B1(String color, int length, int width) { super(color,length); this.width = width; super.setname("b1"); public int getarea() { return getlength() * width; public String tostring() { String s = super.tostring(); s = s + "\nb1 [width = " + width + " area = " + getarea() + "]"; return s; public class C1 extends B1 { private int height; public C1(String color, int length, int width, int height) { super(color, length, width); this.height = height; super.setname("c1"); public int getvolume() { return getarea() * height; public String tostring() { String s = super.tostring(); s = s + "\nc1 [height = " + height + " volume = " + getvolume() + "]"; return s; public class Q4_1 { public static void main(string[] args) { A1 a, a1; B1 b, b1; C1 c1; a1 = new A1("Blue",2); System.out.println(a1); b1 = new B1("Green",6,4); System.out.println(b1); c1 = new C1("Grey",3,2,5); System.out.println(c1); a = b1; System.out.println(a); b = c1; System.out.println(b);

Version 1 Page Intentionally Left Blank

Version 2 1. (10 Points) What is the output of the following code: int total = 0; int i = 0; while( total < 60 ) { switch( i ) { case 0: total += 20; i = 1; break; case 1: i = 2; total -= 10; case 2: i = 0; total += 10; break; System.out.println(total); 2. (30 Points) The Java class named Course has four private instance variables: title, which is a String representing the title of the course. instructor, which is a String representing the instructor of the course. Credits, which is a int representing the number of credits of the course. department, which is a String representing the department of the course. a. (10 Points) Write a contructor for the class that accepts parameters for all intance variables and intantiates an object Course with those values:

Version 2 b. (15 Points) Write the public boolean equals(object obj) method to compare Courses for equality. (You can assume that all instance variable have getters): c. (5 Points) Write the public String tostring() method to return a String containing all the information about a Course: 3. (10 Points) Write a static method named swap that swaps two elements in an array of double values. The method should accept the following parameters: An int named index1 representing the index of one element being swapped. An int named index2 representing the index of the other element being swapped. A double array named array containing the values being swapped. The method should not return anything.

Version 2 5. (40 Points) Write a class named MulDiv that extends JFrame and contains the following elements: Two JTextField s (both start with a 0 as their text) Two JButton s (one called Mul the other called Div ) One JLabel The user should be able to use your GUI to enter two numbers using the JTextField s. If the user presses the Mul button, your GUI should multiphy the two numbers and display the result in the JLabel. If the user presses the Div button, your GUI should divide one number by the other and display the result in the JLabel. Please note that you can convert a String s to an int like this: int num = new Integer(s).intValue(); Also, note that if s does not contain a parsable integer, the above Integer constructor would throw a NumberFormatException. Your code should be able to handle this exception without crashing. You should also handle the ArithmeticException that could result when you divide by 0.

Version 2

Version 2 5. (30 Points) Given the following classes, show the output from running the Q4_2 class public class A2 { private String color,name; private int length; public A2(String color, int length) { this.color = color; this.length = length; this.name = "A2"; public void setname(string name) { this.name = name; public int getlength() { return length; public String tostring() { return "A2 [name = " + name + " color = " + color + " length = " + length + "]"; public class B2 extends A2 { private int width; public B2(String color, int length, int width) { super(color,length); this.width = width; super.setname("b2"); public int getarea() { return getlength() * width; public String tostring() { String s = super.tostring(); s = s + "\nb2 [width = " + width + " area = " + getarea() + "]"; return s; public class C2 extends B2 { private int height; public C2(String color, int length, int width, int height) { super(color, length, width); this.height = height; super.setname("c2"); public int getvolume() { return getarea() * height; public String tostring() { String s = super.tostring(); s = s + "\nc2 [height = " + height + " volume = " + getvolume() + "]"; return s; public class Q4_2 { public static void main(string[] args) { A2 a, a1; B2 b, b1; C2 c1; a1 = new A2("Grey", 5); System.out.println(a1); b1 = new B2("Blue", 4, 4); System.out.println(b1); c1 = new C2("Red", 2, 4, 3); System.out.println(c1); a = b1; System.out.println(a); b = c1; System.out.println(b);

Version 2 Page Intentionally Left Blank