Introduction to Computer Science Unit 4B. Programs: Classes and Objects

Size: px
Start display at page:

Download "Introduction to Computer Science Unit 4B. Programs: Classes and Objects"

Transcription

1 Introduction to Computer Science Unit 4B. Programs: Classes and Objects This section must be updated to work with repl.it 1. Copy the Box class and compile it. But you won t be able to run it because it does not have a main method. Create a second class file named TestBoxes which has a main method. In the main method do the following: - create a Box object 2 ft wide by 6 ft long - call the getarea method and display the result - call the bigger method and double the dimensions of the box - call the getperimeter method and display the result public class Box { private double length, width; public Box( double len, double wid ) { length = len; width = wid; public double getarea() { return length * width; public double getperimeter () { double p = 2.0*( length + width); return p; 2. Copy the DiceRunner class and complete the Dice class. Compile and run. Set a breakpoint to check if the doubles method works correctly. public class DiceRunner { public static void main(string[] args) { Dice d = new Dice(); d.roll(); int n = d.gettotal(); System.out.println( n ); if ( d.doubles() ) System.out.println( "Yes" ); d.roll(); n = d.gettotal(); System.out.println( n ); public void bigger( double f ){ length = f * length; width = f * width; public class Dice { private int die1, die2; public Dice() { die1 = 0; die2 = 0; public void roll() { Assign die1 a random integer value between 1 and 6. Do the same thing for die2. Use Math.random(). public int gettotal() { return the sum of die1 and die2

2 if ( d.doubles() ) System.out.println( "Yes" ); public boolean doubles() { return true if both die are the same; otherwise return false. 3. Write another class (with a main method) in the same location as the files you created for Problem 2. In this main method, create one Dice object and roll the dice 100 times (in other words, call the roll method followed by the gettotal method 100 times). Count how many times you roll a 7 or 11. Also count how many times you rolled a 2, 3, or 12. And count how many doubles you got. Display the results as percents. For example: You rolled a 7 or 11 21% of the time. You rolled a 2, 3, or 12 13% of the time. You rolled doubles 15% of the time Note. Take advantage of the fact that you are rolling the dice 100 times to calculate the percentages 4. Copy the Circle class and complete the two methods to the right. getc should return the circumference of the circle and changer should assign the value of the parameter to the radius. Write a second class with a main method. In the main method: - create a Circle object with a radius of 3. - call the getc method and display the returned value (it should be ) - call the getarea method and display the returned value (it should be ) - call the changer method and change the circle s radius to 1. - call the getarea method again and display the returned value (it should 5. Write the Employee class so that when the main method in RunEmployee is executed, the following is displayed. Salary is Salary is Salary is public class Circle { private double radius; public Circle( double r ) { radius = r; public double getarea() { double a = Math.PI * radius * radius; return a; public double getc () { it returns the circle s circumference public void changer( double r ) { changes the value of radius to r public class RunEmployee { public static void main(string[] args) { Employee e = new Employee( ); e.increase( ); double p = e.getpay(); System.out.println( "Salary is " + p ); e.increase( );

3 The Employee class has one instance variable for storing an Employee s salary. p = e.getpay(); System.out.println( "Salary is " + p ); Note: the increase method should change the value of the instance variable. The getpay method simply returns the value of the instance variable. e.increase( ); p = e.getpay(); System.out.println( "Salary is " + p ); 6. Finish the Travel class. Then write a second class that has a main method. In the main method: - Write a loop that iterates 21 times with the counter going from 6 to 40 in steps of two. - In the body of the loop, create a Travel object and call each method. Print the returned values. - The output should look like this: people = 6, vans = 1, canoes = 2, planes = 1 people = 8, vans = 1, canoes = 3, planes = 1 people = 10, vans = 2, canoes = 4, planes = 1 people = 12, vans = 2, canoes = 4, planes = 1 people = 14, vans = 2, canoes = 5, planes = 2... people = 32, vans = 4, canoes = 11, planes = 3 people = 34, vans = 5, canoes = 12, planes = 3 people = 36, vans = 5, canoes = 12, planes = 3 people = 38, vans = 5, canoes = 13, planes = 4 people = 40, vans = 5, canoes = 14, planes = 4 7. The Prism class represents a rectangular prism. Complete the two methods. 8. Write another class that has a main method. In the main method: -Create a Scanner object and have the user enter three integers. - Instantiate a Prism object using those three integers. - Call the surfacearea method and print the returned value. - Call the issmall method. If it returns true, print This is a small prism. If the method returns false, print This is not a small prism. Here is sample output. public class Travel { private int people; public Travel( int n ) { people = n; public int gobyvan(){ this returns the number of vans required to transport everyone (8 per van) public int gobycanoe(){ this returns the number of canoes required (3 per canoe) public int gobyplane() { this returns the number of planes required (12 per plane) public class Prism{ private int len, w, h; public Prism( int a, int b, int c ) { len = a; w = b; h = c; public int surfacearea(){ Calculates and returns the surface area of the prism. public boolean issmall(){

4 Enter the length, width, height Its surface area is 94 square units This is a small prism 9. Copy the Car class. In another class s main method, create two car objects. Write a while loop where the drive method is called for each car and their locations are printed. When a car has gone over 200 miles, the loop stops and that car is the winner. Here is one sample output. Car 1: 4 miles, Car 2: 36 miles. Car 1: 14 miles, Car 2: 71 miles. Car 1: 17 miles, Car 2: 75 miles. Car 1: 156 miles, Car 2: 178 miles. Car 1: 167 miles, Car 2: 217 miles. Car 2 wins But obviously sometimes Car 1 will win and there could even be a tie. 10. First complete the Number class. Then we play the guessing game. Each turn the user gets to ask if the secret number is a multiple of some number and then they get to guess the secret number. Here's a rough outline. public class Runner { public static void main(string[] args) { create a Scanner object create a Number object boolean keepplaying = true; int count = 0; while ( keepplaying ) { count++; Ask the user to enter a positive integer. Get response and call the multipleof method. Print a message that indicates if the secret number is a multiple of that input or not Ask the user to guess what the secret number is. Call the guess method. If the user s right then print Congratulations, print count, and set keepplaying to false (to get out of the loop). If the user is wrong, then indicate that. Calculates the volume of the prism. If the volume is less than 75 cubic units, then this is a small prism and the method returns true. Otherwise the method returns false. public class Car { private int x; // location of the car public Car( ) { x = 0; public int getx(){ return x; public void drive() { x= x + (int)( 40*Math.random()); public class Number { private int num; public Number( ) { num = (int)(100*math.random() )+1; public boolean multipleof( int x ){ returns true if num is a multiple of x public boolean guess( int x ) { returns true if num equals x; otherwise it returns false Enter an integer: 2 The number is NOT a multiple of 2 Guess the secret number: 13 That s not it. Enter an integer: 3 The number is a multiple of 3

5 Guess the secret number: 3 That s not it. To the right is one possible outcome (though it will probably take more than 3 tries to guess the number). Enter an integer:5 The number is a multiple of 5 Guess the secret number: 45 You to it in 3 tries. 11. The Line class objects represent simple linear equations that can be written in slope-intercept form. Complete the two methods of the Line class. 12. Write another class that has a main method. In the main method, create a Scanner object and write a loop that repeats five times. In the loop: Create a random slope (using Math.random) that is one of the following values: -3.0, -2.5, -2.0, , 3.0 If you get stuck on this, the code is below*. Create a random y-intercept that is an integer between -3 and +3. Create a Line object using the random slope and y-intercept. Call the display method. Generate a random x value between 0 and 10. Call the gety method and use this random value. Display the x and y values. Have the user enter x and y values. Call the issolution method to check if those values are a solution to the linear equation or not. Display an appropriate message. Print a blank line. * Here is one way to generate the random decimal values for the slope. int num = (int) (13*Math.random() ) - 6; double slope = 0.5 * num; public class Line{ public double slope, intercept; public Line( double m, double b ){ slope = m; intercept = b; public void display(){ if ( slope == 0 ) SOP( "y = " + intercept ); else if ( intercept < 0 ) SOP("y = " + slope +"x " +intercept); else if ( intercept == 0 ) SOP( "y = " + slope + "x" ); else SOP("y = " + slope + "x +" + intercept); public double gety( double x ){ Given a value for x, return the corresponding value for y public boolean issolution(double x, double y ){ Returns true if the parameters are a solution of the Line. Otherwise it returns false. Your solution may sometimes return false when it should return true due to small errors that can arise working with doubles. Ignore this problem or ask me how to solve it. Here s some sample output for this problem. y = -2.0x If x is 8.0, then y is Check if a point is a solution. Enter x then y (-1.5, 4.0 ) is a solution to this equation

6 y = 1.0x -2.0 If x is 1.0, then y is -1.0 Check if a point is a solution. Enter x then y (0.0, 2.5) is NOT a solution to this equation y = -1.0x -2.0 If x is 7.0, then y is -9.0 Check if a point is a solution. Enter x then y (7.0, -9.0) is a solution to this equation 13. Complete the move method of the GamePiece class. 14. Write another class that has a main method. In the main method, create two GamePiece objects. Keep calling the move method for each object until one of the two objects gets to 21. The output should look something like this: Player 1 has 4, Player 2 has 2 Player 1 has 8, Player 2 has 6 Player 1 has 9, Player 2 has 11 Player 1 has 16, Player 2 has 17 Player 1 has 17, Player 2 has 20 Player 1 has 18, Player 2 has 5 Player 1 has 3, Player 2 has 9 Player 1 has 8, Player 2 has 10 Player 1 has 12, Player 2 has 16 Player 1 has 14, Player 2 has 19 Player 1 has 16, Player 2 has 1 Player 1 has 21, Player 2 has 3 Player 1 wins public class GamePiece{ private int number; public GamePiece(){ number = 0; public void move(){ If number is 21, then do nothing. Otherwise, generate a random integer between 1 and 7 and add this to the instance variable. However, the instance variable should never have a value greater than 21. If it is 22, then reduce it to 1. If it is 23, then it becomes 2, and so on. public int getnumber(){ return number;

Introduction to Java Programs for Packet #4: Classes and Objects

Introduction to Java Programs for Packet #4: Classes and Objects Introduction to Java Programs for Packet #4: Classes and Objects Note. All of these programs involve writing and using more than one class file. 1. Copy the Box class and compile it. But you won t be able

More information

Introduction to Computer Science Unit 3. Programs

Introduction to Computer Science Unit 3. Programs Introduction to Computer Science Unit 3. Programs This section must be updated to work with repl.it Programs 1 to 4 require you to use the mod, %, operator. 1. Let the user enter an integer. Your 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

AP CS Unit 4: Classes and Objects Programs

AP CS Unit 4: Classes and Objects Programs AP CS Unit 4: Classes and Objects Programs 1. Copy the Bucket class. Make sure it compiles (but you won t be able to run it because it does not have a main method). public class Bucket { private double

More information

1 Short Answer (10 Points Each)

1 Short Answer (10 Points Each) 1 Short Answer (10 Points Each) 1. Write a for loop that will calculate a factorial. Assume that the value n has been input by the user and have the loop create n! and store it in the variable fact. Recall

More information

Introduction to Computer Science Unit 5. Programs: Strings

Introduction to Computer Science Unit 5. Programs: Strings Introduction to Computer Science Unit 5. Programs: Strings This section must be updated to work with repl.it 1. Copy RunNames and complete the two methods in the Name class. public class Name{ private

More information

AP Computer Science Unit 1. Programs

AP Computer Science Unit 1. Programs AP Computer Science Unit 1. Programs Open DrJava. Under the File menu click on New Java Class and the window to the right should appear. Fill in the information as shown and click OK. This code is generated

More information

COMP 110 Programming Exercise: Simulation of the Game of Craps

COMP 110 Programming Exercise: Simulation of the Game of Craps COMP 110 Programming Exercise: Simulation of the Game of Craps Craps is a game of chance played by rolling two dice for a series of rolls and placing bets on the outcomes. The background on probability,

More information

Lab 9: Creating a Reusable Class

Lab 9: Creating a Reusable Class Lab 9: Creating a Reusable Class Objective This will introduce the student to creating custom, reusable classes This will introduce the student to using the custom, reusable class This will reinforce programming

More information

Introduction to the Java Basics: Control Flow Statements

Introduction to the Java Basics: Control Flow Statements Lesson 3: Introduction to the Java Basics: Control Flow Statements Repetition Structures THEORY Variable Assignment You can only assign a value to a variable that is consistent with the variable s declared

More information

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous Assignment 3 Methods Review CSC 123 Fall 2018 Notes: All homework must be submitted via e-mail. All parts of assignment must be submitted in a single e-mail with multiple attachments when required. Notes:

More information

1 Short Answer (2 Points Each)

1 Short Answer (2 Points Each) Fall 013 Exam # Key COSC 117 1 Short Answer ( Points Each) 1. What is the scope of a method/function parameter? The scope of a method/function parameter is in the method only, that is, it is local to the

More information

AP Computer Science Unit 3. Programs

AP Computer Science Unit 3. Programs AP Computer Science Unit 3. Programs For most of these programs I m asking that you to limit what you print to the screen. This will help me in quickly running some tests on your code once you submit them

More information

Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2

Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2 Warm up Exercise What are the types and values of the following expressions: 3.0+4 * (3 + 1) 3 / 2 + 1.0 1.0 / 2.0 (int)1.0 / 2 COMP-202 - Programming Basics 1 Warm up Exercise What are the types and values

More information

CS Week 13. Jim Williams, PhD

CS Week 13. Jim Williams, PhD CS 200 - Week 13 Jim Williams, PhD This Week 1. Team Lab: Instantiable Class 2. BP2 Strategy 3. Lecture: Classes as templates BP2 Strategy 1. M1: 2 of 3 milestone tests didn't require reading a file. 2.

More information

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section: 7 Arrays Now go, write it before them in a table, and note it in a book. Isaiah 30:8 To go beyond is as wrong as to fall short. Confucius Begin at the beginning, and go on till you come to the end: then

More information

Introduction to Computer Science Unit 2. Notes

Introduction to Computer Science Unit 2. Notes Introduction to Computer Science Unit 2. Notes Name: Objectives: By the completion of this packet, students should be able to describe the difference between.java and.class files and the JVM. create and

More information

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function Due: Mar25 (Note that this is a 2-week lab) This lab must be done using paired partners. You should choose a different partner

More information

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions: Name: New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case Instructions: KEEP TEST BOOKLET CLOSED UNTIL YOU ARE INSTRUCTED TO BEGIN. This exam is double sided (front

More information

2.2 - Making Decisions

2.2 - Making Decisions 2.2 - Making Decisions So far we have only made programs that execute the statements in order, starting with the statements at the top of the screen and moving down. However, you can write programs that

More information

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ AP Computer Science Unit 1. Writing Programs Using BlueJ 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save

More information

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012 CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012 Name: This exam consists of 6 problems on the following 7 pages. You may use your two-sided hand-written 8 ½ x 11 note sheet during the exam.

More information

PROGRAMMING FUNDAMENTALS

PROGRAMMING FUNDAMENTALS PROGRAMMING FUNDAMENTALS Q1. Name any two Object Oriented Programming languages? Q2. Why is java called a platform independent language? Q3. Elaborate the java Compilation process. Q4. Why do we write

More information

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

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade; Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The

More information

Introduction to Computer Science Unit 2. Exercises

Introduction to Computer Science Unit 2. Exercises Introduction to Computer Science Unit 2. Exercises Note: Curly brackets { are optional if there is only one statement associated with the if (or ) statement. 1. If the user enters 82, what is 2. If the

More information

static String usersname; public static int numberofplayers; private static double velocity, time;

static String usersname; public static int numberofplayers; private static double velocity, time; A class can include other things besides subroutines. In particular, it can also include variable declarations. Of course, you can declare variables inside subroutines. Those are called local variables.

More information

Introduction to Computer Programming

Introduction to Computer Programming Introduction to Computer Programming Lecture #7 - Conditional Loops The Problem with Counting Loops Many jobs involving the computer require repetition, and that this can be implemented using loops. Counting

More information

BLOCK STRUCTURE. class block main method block do-while statement block if statement block. if statement block. Block Structure Page 1

BLOCK STRUCTURE. class block main method block do-while statement block if statement block. if statement block. Block Structure Page 1 BLOCK STRUCTURE A block is a bundle of statements in a computer program that can include declarations and executable statements. A programming language is block structured if it (1) allows blocks to be

More information

1 Short Answer (5 Points Each)

1 Short Answer (5 Points Each) 1 Short Answer (5 Points Each) 1. Write a declaration of an array of 300 strings. String strarray[] = new String[300];. Write a method that takes in an integer n as a parameter and returns one half of

More information

[Page 177 (continued)] a. if ( age >= 65 ); cout << "Age is greater than or equal to 65" << endl; else cout << "Age is less than 65 << endl";

[Page 177 (continued)] a. if ( age >= 65 ); cout << Age is greater than or equal to 65 << endl; else cout << Age is less than 65 << endl; Page 1 of 10 [Page 177 (continued)] Exercises 4.11 Identify and correct the error(s) in each of the following: a. if ( age >= 65 ); cout

More information

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions: Name: New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case Instructions: KEEP TEST BOOKLET CLOSED UNTIL YOU ARE INSTRUCTED TO BEGIN. This exam is double sided (front

More information

McGill University School of Computer Science COMP-202A Introduction to Computing 1

McGill University School of Computer Science COMP-202A Introduction to Computing 1 McGill University School of Computer Science COMP-202A Introduction to Computing 1 Midterm Exam Thursday, October 26, 2006, 18:00-20:00 (6:00 8:00 PM) Instructors: Mathieu Petitpas, Shah Asaduzzaman, Sherif

More information

Chapter 6. Repetition Statements. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Chapter 6. Repetition Statements. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6 Repetition Statements Animated Version required for reproduction or display. Chapter 6-1 Objectives After you have read and studied this chapter, you should be able to Implement repetition control

More information

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed 1 1 COMP200 ABSTRACT CLASSES OOP using Java, from slides by Shayan Javed Abstract Classes 2 3 From the previous lecture: public class GeometricObject { protected String Color; protected String name; protected

More information

Introduction to Computer Science Unit 2. Notes

Introduction to Computer Science Unit 2. Notes Introduction to Computer Science Unit 2. Notes Name: Objectives: By the completion of this packet, students should be able to describe the difference between.java and.class files and the JVM. create and

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

Introduction to Java Unit 1. Using BlueJ to Write Programs

Introduction to Java Unit 1. Using BlueJ to Write Programs Introduction to Java Unit 1. Using BlueJ to Write Programs 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save

More information

University of Massachusetts Amherst, Electrical and Computer Engineering

University of Massachusetts Amherst, Electrical and Computer Engineering University of Massachusetts Amherst, Electrical and Computer Engineering ECE 122 Midterm Exam 1 Makeup Answer key March 2, 2018 Instructions: Closed book, Calculators allowed; Duration:120 minutes; Write

More information

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #2

More information

Lesson 26: Volume of Composite Three-Dimensional Objects

Lesson 26: Volume of Composite Three-Dimensional Objects Classwork Example 1 Find the volume of the following three-dimensional object composed of two right rectangular prisms. Exercise 1 Find the volume of the following three-dimensional figure composed of

More information

Practice Midterm 1. Problem Points Score TOTAL 50

Practice Midterm 1. Problem Points Score TOTAL 50 CS 120 Software Design I Spring 2019 Practice Midterm 1 University of Wisconsin - La Crosse February 25 NAME: Do not turn the page until instructed to do so. This booklet contains 10 pages including the

More information

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation. Chapter 4 Lab Loops and Files Lab Objectives Be able to convert an algorithm using control structures into Java Be able to write a while loop Be able to write an do-while loop Be able to write a for loop

More information

for Middle School

for Middle School for Middle School Grid Games for Middle School Melisa Rice A publication of GridGamesGalore 8087 County Road 0, Ada, OK 780 Grid Games for Middle School Grid Games is an assortment of fun activities covering

More information

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ AP Computer Science Unit 1. Writing Programs Using BlueJ 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save

More information

CONDITIONAL EXECUTION

CONDITIONAL EXECUTION CONDITIONAL EXECUTION yes x > y? no max = x; max = y; logical AND logical OR logical NOT &&! Fundamentals of Computer Science I Outline Conditional Execution if then if then Nested if then statements Comparisons

More information

CS 120 Fall 2008 Practice Final Exam v1.0m. Name: Model Solution. True/False Section, 20 points: 10 true/false, 2 points each

CS 120 Fall 2008 Practice Final Exam v1.0m. Name: Model Solution. True/False Section, 20 points: 10 true/false, 2 points each CS 120 Fall 2008 Practice Final Exam v1.0m Name: Model Solution True/False Section, 20 points: 10 true/false, 2 points each Multiple Choice Section, 32 points: 8 multiple choice, 4 points each Code Tracing

More information

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof Abstract Class Lecture 21 Based on Slides of Dr. Norazah Yusof 1 Abstract Class Abstract class is a class with one or more abstract methods. The abstract method Method signature without implementation

More information

2 + (-2) = 0. Hinojosa 7 th. Math Vocabulary Words. Unit 1. Word Definition Picture. The opposite of a number. Additive Inverse

2 + (-2) = 0. Hinojosa 7 th. Math Vocabulary Words. Unit 1. Word Definition Picture. The opposite of a number. Additive Inverse Unit 1 Word Definition Picture Additive Inverse The opposite of a number 2 + (-2) = 0 Equal Amount The same in quantity = Fraction A number in the form a/b, where b 0. Half One of two equal parts of a

More information

CIS 120 Midterm II March 31, 2017 SOLUTIONS

CIS 120 Midterm II March 31, 2017 SOLUTIONS CIS 120 Midterm II March 31, 2017 SOLUTIONS 1 1. OCaml and Java (14 points) Check one box for each part. a. The following OCaml function will terminate by exhausting stack space if called as loop 10: let

More information

Chapter Goals. Contents LOOPS

Chapter Goals. Contents LOOPS CHAPTER 4 LOOPS Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 30, 2011 Chapter Goals To implement while, for, and do loops To hand-trace the execution of a program To become familiar with common

More information

1 Short Answer (15 Points Each)

1 Short Answer (15 Points Each) Name: Write all of your responses on these exam pages. If you need extra space please use the backs of the pages. 1 Short Answer (15 Points Each) 1. Write the following Java declarations, (a) A double

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

Geometry Workbook WALCH PUBLISHING

Geometry Workbook WALCH PUBLISHING Geometry Workbook WALCH PUBLISHING Table of Contents To the Student..............................vii Unit 1: Lines and Triangles Activity 1 Dimensions............................. 1 Activity 2 Parallel

More information

COMP 202 Java in one week

COMP 202 Java in one week CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator COMP 202 Java in one week The Java Programming Language A programming language

More information

CS1150 Principles of Computer Science Loops (Part II)

CS1150 Principles of Computer Science Loops (Part II) CS1150 Principles of Computer Science Loops (Part II) Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Review Is this an infinite loop? Why (not)?

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6 1 http://xkcd.com/221/ 2 Randomness Lack of predictability: don't know what's coming next Random process: outcomes do not

More information

Surface Area and Volume

Surface Area and Volume Surface Area and Volume Day 1 - Surface Area of Prisms Surface Area = The total area of the surface of a three-dimensional object (Or think of it as the amount of paper you ll need to wrap the shape.)

More information

2009 Fall Startup Event Thursday, September 24th, 2009

2009 Fall Startup Event Thursday, September 24th, 2009 009 Fall Startup Event This test consists of 00 problems to be solved in 0 minutes. All answers must be exact, complete, and in simplest form. To ensure consistent grading, if you get a decimal, mixed

More information

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function Due: Mar13 (Note that this is a 2-week lab) This lab must be done using paired partners. You should choose a different partner

More information

AP CS Unit 3: Control Structures Notes

AP CS Unit 3: Control Structures Notes AP CS Unit 3: Control Structures Notes The if and if-else Statements. These statements are called control statements because they control whether a particular block of code is executed or not. Some texts

More information

Quiz 1 Unit 5A Arrays/Static Name

Quiz 1 Unit 5A Arrays/Static Name Quiz 1 Unit 5A Arrays/Static Name 1. What values are stored in arr after the following code segment has been executed? int[] arr = 1, 2, 3, 4, 5, 6, 7, 8; for (int k = 1; k

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

Problem Grade Total

Problem Grade Total CS 101, Prof. Loftin: Final Exam, May 11, 2009 Name: All your work should be done on the pages provided. Scratch paper is available, but you should present everything which is to be graded on the pages

More information

Introduction to Computer Science Exercises for Unit 4A: Classes and Objects 1. This compiles and runs. What is displayed?

Introduction to Computer Science Exercises for Unit 4A: Classes and Objects 1. This compiles and runs. What is displayed? Introduction to Computer Science Exercises for Unit 4A: Classes and Objects 1. This compiles and runs. What is 2. This compiles and runs. What percent of the time will s be true? What percent of the time

More information

AP CS Unit 6: Inheritance Programs

AP CS Unit 6: Inheritance Programs AP CS Unit 6: Inheritance Programs Program 1. Complete the Rectangle class. The Rectangle public class Rectangle{ class represents private int x1, y1, x2, y2; a rectangle in a standard coordinate plane

More information

M e t h o d s a n d P a r a m e t e r s

M e t h o d s a n d P a r a m e t e r s M e t h o d s a n d P a r a m e t e r s Objective #1: Call methods. Methods are reusable sections of code that perform actions. Many methods come from classes that are built into the Java language. For

More information

Assignment 2.4: Loops

Assignment 2.4: Loops Writing Programs that Use the Terminal 0. Writing to the Terminal Assignment 2.4: Loops In this project, we will be sending our answers to the terminal for the user to see. To write numbers and text to

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Conditional Statements Boolean Expressions and Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Conditional Statements Boolean Expressions and Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

More information

assertion: A statement that is either true or false.

assertion: A statement that is either true or false. Logical assertions assertion: A statement that is either true or false. Examples: Java was created in 1995. The sky is purple. 23 is a prime number. 10 is greater than 20. x divided by 2 equals 7. (depends

More information

APCS Semester #1 Final Exam Practice Problems

APCS Semester #1 Final Exam Practice Problems Name: Date: Per: AP Computer Science, Mr. Ferraro APCS Semester #1 Final Exam Practice Problems The problems here are to get you thinking about topics we ve visited thus far in preparation for the semester

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 5 Lecture 5-4: do/while loops, assertions reading: 5.1, 5.5 1 The do/while loop do/while loop: Performs its test at the end of each repetition. Guarantees that the loop's

More information

Java Outline (Upto Exam 2)

Java Outline (Upto Exam 2) Java Outline (Upto Exam 2) Part 4 IF s (Branches) and Loops Chapter 12/13 (The if Statement) Hand in Program Assignment#1 (12 marks): Create a program called Ifs that will do the following: 1. Ask the

More information

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

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes COMP200 - Object Oriented Programming: Test One Duration - 60 minutes Study the following class and answer the questions that follow: package shapes3d; public class Circular3DShape { private double radius;

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 5 Random Numbers reading: 5.1, 5.6 1 http://xkcd.com/221/ 2 The while loop while loop: Repeatedly executes its body as long as a logical test is true. while (test) { statement(s);

More information

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Motivations If you assigned a negative value for radius

More information

Static Methods. Why use methods?

Static Methods. Why use methods? Static Methods A method is just a collection of code. They are also called functions or procedures. It provides a way to break a larger program up into smaller, reusable chunks. This also has the benefit

More information

int a; int b = 3; for (a = 0; a < 8 b < 20; a++) {a = a + b; b = b + a;}

int a; int b = 3; for (a = 0; a < 8 b < 20; a++) {a = a + b; b = b + a;} 1. What does mystery(3) return? public int mystery (int n) { int m = 0; while (n > 1) {if (n % 2 == 0) n = n / 2; else n = 3 * n + 1; m = m + 1;} return m; } (a) 0 (b) 1 (c) 6 (d) (*) 7 (e) 8 2. What are

More information

Java GUI Test #1 Solutions 7/10/2015

Java GUI Test #1 Solutions 7/10/2015 SI@UCF Java GUI Test #1 Solutions 7/10/2015 1) (12 pts) Jimmy's box for crayons has dimensions L x W x H. Each crayon he puts in has a height of exactly H. Thus, he stands each crayon up in the box forming

More information

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Repe$$on CSC 121 Spring 2017 Howard Rosenthal Repe$$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when

More information

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char Review Primitive Data Types & Variables int, long float, double boolean char String Mathematical operators: + - * / % Comparison: < > = == 1 1.3 Conditionals and Loops Introduction to Programming in

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

1 Short Answer (10 Points Each)

1 Short Answer (10 Points Each) 1 Short Answer (10 Points Each) 1. For the following one-dimensional array, show the final array state after each pass of the three sorting algorithms. That is, after each iteration of the outside loop

More information

Recommended Group Brainstorm (NO computers during this time)

Recommended Group Brainstorm (NO computers during this time) Recommended Group Brainstorm (NO computers during this time) Good programmers think before they begin coding. Part I of this assignment involves brainstorming with a group of peers with no computers to

More information

Repetition, Looping. While Loop

Repetition, Looping. While Loop Repetition, Looping Last time we looked at how to use if-then statements to control the flow of a program. In this section we will look at different ways to repeat blocks of statements. Such repetitions

More information

CLASSES AND OBJECTS. Fundamentals of Computer Science I

CLASSES AND OBJECTS. Fundamentals of Computer Science I CLASSES AND OBJECTS Fundamentals of Computer Science I Outline Primitive types Creating your own data types Classes Objects Instance variables Instance methods Constructors Arrays of objects A Foundation

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 5 Lecture 5-4: do/while loops, assertions reading: 5.1, 5.5 1 The do/while loop do/while loop: Performs its test at the end of each repetition. Guarantees that the loop's

More information

CC112 Structured Programming

CC112 Structured Programming Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer Engineering Department CC112 Structured Programming Lecture 3 1 LECTURE 3 Input / output operations

More information

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

! definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. ! We often use language like Indefinite loops while loop! indefinite loop: A loop where it is not obvious in advance how many times it will execute.! We often use language like " "Keep looping as long as or while this condition is

More information

5. What is a block statement? A block statement is a segment of code between {}.

5. What is a block statement? A block statement is a segment of code between {}. COSC 117 Exam 1 Key Fall 2012 Part 1: Definitions & Short Answer (3 Points Each) 1. What does CPU stand for? Central Processing Unit 2. Explain the difference between high-level languages and machine language.

More information

Introduction to Classes and Objects. David Greenstein Monta Vista High School

Introduction to Classes and Objects. David Greenstein Monta Vista High School Introduction to Classes and Objects David Greenstein Monta Vista High School Client Class A client class is one that constructs and uses objects of another class. B is a client of A public class A private

More information

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18 Assignment Lecture 9 Logical Operations Formatted Print Printf Increment and decrement Read through 3.9, 3.10 Read 4.1. 4.2, 4.3 Go through checkpoint exercise 4.1 Logical Operations - Motivation Logical

More information

Give one example where you might wish to use a three dimensional array

Give one example where you might wish to use a three dimensional array CS 110: INTRODUCTION TO COMPUTER SCIENCE SAMPLE TEST 3 TIME ALLOWED: 60 MINUTES Student s Name: MAXIMUM MARK 100 NOTE: Unless otherwise stated, the questions are with reference to the Java Programming

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

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm CIS 110 Introduction To Computer Programming February 29, 2012 Midterm Name: Recitation # (e.g. 201): Pennkey (e.g. bjbrown): My signature below certifies that I have complied with the University of Pennsylvania

More information

Lesson 26: Volume of Composite Three-Dimensional Objects

Lesson 26: Volume of Composite Three-Dimensional Objects Lesson 26: Volume of Composite Three-Dimensional Objects Student Outcomes Students compute volumes of three-dimensional objects composed of right prisms by using the fact that volume is additive. Lesson

More information

Name ANSWER KEY Date Period Score. Place your answers in the answer column. Show work clearly and neatly. FOCUS ON WHAT IS NECESSARY FOR SUCCESS!

Name ANSWER KEY Date Period Score. Place your answers in the answer column. Show work clearly and neatly. FOCUS ON WHAT IS NECESSARY FOR SUCCESS! OAKS Test Review PRACTICE Name ANSWER KEY Date Period Score Place your answers in the answer column. Show work clearly and neatly. FOCUS ON WHAT IS NECESSARY FOR SUCCESS! Solve:. n = 5. 4(4x ) = (x + 4)

More information

static int min(int a, int b) Returns the smaller of two int values. static double pow(double a,

static int min(int a, int b) Returns the smaller of two int values. static double pow(double a, The (Outsource: Supplement) The includes a number of constants and methods you can use to perform common mathematical functions. A commonly used constant found in the Math class is Math.PI which is defined

More information

The Math Class (Outsource: Math Class Supplement) Random Numbers. Lab 06 Math Class

The Math Class (Outsource: Math Class Supplement) Random Numbers. Lab 06 Math Class The (Outsource: Supplement) The includes a number of constants and methods you can use to perform common mathematical functions. A commonly used constant found in the Math class is Math.PI which is defined

More information

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding Java Class Design Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding eugeny.berkunsky@gmail.com http://www.berkut.mk.ua Objectives Implement encapsulation Implement inheritance

More information