Activity_05 - COMP 1210 Activity 5: Conditionals and Loops...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COMP 1210 Activity 5: Conditionals and Loops Page 1 of 4 Due: Activity (in-lab): Monday, February 20, 2012 by the end of lab Goals: By the end of this activity you should be able to do the following: Gain a further understanding of if-else statements Understand the basics of loops Description: In this activity you will create two classes. NumberOperations will hold an integer value and perform various operations on that value. Directions: Part 1: NumberOperations: Method Stubs (40%) • Create a class called NumberOperations. • Add method stubs for the following methods. The first two are given; do the rest on your own. o The constructor takes an int parameter called numberIn o getValue: takes no parameters; returns an int value Create method stubs with placeholder returns for each method on your own. o o o o oddsUnder: takes no parameters; returns a String powersTwoUnder: takes no parameters; returns a String isGreater: takes an int parameter called compareNumber; returns an int toString: takes no parameters; returns a String Compile NumberOperations and run the following in interactions. Do not continue until your program compiles and the following code runs without error in interactions. ϼÏÏNumberOperations numOps = new NumberOperations(5); ϼÏÏString s1 = numOps.oddsUnder(); ϼÏÏString s2 = numOps.powersTwoUnder(); ϼÏÏint n1 = numOps.isGreater(2); ϼÏÏString s3 = numOps.toString(); Part 2: NumberOperations: instance variable, Constructor, getValue, and toString (20%) • Add an instance variable with the name number to your class that is of type int. • In your constructor, add code that will set the value of number to numberIn. • In your getValue method, delete the placeholder return and return the value of number. • Replace the placeholder return in the toString method with the following code: [Note that the result of concatenating number, an int, with an empty String is a String.] COMP 1210 | Spring 2012 1 of 5 COMP 1210 Activity 5: Conditionals and Loops Page 2 of 4 Compile NumberOperations and run the following code in the interactions pane. Do not continue until the following code runs without error in interactions. ϼÏÏNumberOperations numOps = new NumberOperations(5); ϼÏÏnumOps.getValue() ÏÏÏÏ5 ϼÏÏnumOps // displays the toString return value ÏÏÏÏ5 Part 3: NumberOperations: oddsUnder Method (10%) • Create a local variable in oddsUnder called output and initialize it to an empty string literal. • Add a local int variable i and a while loop that will iterate through each value of i until the value of number. • Inside of the above loop, add code that will concatentate the value of i if it is an odd number. Also increment the value of i during each iteration of the loop. • After the loop, add code to return the value of output. Compile NumberOperations and run the following code in the interactions pane. Do not continue until the following code runs without error in interactions. ϼÏÏNumberOperations numOps = new NumberOperations(9); ϼÏÏnumOps.oddsUnder() ÏÏÏÏ1 3 5 7 Part 4: NumberOperations: powersTwoUnder Method (15%) • Create a local String variable in powersTwoUnder called output and initialize it to an empty string literal as you did in oddsUnder. • Create another local variable of type int called powers and initialize its value to 1. • Add a while loop that will iterate through each number up until the value of number. COMP 1210 | Spring 2012 2 of 5 COMP 1210 Activity 5: Conditionals and Loops Page 3 of 4 • Inside of the while loop, add code that will concatentate the value of powers to output if it is a power of 2 and then calculate the next power of two (the comments below are optional). • Add code to return the value of output. Compile NumberOperations and run the following code in the interactions pane. Do not continue until the following code runs without error in interactions. ϼÏÏNumberOperations numOps = new NumberOperations(20); ϼÏÏnumOps.powersTwoUnder() ÏÏÏÏ1 2 4 8 16 Part 4: NumberOperations: isGreater Method (15%) • Delete the placeholder return from isGreater and add code that will return 1 if number is greater than compareNumber, -1 if number is less than compareNumber, or 0 if the numbers are equal. Compile NumberOperations and run the following code in the interactions pane. Do not continue until the following code runs without error in interactions. ϼÏÏNumberOperations numOps = new NumberOperations(10); ϼÏÏnumOps.isGreater(2) ÏÏÏÏ1 ϼÏÏnumOps.isGreater(15) ÏÏÏÏ-1 ϼÏÏnumOps.isGreater(10) ÏÏÏÏ0 COMP 1210 | Spring 2012 3 of 5 COMP 1210 Activity 5: Conditionals and Loops Page 4 of 4 Part 5: NumberOpsList Class • Download the driver program called NumberOpsList and then add the indicated code (described in the // comments) so that it does the following: prompts the users for a number, creates a NumberOperations object for the number, adds the NumberOperations object to an ArrayList called numOps, and then prompts the user for the next number. Once the user enters a value of 0, print out each NumberOperations object in the ArrayList along with its “odds under” and its “powers of 2 under”. Example output: ÏÏ«Ï ----jGRASP exec: java -ea NumberOpsList ÏÏ§Ï ÏϧÏEnter a list of positive integers: ¼¼§Ï12 ¼¼§Ï9 ¼¼§Ï17 ¼¼§Ï0 ÏϧÏFor: 12 ÏÏ§Ï Odds under: 1 3 5 7 ÏÏ§Ï Powers of 2 under: 1 2 4 ÏϧÏFor: 9 ÏÏ§Ï Odds under: 1 3 5 7 ÏÏ§Ï Powers of 2 under: 1 2 4 ÏϧÏFor: 17 1 3 5 7 ÏÏ§Ï Odds under: ÏÏ§Ï Powers of 2 under: 1 2 4 ÏÏ§Ï ÏÏ©Ï ----jGRASP: operation complete. 9 8 11 8 9 8 11 16 13 COMP 1210 | Spring 2012 15 4 of 5 ...
View Full Document

This note was uploaded on 03/02/2012 for the course COMP 1210 taught by Professor Cross during the Winter '07 term at Auburn University.

Ask a homework question - tutors are online