Here are some review questions to give you extra practice with what you have lea
ID: 3664722 • Letter: H
Question
Here are some review questions to give you extra practice with what you have learned so far.
Complete the following Additional Activity for the Hailstone Series lab.
Implement a new static method declared as follows:
1
2
3
4
5
6
7
8
9
10
11
/**
* Repeatedly asks the user for a positive integer until the user enters
* one. Returns the positive integer.
*
* @param in
* the input stream
* @param out
* the output stream
* @return a positive integer entered by the user
*/
private static int getPositiveInteger(SimpleReader in, SimpleWriter out) {...}
Note that you cannot assume the user will provide a number; the user can type pretty much anything. So your method should read the input as a String (use SimpleReader.nextLine()), then make sure that the input is an integer number (use FormatChecker.canParseInt()), then convert the string to an integer (use Integer.parseInt()), and finally check that the integer is positive.
(This is modified from Review Exercise R4.19 at the end of Chapter 4 of Big Java Late Objects.) Using the kind of tracing tables discussed in Writing and Tracing Loops, provide tracing tables for these loops:
1
2
3
4
5
6
int n = 1;
int i = 2;
while (i < 5) {
n = n + i;
i = i + 1;
}
1
2
3
4
5
6
int i = 2;
double n = 1.0 / 2.0;
while (i <= 5) {
n = n + 1.0 / i;
i = i + 1;
}
1
2
3
4
5
6
double x = 1.0;
double y = 1.0;
while (x < 1.8) {
y = y / 2.0;
x = x + y;
}
1
2
3
4
5
6
int x = 3;
int y = 4;
while (y > 0) {
x = x + 1;
y = y - 1;
}
(This is from Programming Exercise P4.1 at the end of Chapter 4 of Big Java Late Objects.) Write program fragments (i.e., you do not need to write complete programs) with loops that compute:
The sum of all even numbers between 2 and 100 (inclusive).
The sum of all squares between 1 and 100 (inclusive).
All powers of 2 from 20 up to 220 (inclusive).
The sum of all odd numbers between a and b (inclusive), where a and b are integer variables with a b.
The sum of all digits at odd positions (right-to-left starting at 1 as the right-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 7 + 6 + 3 = 16.)
The sum of all digits at odd positions (left-to-right starting at 1 as the left-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 4 + 2 + 7 = 13.)
(This is from Review Exercise R5.2 at the end of Chapter 5 of Big Java Late Objects.) Write method headers for methods with the following descriptions.
Computing the larger of two integers
Computing the smallest of three real numbers
Checking whether an integer is a prime number, returning true if it is and false otherwise
Checking whether a string of characters is contained inside another string of characters
Computing the balance of an account with a given initial balance, an annual interest rate, and a number of years of earning interest
Printing the balance of an account with a given initial balance and an annual interest rate over a given number of years
Printing the calendar for a given month and year
Computing the weekday for a given day, month, and year (as a string such as "Monday")
Generating a random integer between 1 and n
(This is from Review Exercise R5.14 at the end of Chapter 5 of Big Java Late Objects.) Consider the following method that is intended to swap the values of two integers:
1
2
3
4
5
6
7
8
9
10
11
public static void falseSwap(int a, int b) {
int temp = a;
a = b;
b = temp;
}
public static void main(String[] args) {
int x = 3;
int y = 4;
falseSwap(x, y);
System.out.println(x + " " + y);
}
Why doesn’t the falseSwap method swap the contents of x and y?(This is modified from Programming Exercise P5.2 at the end of Chapter 5 of Big Java Late Objects.) Write the following methods and provide a program to test them.
boolean allTheSame(int x, int y, int z),
returning true if the arguments are all the same
boolean allDifferent(int x, int y, int z),
returning true if the arguments are all different
boolean sorted(int x, int y, int z),
returning true if the arguments are sorted with the smallest one coming first
LINK: https://www.dropbox.com/s/0nkezhvlfnzf2ww/Homework_%20Review%20I.html?dl=0
1
2
3
4
5
6
7
8
9
10
11
/**
* Repeatedly asks the user for a positive integer until the user enters
* one. Returns the positive integer.
*
* @param in
* the input stream
* @param out
* the output stream
* @return a positive integer entered by the user
*/
private static int getPositiveInteger(SimpleReader in, SimpleWriter out) {...}
Explanation / Answer
private static int getPositiveInteger(SimpleReader in, SimpleWriter out) {
int result;
while(true){
String input = in.nextLine();
input = input.trim();
try{
result = Integer.parseInt(input);
if(result>0) break;
}catch(Exception e){}
}
return result;
}
//////////////////////////////////////////////////
int n = 1;
int i = 2;
while (i < 5) {
n = n + i;
i = i + 1;
}
// Trace table
i = 2, n =1
i = 3, n =3
i = 4, n =6
i = 5, n =10
********************
int i = 2;
double n = 1.0 / 2.0;
while (i <= 5) {
n = n + 1.0 / i;
i = i + 1;
}
// Trace table
i = 2, n =0.5
i = 3, n =1.0
i = 4, n =1.3333333333333333
i = 5, n =1.5833333333333333
i = 6, n =1.7833333333333332
*********************
double x = 1.0;
double y = 1.0;
while (x < 1.8) {
y = y / 2.0;
x = x + y;
}
// Trace table
x = 1.0, y =1.0
x = 1.5, y =0.5
x = 1.75, y =0.25
x = 1.875, y =0.125
*****************************
int x = 3;
int y = 4;
while (y > 0) {
x = x + 1;
y = y - 1;
}
// Trace table
x = 3, y =4
x = 4, y =3
x = 5, y =2
x = 6, y =1
x = 7, y =0
//////////////////////////////////////////////////////
// The sum of all even numbers between 2 and 100 (inclusive).
int sum = 0;
for(int i=2;i<=100;i=i+2)
sum = sum + i;
System.out.println("sum = "+ sum);
// The sum of all squares between 1 and 100 (inclusive).
long sum = 0;
for(int i=1;i<=100;i++)
sum = sum + (long)Math.pow(i,2);
System.out.println("sum = "+ sum);
// All powers of 2 from 20 up to 220 (inclusive).
for(int i=20;i<=220;i++){
System.out.println(Math.pow(2,i));
}
//The sum of all odd numbers between a and b (inclusive), where a and b are integer variables with a b.
public long sumofOddsRange(int a, int b){
long sum =0;
for(int i=a;i<=b;i=i+2){
sum = sum + i;
}
return sum;
}
// The sum of all digits at odd positions (right-to-left starting at 1 as the right-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 7 + 6 + 3 = 16.)
public static int sumOfOddDigitsfromRight(long n){
char[] arr = (n+"").toCharArray();
int sum = 0;
for(int i=arr.length-1;i>=0;i=i-2)
sum = sum + Integer.parseInt(arr[i]+"");
return sum;
}
// The sum of all digits at odd positions (left-to-right starting at 1 as the left-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 4 + 2 + 7 = 13.)
public static int sumOfOddDigitsfromLeft(long n){
char[] arr = (n+"").toCharArray();
int sum = 0;
for(int i=0;i<arr.length;i=i+2)
sum = sum + Integer.parseInt(arr[i]+"");
return sum;
}
/////////////////////
Write method headers for methods with the following descriptions.
Computing the larger of two integers
-> public int biggestOfTwo(int a, int b){...}
Computing the smallest of three real numbers
-> public int smallestOfThree(int a, int b, int c){...}
Checking whether an integer is a prime number, returning true if it is and false otherwise
-> public boolean isPrime(int number){...}
Checking whether a string of characters is contained inside another string of characters
-> public boolean isSubString(String main, String part){...}
Computing the balance of an account with a given initial balance, an annual interest rate, and a number of years of earning interest
-> public double getBalance(double initial, double intrate, double years){...}
Printing the calendar for a given month and year
-> public void printCalendar(int month, int year){...}
Computing the weekday for a given day, month, and year (as a string such as "Monday")
-> public String getDay(int day, int month, int year){...}
Generating a random integer between 1 and n
-> public int getRandomNumberUptoN(int n){...}
///////////////
Why doesn’t the falseSwap method swap the contents of x and y?
It won't because we passed values to the parameters. swapping has been done in method with actuval parameters which are not same formal parameters sent to the method.
////
public class temp {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("allTheSame(2,3,4) -> "+ allTheSame(2,3,4));
System.out.println("allTheSame(2,2,4) -> "+ allTheSame(2,2,4));
System.out.println("allTheSame(2,2,2) -> "+ allTheSame(2,2,2));
System.out.println("allDifferent(2,3,4)"+allDifferent(2,3,4));
System.out.println("allDifferent(2,2,4)"+allDifferent(2,2,4));
System.out.println("allDifferent(2,2,2)"+allDifferent(2,2,2));
System.out.println("sorted(2,2,2)"+sorted(2,2,2));
System.out.println("sorted(2,3,4)"+sorted(2,3,4));
System.out.println("sorted(2,3,2)"+sorted(2,3,2));
}
static boolean allTheSame(int x, int y, int z){
return x==y && y==z;
}
static boolean allDifferent(int x, int y, int z){
return !(x==y || y==z);
}
static boolean sorted(int x, int y, int z){
return (x<=y)&&(y<=z);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.