Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with a comp sci activity, the Driver file that the class file needs is screenshotted too. Activity: Conditionals and Loops Page 1 of

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Need help with a comp sci activity, the Driver file that the class file needs is screenshotted too.

Activity: Conditionals and Loops Page 1 of 5 Goals: By the end of this activity you should be able to do the following: Gain a further understanding of if and if-else statements Understand the basics of while statements (a.k.a., while loops) Introduces the ArrayList class (java.util.ArrayList) Description: In this activity you will create a NumberOperations class which will hold an integer value and provide methods to perform various operations on that value. You will also download and modify a class called NumberOpsDriver which creates an ArrayList object and then reads in a value from the keyboard. While the value is not zero, the program creates an instance of the NumberOperations class for the value, adds the instance to the ArrayList, and then reads the next value from the keyboard. Directions: Part 1: NumberOperations: Method Stubs (a.k.a. skeleton code) Don't forget to add your Javadoc comments, the class, constructor and each method will need one. Create a class called NumberOperations with an instance variable called number of type int. = public class Numberoperations { | private int number; Add method stubs for the following methods. The first two are given; do the rest on your own. The constructor takes an int parameter called numberin public NumberOperations (int numberIn) { o getValue: takes no parameters, returns an int value public int getValue() - return 0; // placeholder return Create method stubs with placeholder returns for each method on your own. o o o o odds Under: takes no parameters, returns a String powersTwoUnder: takes no parameters, returns a String isGreater: takes an int parameter called compare Number, 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 runtime errors in interactions. Note that return values will be the placeholder values in the "stubbed" methods. NumberOperations numOps = new NumberOperations (5); String sl = numops.oddsUnder(); String s2 = numops.powersTwoUnder(); int nl = numops.isGreater (2); String s3 = numops.toString(); Activity: Conditionals and Loops Page 2 of 5 Part 2: NumberOperations: Constructor, getValue, and toString 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: return number + ""; [Note that the result of concatenating number, which is an int, with an empty String is a String] 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() numOps // displays the toString return value Part 3: NumberOperations: odds Under Method - Returns a String containing the positive odd integer's less than the value of number. Create a local variable in odds Under called output and initialize it to an empty string literal. public String odds Under() { String output = ""; L Add a local int variable i and a while loop that will iterate through the loop as long as the value of i is less than the value of number. int i = 0; while (i ArrayList numOpsList = new ArrayList(); // prompt user for set of numbers System.out.println("Enter a list of positive integers separated" + "with a space followed by 0:"); // get first user input using in.nextInt() // add a while loop as described below: // while the input is not equal to o // add a new NumberOperations object to numOpsList based on user input // get the next user input using in.nextInt() int index = 0; while (index

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions