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

Size: px
Start display at page:

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

Transcription

1 Review Exercises Here is a list of review exercises. There are times when an exercise will be open ended, allowing you to pursue multiple aspects of the material. While encompassing some areas we have studied, you should further supplement your studying by reviewing: your notes, your project assignments, exercise questions given in lecture, exercise questions given in discussion sessions, and problems from the chapters in our textbook. 1. Fill in the missing pieces/syntax of the method utilized below. State the base case(s), recursive case(s), and recursive step(s) utilized in the method. Also, discuss how the program is moving from the start to the base case(s). Finally, step through the code, showing the process of its execution. method( ) if (i > 21) { return 1 if (i % 2 == 0) { return 4 + method(i + 5) if (i % 2 == 1) { return 2 + method(i + 3) First, here are the blanks filled in: public static int method( int i ) (Note: the static modifier is necessary when method is used directly by the main method, static is left off when not used directly by main, when not used in a static context. You should test the method in both situations to see that it operates the same, but the need for static changes.) if (i > 21) // Base Case (BC) return 1 if (i % 2 == 0) // Recursive Case #1 (RC1) return 4 + method(i + 5) // Recursive Step #1 (RS1) if (i % 2 == 1) // Recursive Case #2 (RC2) return 2 + method(i + 3) // Recursive Step #2 (RS2) Sample execution if the input parameter initially given to method is 0. Note how the indentation lines up, connecting the statement where the recursive call begins to the finalization of the return statement, where the all the details of the entire return are known. Also, note how the value of i is expressed and lines up within and still connected to its usage within each recursive call. method(0) i = 0 // RC1 is called

2 return (4 + method(5)) i = 5 // RC2 is called return (2 + method(8)) i = 8 // RC1 is called return (4 + method(13)) i = 13 // RC2 is called return (2 + method(16)) i = 16 // RC1 is called return (4 + method(21)) i = 21 // RC2 is called return (2 + method(24)) i = 24 // BC is reached return 1 return 2+1 = 3 => completes the statement: return (2 + method(26)) return = 7 => completes the statement: return (4 + method(21)) return 2+7 = 9 => completes the statement: return (2 + method(16)) return 4+9 = 13 => completes the statement: return (4 + method(13)) return 2+13 = 15 => completes the statement: return (2 + method(8)) return 4+15 = 19 => completes the statement: return (4 + method(5)) Therefore the call to method passing in 0, method(0), returns Recursively create a solution to printing intersecting diagonals to the screen, similar to an X. State the base case(s), recursive case(s), and recursive step(s) utilized in your solution. Also, discuss how your solution is moving from the start to the base case(s). If five were entered, the output would be: diagonals() analysis and design Base Case: when all rows are printed row == number Rec Case: not all rows printed row < number Rec Step: Handle all prints for this row, newline, Move to the next row

3 printcolumns(number, 0); System.out.println(); diagonals(number, row + 1); printcolumns() analysis and design Base Case: when all columns printed col == number Rec Case: not all columns printed col < number Rec Step: Handle the print for this column, move to the next column two different steps, based upon different sub cases if (col == row) print row value if (col == number - row) print row value else print blank space printcolumns(number, col + 1); class Diagonals { private int number; public Diagonals() { number = 5; public Diagonals(int number) { this.number = number; public void printdiagonals() { printdiagonals(1); public int getnumber() { return number; public void printdiagonals(int row) { if (row <= number) { printcolumns(row, 1); System.out.println(); printdiagonals(row + 1); public void printcolumns(int row, int col) { if (col <= number) { if (col == row col == (number - row) + 1) { System.out.print(row); else { System.out.print(" "); printcolumns(row, col + 1);

4 public void setnumber(int number) { this.number = number; 3. Create the class Vehicle. The class will contain a protected property of type String, the color of the vehicle. 4. Create the class Bike, a subclass of Vehicle. Bike will use the private property of type String, called energylevel, to represent the energy level the rider of the bike exerts. When a bike is ridden, the movement will burn calories, high, medium, and low energy levels burning 500, 250, and 100 calories respectively. Bike will contain the method move, which will simulate its movement. The method will not receive any parameters and will return int. 5. Create the class Car, a subclass of Vehicle. Car will use two private properties of type int, called distance and speed, to represent the distance the car has traveled and the speed of the travel. When the car is driven, the movement will take a certain amount of time, determined by dividing the distance by the speed. Car will contain the method move, which will simulate its movement. The method will not receive any parameters and will return an int representation of the time. class Vehicle { protected String color; public Vehicle () { color = new String(); public Vehicle(String color) { this.color = color; public String getcolor() { return color; public void setcolor (String c) { color = c; public String tostring() { return "Vehicle " + getcolor() + "\n"; class Bike extends Vehicle { private String energylevel;

5 // Create the standard constructors... public Bike(){ super(); energylevel = new String("medium"); public Bike(String energylevel, String color){ super(color); this.energylevel = energylevel; // Returns the calories consumed public int move(){ // Bike moves and burns calories if (energylevel == "high") return 500; else if (energylevel == "medium") return 250; else if (energylevel == "low") return 100; else return -1; public String tostring() { return super.tostring() + "Energy Level: " + energylevel + "\n"; // Create all other appropriate methods: // gets, sets, etc. class Car extends Vehicle { private int distance; private int speed; // Create the standard constructors... public Car(){ super(); distance = 10; // mi speed = 5; // mi/hr public Car(int distance, int speed, String color){ super(color); this.distance = distance; // mi this.speed = speed; // mi/hr

6 // Returns the amount of time that the car is driven public int move() { // Car moves and consumes time returns distance / speed; public String tostring() { return super.tostring() + "Distance: " + distance + "\n" + "Speed: " + speed + "\n"; // Create all other appropriate methods: // gets, sets, etc. 6. Create the UML diagram representing the classes and class relationships of Bike, Car, and Vehicle. Vehicle Bike Car Vehicle #color: String +Vehicle() +Vehicle(color: String) +getcolor(): String +setcolor(color: String): void +tostring(): String -energylevel: String Bike +Bike() +Bike(energyLevel: String) +getenergylevel(): String

7 +move(): int +setenergylevel(energylevel: String): void +tostring(): String -distance: int -speed: int Car +Car() +Car(distance: int, speed: int) +getdistance(): int +getspeed(): int +move(): int +setdistance(distance: int): void +setspeed(speed: int): void +tostring(): String 7. Create a class Model. The class will represent different types of car models. The class will have private properties of type String type and manufacturer. What methods are necessary within this class? Create all of them. 8. Update the class Car. It will now have private properties color type String, model type Model, and speed type double. The class will have a method called accelerate which receives the amount of increase in speed and a method decelerate with receives the amount of decrease in speed. What other methods are necessary within this class? Create all of them. 9. Create a class Dealership. The class will have a private property cars type Car an array. What other methods are necessary in this class? Create all of them. 10. Add the method public Car removecariterative(string color) to the class Dealership. The method will iteratively find if a car matching the color given is in the array of cars. If so, remove the car from the array and return the car found. As you search for the car having a specific color, what will your comparison be? Use compareto, equals, or equalsignorecase from the class String to compare. Refer to the class Dealership solution. 11. Add the method public Car removecarrecursive (String color) to the class Dealership. The method will recursively find if a car matching the color given is in the array of cars. If so, remove the car from the array and return the car found. State the base case(s), recursive

8 case(s), and recursive step(s) utilized in your solution. Also, discuss how your solution is moving from the start to the base case. 12. Create a driver program. In the driver, allow the user to create and add cars to the dealership. Create an instance of your car, your roommate's car, and your dream car. What other methods are necessary in this class? Create all of them. The following classes provide solutions to Problems 8 through 13. import java.util.scanner; class Model { private String manufacturer; private String type; public Model() { this.manufacturer = new String(); this.type = new String(); public Model(String manufacturer, String type) { this.manufacturer = manufacturer; this.type = type; public String getmanufacturer() { return manufacturer; public String gettype() { return type; public void setmanufacturer(string manufacturer) { this.manufacturer = manufacturer; public void settype(string type) { this.type = type; public String tostring() { return "The Model Manufacturer is " + manufacturer + "\n" + "The Model Type is " + type + "\n"; class Car { private String color; private Model model; private double speed; public Car() { this.color = new String();

9 this.model = new Model(); this.speed = 35; public Car(String color, Model model, double speed) { this.color = color; this.model = model; this.speed = speed; public void accelerate(int amount) { speed = speed + amount; public void decelerate(int amount) { if (speed < amount) { speed = 0; else { speed = speed - amount; public String getcolor() { return color; public Model getmodel() { return model; public double getspeed() { return speed; public void setmodel(model model) { this.model = model; public void setspeed(double speed) { this.speed = speed; public String tostring() { return "The Color of the Car is " + color + "\n" + model.tostring() + "The Speed of the Car is " + speed + "\n"; class Dealership { private Car cars[]; public Dealership() { cars = new Car[10]; public Dealership(Car cars[]) {

10 this.cars = cars; public boolean addcar(car car) { int index = 0; if (cars[cars.length - 1]!= null) { System.out.println("No spaces available for a new car"); return false; while (cars[index]!= null) { index++; cars[index] = car; return true; public Car[] getcars() { return cars; public void setcars(car cars[]) { this.cars = cars; public Car removecariterative(string color) { Car car = null; int i, index = 0; while (index < cars.length &&!color.equalsignorecase(cars[index].getcolor())) { index++; if (index == cars.length) { System.out.println("A " + color + " could not be found to remove."); else { car = cars[index]; for (i = index; i < cars.length - 1; i++) { cars[i] = cars[i + 1]; cars[cars.length - 1] = null; System.out.println("This car has been removed.\n" + car.tostring()); return car; public Car removecarrecursive(string color) { Car car = null;

11 int index = findcarrecursive(color, 0); if (index >= 0 && index < cars.length) { car = cars[index]; shiftcars(index); System.out.println("This car has been removed.\n" + car.tostring()); return car; public int findcarrecursive(string color, int index) { if (index >= cars.length) { return cars.length; else if (color.equalsignorecase(cars[index].getcolor())) { return index; else { return findcarrecursive(color, index + 1); public void shiftcars(int index) { if (index >= cars.length - 1) { cars[cars.length - 1] = null; else { cars[index] = cars[index + 1]; shiftcars(index + 1); public String tostring() { int i = 0; String data = new String("The cars at the" + "dealership are:\n"); while (i < cars.length && cars[i]!= null) { data = data.concat("car #" + (i + 1) + "\n"); data = data.concat(cars[i].tostring()); i++; return data; class Driver { public static void main(string args[]) { boolean loop = true; char choice;

12 Dealership dealership = new Dealership(); Diagonals diagonals = new Diagonals(); Scanner scanner = new Scanner(System.in); dealership.addcar(new Car("blue", new Model("Honda", "Accord"), 75)); dealership.addcar(new Car("red", new Model("Ford", "F150"), 50)); dealership.addcar(new Car("green", new Model("Ford", "Mustang"), 25)); while (loop) { System.out.println("(a) Print Diagonals"); System.out.println("(b) Add a Car"); System.out.println("(c) Remove a Car Iterative"); System.out.println("(d) Remove a Car Recursive"); System.out.println("(e) Print all Car data"); System.out.println("(q) Quit"); System.out.print("Select an Option: "); choice = scanner.nextline().charat(0); if (choice == 'a') { System.out.print("Enter the number of rows " + "in the diagonals: "); diagonals.setnumber(integer.parseint( scanner.nextline())); diagonals.printdiagonals(); if (choice == 'b') { System.out.print("Enter the color of the car: "); String color = scanner.nextline(); System.out.print("Enter the manufacturer " + "of the car: "); String manufacturer = scanner.nextline(); System.out.print("Enter the type of the car: "); String type = scanner.nextline(); System.out.print("Enter the speed of the car: "); double speed = Double.parseDouble(scanner.nextLine()); dealership.addcar(new Car(color, new Model(manufacturer, type), speed)); if (choice == 'c') { System.out.print("Enter the color of " + "the car to remove iteratively: "); dealership.removecariterative(scanner.nextline());

13 if (choice == 'd') { System.out.print("Enter the color of " + "the car to remove recursively: "); dealership.removecarrecursive(scanner.nextline()); if (choice == 'e') { System.out.println(dealership.toString()); if (choice == 'q') { loop = false; 13. Analyze and design a solution to the problem of alarms and their subclasses. Consider, the classes Alarm, SmokeAlarm, ClockAlarm, and SecurityAlarm. The class Alarm will serve as a superclass to the other three classes. Since an alarm will need to ring or be sounded, and the decision to ring will be determined by different factors for each subclass, Alarm will be abstract and defer the implementation of the method ring to each subclass. The class SmokeAlarm will ring when the level of smoke detected in the area exceeds a defined threshold. Class properties will define the currentlevel and maxlevel of smoke. The class ClockAlarm will ring when a numeric time is reached. Two double class properties will the currenttime and ringtime. When the current time reaches the ring time, the alarm will sound. Note, a back up or second ring time can be tracked by using two properties: ringtime1 and ringtime2. The class SecurityAlarm will ring when an incorrect integer code sequence of 0, 1, 2,, 9 values is entered. The code sequence will be stored as an ArrayList of type Integer. The user entered code sequence will also be stored as an ArrayList of type Integer. If the two match, the alarm will not ring.

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.

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. Grading Page 1 / 4 Page3 / 20 Page 4 / 13 Page 5 / 10 Page 6 / 26 Page 7 / 17 Page 8 / 10 Total / 100 1. (4 points) What is your course section? CS 101 CS 101E Pledged Page 1 of 8 Pledged The following

More information

Java Simple Data Types

Java Simple Data Types Intro to Java Unit 1 Multiple Choice Test Key Java Simple Data Types This Test Is a KEY DO NOT WRITE ON THIS TEST This test includes program segments, which are not complete programs. Answer such questions

More information

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

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

More information

JAVA OPERATORS GENERAL

JAVA OPERATORS GENERAL JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1 Name SOLUTION Page Points Score 2 15 3 8 4 18 5 10 6 7 7 7 8 14 9 11 10 10 Total 100 1 P age 1. Program Traces (41 points, 50 minutes)

More information

Lab Exercise 1. Objectives: Part 1. Introduction

Lab Exercise 1. Objectives: Part 1. Introduction Objectives: king Saud University College of Computer &Information Science CSC111 Lab Object II All Sections - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

More information

AP COMPUTER SCIENCE A

AP COMPUTER SCIENCE A AP COMPUTER SCIENCE A CONTROL FLOW Aug 28 2017 Week 2 http://apcs.cold.rocks 1 More operators! not!= not equals to % remainder! Goes ahead of boolean!= is used just like == % is used just like / http://apcs.cold.rocks

More information

DM550 Introduction to Programming part 2. Jan Baumbach.

DM550 Introduction to Programming part 2. Jan Baumbach. DM550 Introduction to Programming part 2 Jan Baumbach jan.baumbach@imada.sdu.dk http://www.baumbachlab.net CALLING & DEFINING FUNCTIONS 2 Functions and Methods all functions in java are defined inside

More information

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination Monday, March 9, 2009 Examiners: Mathieu Petitpas [Section 1] 18:30

More information

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

This exam is open book. Each question is worth 3 points. This exam is open book. Each question is worth 3 points. Page 1 / 15 Page 2 / 15 Page 3 / 12 Page 4 / 18 Page 5 / 15 Page 6 / 9 Page 7 / 12 Page 8 / 6 Total / 100 (maximum is 102) 1. Are you in CS101 or

More information

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination Tuesday, November 4, 2008 Examiners: Mathieu Petitpas [Section 1] 18:30

More information

Java Simple Data Types

Java Simple Data Types Intro to Java Unit 1 Multiple Choice Java Simple Data Types DO NOT WRITE ON THIS TEST This test includes program segments, which are not complete programs. Answer such questions with the assumption that

More information

Key Java Simple Data Types

Key Java Simple Data Types AP CS P w Java Unit 1 Multiple Choice Practice Key Java Simple Data Types This test includes program segments, which are not complete programs. Answer such questions with the assumption that the program

More information

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

1. Which of the following is the correct expression of character 4? a. 4 b. 4 c. '\0004' d. '4' Practice questions: 1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4' 2. Will System.out.println((char)4) display 4? a. Yes b. No 3. The expression "Java

More information

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method. Name: Write all of your responses on these exam pages. 1 Short Answer (5 Points Each) 1. What is the difference between a compiler and an interpreter? Also, discuss Java s method. 2. Java is a platform-independent

More information

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

CMP-326 Exam 2 Spring 2018 Total 120 Points Version 1 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

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #04: Fall 2015 1/20 Office hours Monday, Wednesday: 10:15 am to 12:00 noon Tuesday, Thursday: 2:00 to 3:45 pm Office: Lindley Hall, Room 401C 2/20 Printing

More information

public static void main(string[] args) throws FileNotFoundException { ArrayList<Vehicle> arrayofvehicles = new ArrayList<Vehicle>();

public static void main(string[] args) throws FileNotFoundException { ArrayList<Vehicle> arrayofvehicles = new ArrayList<Vehicle>(); import java.io.*; import java.util.*; import java.text.*; import java.lang.*; // Class n00845431 public class Project4 static class EmailComparator implements Comparator public int compare(vehicle

More information

DM503 Programming B. Peter Schneider-Kamp.

DM503 Programming B. Peter Schneider-Kamp. DM503 Programming B Peter Schneider-Kamp petersk@imada.sdu.dk! http://imada.sdu.dk/~petersk/dm503/! TYPE CASTS & FILES & EXCEPTION HANDLING 2 Type Conversion Java uses type casts for converting values

More information

Object Oriented Programming. Java-Lecture 6 - Arrays

Object Oriented Programming. Java-Lecture 6 - Arrays Object Oriented Programming Java-Lecture 6 - Arrays Arrays Arrays are data structures consisting of related data items of the same type In Java arrays are objects -> they are considered reference types

More information

CS 101 Exam 2 Spring Id Name

CS 101 Exam 2 Spring Id Name CS 101 Exam 2 Spring 2005 Email Id Name This exam is open text book and closed notes. Different questions have different points associated with them. Because your goal is to maximize your number of points,

More information

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community CSCI-12 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu 1. Provide a detailed explanation of what the following code does: 1 public boolean checkstring

More information

STUDENT LESSON A12 Iterations

STUDENT LESSON A12 Iterations STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of

More information

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: CSC 1051 Algorithms and Data Structures I Final Examination May 2, 2016 Name: Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the spaces provided.

More information

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

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

DM537 Object-Oriented Programming. Peter Schneider-Kamp.

DM537 Object-Oriented Programming. Peter Schneider-Kamp. DM537 Object-Oriented Programming Peter Schneider-Kamp petersk@imada.sdu.dk! http://imada.sdu.dk/~petersk/dm537/! TYPE CASTS & FILES & EXCEPTION HANDLING 2 Type Conversion Java uses type casts for converting

More information

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements COMP-202 Unit 4: Programming With Iterations CONTENTS: The while and for statements Introduction (1) Suppose we want to write a program to be used in cash registers in stores to compute the amount of money

More information

CSCI Problem Solving, Programming and Computers Spring, 2016 Assignment 2 {Inheritance, Interfaces, Arrays}

CSCI Problem Solving, Programming and Computers Spring, 2016 Assignment 2 {Inheritance, Interfaces, Arrays} CSCI 160 - Problem Solving, Programming and Computers Spring, 2016 Assignment 2 Inheritance, Interfaces, Arrays Q1. [20 points] Q1. TXT Short answer questions Fill in the blanks in each of the following

More information

Loops. CSE 114, Computer Science 1 Stony Brook University

Loops. CSE 114, Computer Science 1 Stony Brook University Loops CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation Suppose that you need to print a string (e.g., "Welcome to Java!") a user-defined times N: N?

More information

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003 1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 7, 2003 Name: Email Address: TA: Section: You have 90 minutes to complete this exam. For coding questions, you do not need to

More information

CMSC131 Final Exam Practice Questions

CMSC131 Final Exam Practice Questions CMSC131 Final Exam Practice Questions Disclaimer: The following are questions that try to provide you with some practice material for the final exam. By no means do they represent the only material you

More information

Programming Basics. Digital Urban Visualization. People as Flows. ia

Programming Basics.  Digital Urban Visualization. People as Flows. ia Programming Basics Digital Urban Visualization. People as Flows. 28.09.2015 ia zuend@arch.ethz.ch treyer@arch.ethz.ch Programming? Programming is the interaction between the programmer and the computer.

More information

Chapter 6 Lab Classes and Objects

Chapter 6 Lab Classes and Objects Lab Objectives Chapter 6 Lab Classes and Objects Be able to declare a new class Be able to write a constructor Be able to write instance methods that return a value Be able to write instance methods that

More information

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose, java provides control structures

More information

Chapter 6 Lab Classes and Objects

Chapter 6 Lab Classes and Objects Gaddis_516907_Java 4/10/07 2:10 PM Page 51 Chapter 6 Lab Classes and Objects Objectives Be able to declare a new class Be able to write a constructor Be able to write instance methods that return a value

More information

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

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed 1 1 COMP200 INHERITANCE OOP using Java, from slides by Shayan Javed 2 Inheritance Derive new classes (subclass) from existing ones (superclass). Only the Object class (java.lang) has no superclass Every

More information

CSC 240 Computer Science III Spring 2018 Midterm Exam. Name

CSC 240 Computer Science III Spring 2018 Midterm Exam. Name CSC 240 Computer Science III Spring 2018 Midterm Exam Name Page Points Score 2 9 4-6 53 7-10 38 Total 100 1 P age 1. Tracing programs (1 point each value): For each snippet of Java code on the left, write

More information

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: Question Value Score

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: Question Value Score CSC 1051 Algorithms and Data Structures I Final Examination May 2, 2016 Name: Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the spaces provided.

More information

Practice Problems: Instance methods

Practice Problems: Instance methods Practice Problems: Instance methods Submit your java files to D2L. Late work will not be acceptable. a. Write a class Person with a constructor that accepts a name and an age as its argument. These values

More information

CSIS 10A Assignment 9 Solutions

CSIS 10A Assignment 9 Solutions CSIS 10A Assignment 9 Solutions Read: Chapter 9 Choose and complete any 10 points from the problems below, which are all included in the download file on the website. Use BlueJ to complete the assignment,

More information

CSCI Problem Solving, Programming and Computers Spring, 2016 Assignment 2 {Inheritance, Interfaces, Arrays}

CSCI Problem Solving, Programming and Computers Spring, 2016 Assignment 2 {Inheritance, Interfaces, Arrays} CSCI 160 - Problem Solving, Programming and Computers Spring, 2016 Assignment 2 Inheritance, Interfaces, Arrays Q1. [20 points] Q1. TXT Short answer questions Fill in the blanks in each of the following

More information

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

while (/* array size less than 1*/){ System.out.print(Number of students is invalid. Enter + number of students: ); /* read array size again */ import java.util.scanner; public class CourseManager1 { public static void main(string[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter number of students: "); /* read the number

More information

Tutorial 7. If it compiles, what does it print? Inheritance 1. Inheritance 2

Tutorial 7. If it compiles, what does it print? Inheritance 1. Inheritance 2 Tutorial 7 If it compiles, what does it print? Here are some exercises to practice inheritance concepts, specifically, overriding, overloading, abstract classes, constructor chaining and polymorphism.

More information

Practice Midterm 2 CMPS 12A Fall 2017

Practice Midterm 2 CMPS 12A Fall 2017 1.) Determine the output of the following program. public class Question1{ Car x = new Car("gray", 100000); Car y = new Car("red", 125000); Car z = new Car("blue", 150000); x = y; roadtrip(x); z = x; System.out.println(z.mileage);

More information

Midterm Examination (MTA)

Midterm Examination (MTA) M105: Introduction to Programming with Java Midterm Examination (MTA) Spring 2013 / 2014 Question One: [6 marks] Choose the correct answer and write it on the external answer booklet. 1. Compilers and

More information

Lecture 2: Java & Javadoc

Lecture 2: Java & Javadoc Lecture 2: Java & Javadoc CS 62 Fall 2018 Alexandra Papoutsaki & William Devanny 1 Instance Variables or member variables or fields Declared in a class, but outside of any method, constructor or block

More information

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET SOLUTION SHEET ENGR 2710U: Object Oriented Programming & Design Midterm Exam October 19, 2012, Duration: 80 Minutes (9 Pages, 14 questions, 100 Marks) Instructor: Dr. Kamran Sartipi Name: Student Number:

More information

Place your name tag here

Place your name tag here CS 170 Exam 1 Section 001 Spring 2015 Name: Place your name tag here Instructions: Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work. Do NOT communicate with

More information

CSIS 10A Assignment 4 SOLUTIONS

CSIS 10A Assignment 4 SOLUTIONS CSIS 10A Assignment 4 SOLUTIONS Calculator.java Name: Naomi Lyles Approximate completion time: 20 minutes References: None Write a description of class Calculator here. @author (your name) @version (a

More information

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements WIT COMP1000 Simple Control Flow: if-else statements Control Flow Control flow is the order in which program statements are executed So far, all of our programs have been executed straight-through from

More information

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

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course : Advanced Java Concepts + Additional Questions from Earlier Parts of the Course 1. Given the following hierarchy: class Alpha {... class Beta extends Alpha {... class Gamma extends Beta {... In what order

More information

CSE 114 Computer Science I

CSE 114 Computer Science I CSE 114 Computer Science I Iteration Cape Breton, Nova Scotia What is Iteration? Repeating a set of instructions a specified number of times or until a specific result is achieved How do we repeat steps?

More information

Fundamentals of Programming Data Types & Methods

Fundamentals of Programming Data Types & Methods Fundamentals of Programming Data Types & Methods By Budditha Hettige Overview Summary (Previous Lesson) Java Data types Default values Variables Input data from keyboard Display results Methods Operators

More information

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

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007 Name: Please fill in your Student Number and Name. Student Number : Student Number: University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ 2007 Final Examination Question Max

More information

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them. Dec. 9 Loops Please answer the following questions. Do not re-code the enclosed codes if you have already completed them. What is a loop? What are the three loops in Java? What do control structures do?

More information

CS 101 Spring 2007 Midterm 2 Name: ID:

CS 101 Spring 2007 Midterm 2 Name:  ID: You only need to write your name and e-mail ID on the first page. This exam is CLOSED text book, closed-notes, closed-calculator, closed-neighbor, etc. Questions are worth different amounts, so be sure

More information

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors 1 CS/ENGRD 2110 SPRING 2017 Lecture 5: Local vars; Inside-out rule; constructors http://courses.cs.cornell.edu/cs2110 Announcements 2 1. Writing tests to check that the code works when the precondition

More information

Tutorial 03. Exercise 1: CSC111 Computer Programming I

Tutorial 03. Exercise 1: CSC111 Computer Programming I College of Computer and Information Sciences CSC111 Computer Programming I Exercise 1: Tutorial 03 Input & Output Operators Expressions A. Show the result of the following code: 1.System.out.println(2

More information

Programming with Java

Programming with Java Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules

More information

CONSTRUCTORS. CS302 Introduction to Programming University of Wisconsin Madison Lecture 19. By Matthew Bernstein

CONSTRUCTORS. CS302 Introduction to Programming University of Wisconsin Madison Lecture 19. By Matthew Bernstein CONSTRUCTORS CS302 Introduction to Programming University of Wisconsin Madison Lecture 19 By Matthew Bernstein matthewb@cs.wisc.edu Constructors A constructor initializes the instance variables of an object

More information

Oct Decision Structures cont d

Oct Decision Structures cont d Oct. 29 - Decision Structures cont d Programming Style and the if Statement Even though an if statement usually spans more than one line, it is really one statement. For instance, the following if statements

More information

Part (II): In the body method main : Write the call to method formatname and have the return value stored in the variable formatted.

Part (II): In the body method main : Write the call to method formatname and have the return value stored in the variable formatted. CPSC 233 Midterm Review: Winter 2013 Written questions: Question 1 Refer to the code in class MyFormatter and class Driver public class MyFormatter { private String first; private String last; private

More information

Inheritance (IS A Relationship)

Inheritance (IS A Relationship) Inheritance (IS A Relationship) We've talked about the basic idea of inheritance before, but we haven't yet seen how to implement it. Inheritance encapsulates the IS A Relationship. A String IS A Object.

More information

CS212 Midterm. 1. Read the following code fragments and answer the questions.

CS212 Midterm. 1. Read the following code fragments and answer the questions. CS1 Midterm 1. Read the following code fragments and answer the questions. (a) public void displayabsx(int x) { if (x > 0) { System.out.println(x); return; else { System.out.println(-x); return; System.out.println("Done");

More information

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit. Name CS 110 Practice Final Exam originally from Winter, 2003 Instructions: closed books, closed notes, open minds, 3 hour time limit. There are 4 sections for a total of 49 points. Part I: Basic Concepts,

More information

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn to define and invoke void and return java methods JAVA

More information

Selenium Class 9 - Java Operators

Selenium Class 9 - Java Operators Selenium Class 9 - Java Operators Operators are used to perform Arithmetic, Comparison, and Logical Operations, Operators are used to perform operations on variables and values. public class JavaOperators

More information

CS 302 Week 9. Jim Williams

CS 302 Week 9. Jim Williams CS 302 Week 9 Jim Williams This Week P2 Milestone 3 Lab: Instantiating Classes Lecture: Wrapper Classes More Objects (Instances) and Classes Next Week: Spring Break Will this work? Double d = new Double(10);

More information

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Islamic University of Gaza Faculty of Engineering Computer Engineering Department Student Mark Islamic University of Gaza Faculty of Engineering Computer Engineering Department Question # 1 / 18 Question # / 1 Total ( 0 ) Student Information ID Name Answer keys Sector A B C D E A B

More information

Assignment 8B SOLUTIONS

Assignment 8B SOLUTIONS CSIS 10A Assignment 8B SOLUTIONS Read: Chapter 8 Choose and complete any 10 points from the problems below, which are all included in the download file on the website. Use BlueJ to complete the assignment,

More information

CSE 143 Lecture 18. More Recursive Backtracking. slides created by Marty Stepp

CSE 143 Lecture 18. More Recursive Backtracking. slides created by Marty Stepp CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp http://www.cs.washington.edu/143/ Exercise: Dominoes The game of dominoes is played with small black tiles, each having 2 numbers

More information

Prof. Navrati Saxena TA: Rochak Sachan

Prof. Navrati Saxena TA: Rochak Sachan JAVA Prof. Navrati Saxena TA: Rochak Sachan Operators Operator Arithmetic Relational Logical Bitwise 1. Arithmetic Operators are used in mathematical expressions. S.N. 0 Operator Result 1. + Addition 6.

More information

MIDTERM REVIEW. midterminformation.htm

MIDTERM REVIEW.   midterminformation.htm MIDTERM REVIEW http://pages.cpsc.ucalgary.ca/~tamj/233/exams/ midterminformation.htm 1 REMINDER Midterm Time: 7:00pm - 8:15pm on Friday, Mar 1, 2013 Location: ST 148 Cover everything up to the last lecture

More information

AP CS Unit 7: Interfaces Exercises 1. Select the TRUE statement(s).

AP CS Unit 7: Interfaces Exercises 1. Select the TRUE statement(s). AP CS Unit 7: Interfaces Exercises 1. Select the TRUE statement(s). a) This code will not compile because a method cannot specify an interface as a parameter. public class Testing { public static void

More information

1 Short Answer (15 Points Each)

1 Short Answer (15 Points Each) COSC 7 Exam # Solutions Spring 08 Short Answer (5 Points Each). Write a method called RollCount that takes in two integer parameters rolls and target. The method should simulate the rolling of two die,

More information

ICSE Class 10 Computer Applications ( Java ) 2010 Solved Question...

ICSE Class 10 Computer Applications ( Java ) 2010 Solved Question... 1 of 12 05-11-2015 16:23 ICSE J Java for Class X Computer Applications ICSE Class 10 Computer Applications ( Java ) 2010 Solved Question Paper COMPUTER APPLICATIONS (Theory) Two Hours Answers to this Paper

More information

University of Palestine. Mid Exam Total Grade: 100

University of Palestine. Mid Exam Total Grade: 100 First Question No. of Branches (5) A) Choose the correct answer: 1. If we type: system.out.println( a ); in the main() method, what will be the result? int a=12; //in the global space... void f() { int

More information

Question 1 [20 points]

Question 1 [20 points] Question 1 [20 points] a) Write the following mathematical expression in Java. c=math.sqrt(math.pow(a,2)+math.pow(b,2)- 2*a*b*Math.cos(gamma)); b) Write the following Java expression in mathematical notation.

More information

CS Week 14. Jim Williams, PhD

CS Week 14. Jim Williams, PhD CS 200 - Week 14 Jim Williams, PhD This Week 1. Final Exam: Conflict Alternatives Emailed 2. Team Lab: Object Oriented Space Game 3. BP2 Milestone 3: Strategy 4. Lecture: More Classes and Additional Topics

More information

CSCI 1101 Winter 2017 Laboratory No Submission deadline is p.m. (5 minutes to midnight) on Saturday, February 4th, 2017.

CSCI 1101 Winter 2017 Laboratory No Submission deadline is p.m. (5 minutes to midnight) on Saturday, February 4th, 2017. CSCI 1101 Winter 2017 Laboratory No. 3 This lab is a continuation of the concepts of object-oriented programming, specifically the use of static variables and static methods, and object interactions. If

More information

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website: Repetition CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this

More information

Full file at

Full file at Chapter 1 Primitive Java Weiss 4 th Edition Solutions to Exercises (US Version) 1.1 Key Concepts and How To Teach Them This chapter introduces primitive features of Java found in all languages such as

More information

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks: كلية الحاسبات وتقنية المعلوما Exam 2 Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: November 22, 2015 Student Name: Student ID: Total Marks: 40 Obtained Marks: Instructions: Do not open this

More information

IT 313 Advanced Application Development

IT 313 Advanced Application Development Page 1 of 7 IT 313 Advanced Application Development Practice Midterm Exam Part A. Multiple Choice Questions. Answer all questions. Optional: supply a reason or show work for partial credit in case you

More information

1 Short Answer (10 Points Each)

1 Short Answer (10 Points Each) COSC 117 Exam # Solutions Fall 01 1 Short Answer (10 Points Each) 1. Write a declaration for a two dimensional array of doubles that has 1 rows and 17 columns. Then write a nested for loop that populates

More information

Chapter 13: Arrays of Objects

Chapter 13: Arrays of Objects Chapter 13: Arrays of Objects Think Java: How to Think Like a Computer Scientist 5.1.2 by Allen B. Downey As in Chapter 11, we will be following a more standard approach than the one taken by Downey in

More information

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY CSC 1051 Algorithms and Data Structures I Midterm Examination October 11, 2018 Name: KEY Question Value Score 1 20 2 20 3 20 4 20 5 20 TOTAL 100 Please answer questions in the spaces provided. If you make

More information

Conditional Programming

Conditional Programming COMP-202 Conditional Programming Chapter Outline Control Flow of a Program The if statement The if - else statement Logical Operators The switch statement The conditional operator 2 Introduction So far,

More information

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose, java provides control structures

More information

CS1083 Week 2: Arrays, ArrayList

CS1083 Week 2: Arrays, ArrayList CS1083 Week 2: Arrays, ArrayList mostly review David Bremner 2018-01-08 Arrays (1D) Declaring and using 2D Arrays 2D Array Example ArrayList and Generics Multiple references to an array d o u b l e prices

More information

CS141 Programming Assignment #6

CS141 Programming Assignment #6 CS141 Programming Assignment #6 Due Sunday, Nov 18th. 1) Write a class with methods to do the following output: a) 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1 b) 1 2 3 4 5 4 3 2 1 1 2 3 4 * 4 3 2 1 1 2 3 * * * 3 2 1

More information

ICSE Class 10 Computer Applications ( Java ) 2014 Solved Question Paper

ICSE Class 10 Computer Applications ( Java ) 2014 Solved Question Paper 1 of 10 05-11-015 16:1 ICSE J Java for Class X Computer Applications ICSE Class 10 Computer Applications ( Java ) 014 Solved Question Paper ICSE Question Paper 014 (Solved) Computer Applications Class

More information

CMPS 12A Introduction to Programming Midterm 2 Review Problems

CMPS 12A Introduction to Programming Midterm 2 Review Problems CMPS 12A Introduction to Programming Midterm 2 Review Problems Note: Do problems 4, 5 and 9 from the Midterm 1 review sheet. Problems 6, 8 and 9 from this sheet have not yet been covered. We'll see how

More information

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: CSC 1051 Algorithms and Data Structures I Final Examination May 2, 2015 Name: Question Value 1 10 Score 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 20 TOTAL 100 Please answer questions in the spaces provided.

More information

Chapter 3. Selections

Chapter 3. Selections Chapter 3 Selections 1 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator 6. The Switch Statement 7. Useful Hints 2 1. Flow of

More information

5) (4 points) What is the value of the boolean variable equals after the following statement?

5) (4 points) What is the value of the boolean variable equals after the following statement? For problems 1-5, give a short answer to the question. (15 points, ~8 minutes) 1) (4 points) Write four Java statements that declare and initialize the following variables: A) a long integer with the value

More information

16. Give a detailed algorithm for making a peanut butter and jelly sandwich.

16. Give a detailed algorithm for making a peanut butter and jelly sandwich. COSC120FinalExamReview2010 1. NamethetwotheoreticalmachinesthatCharlesBabbagedeveloped. 2. WhatwastheAntikytheraDevice? 3. Givethecodetodeclareanintegervariablecalledxandthenassignitthe number10. 4. Givethecodetoprintout

More information

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each Your Name: Your Pitt (mail NOT peoplesoft) ID: Part Question/s Points available Rubric Your Score A 1-6 6 1 pt each B 7-12 6 1 pt each C 13-16 4 1 pt each D 17-19 5 1 or 2 pts each E 20-23 5 1 or 2 pts

More information

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016 Name: USC NetID (e.g., ttrojan): CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016 There are 5 problems on the exam, with 56 points total available. There are 10 pages to the exam (5 pages

More information

Chapter 10 Linked Lists A

Chapter 10 Linked Lists A Data Structures for Java William H. Ford William R. Topp Chapter 10 Linked Lists A Bret Ford 2005, Prentice Hall Introducing Linked Lists To insert or remove an element at an interior location in an ArrayList

More information