Question

In: Computer Science

class Loops{ public void printNumbers(int low, int high){ // using a for loop, print all numbers...

class Loops{

public void printNumbers(int low, int high){
// using a for loop, print all numbers from low to high
for(int i = low; i <= high; i++){
System.out.println(i);
}
}
public int sumOfNumbers(int n){
// using a for loop, calculate and return the sum of first n numbers
// i.e n = 5, answer = 5+4+3+2+1 = 15
int sum = 0;
for(int i = 1; i <= n; i++){
sum += i;
}
return sum;
}
public void printMultiplicationTable(int num){
// using a for loop, print the multiplication table of num (up to first 10!)
// i.e. num = 5, 5*1=5, 5*2=10, 5*3=15, 5*4=20, 5*5=25, 5*6=30, 5*7=35...
for(int i = 1; i <= 10; i++){
System.out.println(num + " * " + i + "=" + (num *i));
}
}
public int getFactorialOfNum(int num){
// using for loop, calculate and return the factorial of number
// i.e. factorial of 4 is: 4*3*2*1 = 24
// i.e. factorial of 5 is: 5*4*3*2*1 = 120
for(int i = 1; i <= num; i++){
factorial = factorial * i;
}
System.out.println("Factorial of "+ num +" is: "+ factorial);
}
public int pow(int base, int power){
// using for loop, calculate base raised to power
// i.e. base = 2, power = 3 = 2^3 = 2*2*2 = 8
// i.e. base = 5, power = 4 = 5^4 = 5*5*5*5 = 625
int p = 1;
for(int i = 0; i < power; i++){
p *= base;
}
return p;
}
public String reverseMyString(String input){
// using a loop, reverse a string
// i.e. input = "hello", reversed answer: "olleh"
// i.e. input = "bye", reversed answer: "eyb"
String reverse = " ";
for(int i = 0; i < input.length(); i++){
reverse = input.chartAt(i) + reverse;
}
return reverse;
}
public boolean isEven(int num){
// return true if the number is even, false otherwise
for (int i = 0; i <= 10; i++){
if(i % 2 == 0) {
return true;
}
}
}
public boolean isOdd(int num){
// return true if the number is odd, false otherwise
for(int i = 0; i <= 10; i++){
if(i % 2 != 0){
return true;
}
}
}
public boolean isPrime(int num){
// return true if the number is prime, false otherwise
for(int i = 2; i < num; ++i){
if(num % 1 == 0){
return false;
}
}
return num > 1;
}
}

Write a LoopsDriver:

  • Write an infinite loop which does the following
    • Prints a menu of 10 options (9 methods that the user is allowed to call, 0 to exit)
    • Asks the user which method to call
    • Asks the user to enter the appropriate argument
      • reverseMyString needs a String from the user
      • isPrime needs an integer from the user
      • pow needs 2 integers from the user
    • Calls the method and prints out the answer

Solutions

Expert Solution

Java code

import java.util.Scanner;

class Loops {

    public void printNumbers(int low, int high) {

        // using a for loop, print all numbers from low to high

        for (int i = low; i <= high; i++) {

            System.out.println(i);

        }

    }

    public int sumOfNumbers(int n) {

        // using a for loop, calculate and return the sum of first n numbers

        // i.e n = 5, answer = 5+4+3+2+1 = 15

        int sum = 0;

        for (int i = 1; i <= n; i++) {

            sum += i;

        }

        return sum;

    }

    public void printMultiplicationTable(int num) {

        // using a for loop, print the multiplication table of num (up to first 10!)

        // i.e. num = 5, 5*1=5, 5*2=10, 5*3=15, 5*4=20, 5*5=25, 5*6=30, 5*7=35...

        for (int i = 1; i <= 10; i++) {

            System.out.println(num + " * " + i + "=" + (num * i));

        }

    }

    public void getFactorialOfNum(int num) {

        int factorial = 1;

        for (int i = 1; i <= num; i++) {

            factorial = factorial * i;

        }

        System.out.println("Factorial of " + num + " is: " + factorial);

    }

    public int pow(int base, int power) {

        // using for loop, calculate base raised to power

        // i.e. base = 2, power = 3 = 2^3 = 2*2*2 = 8

        // i.e. base = 5, power = 4 = 5^4 = 5*5*5*5 = 625

        int p = 1;

        for (int i = 0; i < power; i++) {

            p *= base;

        }

        return p;

    }

    public String reverseMyString(String input) {

        // using a loop, reverse a string

        // i.e. input = "hello", reversed answer: "olleh"

        // i.e. input = "bye", reversed answer: "eyb"

        String reverse = " ";

        for (int i = 0; i < input.length(); i++) {

            reverse = input.charAt(i) + reverse;

        }

        return reverse;

    }

    public boolean isEven(int num) {

        return num % 2 == 0;

    }

    public boolean isOdd(int num) {

        return num % 2 == 1;

    }

    public boolean isPrime(int num) {

        // return true if the number is prime, false otherwise

        for (int i = 2; i < num; ++i) {

            if (num % i == 0) {

                return false;

            }

        }

        return true;

    }

}

public class Main {

    public static void main(String[] args) {

        int choice, num1, num2;

        Scanner scan = new Scanner(System.in);

        Loops loop = new Loops();

        do {

            System.out.print(

                    "\n1. Print Numbers\n2.sum of numbers\n3.Print Multiplication Table\n4.Find factorial\n5.calculate power\n6.Reverse String\n7.check for even\n8.check for odd\n9.check for Prime\n0.Exit\nEnter choice : ");

            choice = scan.nextInt();

            switch (choice) {

                case 1: {

                    System.out.print("Enter value of low : ");

                    num1 = scan.nextInt();

                    System.out.print("Enter value of high : ");

                    num2 = scan.nextInt();

                    loop.printNumbers(num1, num2);

                    break;

                }

                case 2: {

                    System.out.print("Enter number : ");

                    num1 = scan.nextInt();

                    System.out.println(loop.sumOfNumbers(num1));

                    break;

                }

                case 3: {

                    System.out.print("Enter number : ");

                    num1 = scan.nextInt();

                    loop.printMultiplicationTable(num1);

                    break;

                }

                case 4: {

                    System.out.print("Enter number : ");

                    num1 = scan.nextInt();

                    loop.getFactorialOfNum(1);

                    break;

                }

                case 5: {

                    System.out.print("Enter number: ");

                    num1 = scan.nextInt();

                    System.out.print("Enter power : ");

                    num2 = scan.nextInt();

                    System.out.println(loop.pow(num1, num2));

                    break;

                }

                case 6: {

                    scan.nextLine();

                    System.out.print("Enter String : ");

                    String str = "";

                    str = scan.nextLine();

                    System.out.println(loop.reverseMyString(str));

                    break;

                }

                case 7: {

                    System.out.print("Enter number : ");

                    num1 = scan.nextInt();

                    System.out.println(loop.isEven(num1));

                    break;

                }

                case 8: {

                    System.out.print("Enter number : ");

                    num1 = scan.nextInt();

                    System.out.println(loop.isOdd(num1));

                    break;

                }

                case 9: {

                    System.out.print("Enter number : ");

                    num1 = scan.nextInt();

                    System.out.println(loop.isPrime(num1));

                    break;

                }

                case 0: {

                    System.out.println("Good Bye!");

                    break;

                }

                default: {

                    System.out.println("Invalid choice");

                }

            }

        } while (choice != 0);

        scan.close();

    }

}


Related Solutions

public void printNumbers(int low, int high) { // using a for loop, print all numbers from...
public void printNumbers(int low, int high) { // using a for loop, print all numbers from low to high } public int sumOfNumbers(int n) { // using a for loop, calculate and return the sum of first n numbers // i.e n = 5, answer = 5+4+3+2+1 = 15 } public void printMultiplicationTable(int num) { // using a for loop, print the multiplication table of num (up to first 10!) // i.e. num = 5, 5*1=5, 5*2=10, 5*3=15, 5*4=20, 5*5=25,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the array to have the following property:        Suppose the first element in the original array has the value x.        In the new array, suppose that x is in position i, that is data[i] = x.        Then, data[j] <= x for all j < I and data[j] > x for all j > i.        Thus, informally, all the...
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print...
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print the array double the value in the array at position start recMethod(array, start+1, end) print the array } else { print "done" } } Assume the method is invoked with array = [3, 4, 6, 7, 8, 10, 4], start = 2, and end = 5 1.How many times is the array printed before the word "done" is printed? 2.How many times is the...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index...
class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index - 1; // Decrementing the index System.out.printf("%d%n", a[index]); reverse(a, index); // Recursive call } return; } public static void main (String args[]) { int [] array = { 1, 2, 3, 4, 5 }; int n=array.length; reverse(array,n); // function call } } Write a generic version of the corrected recursive reverse method that could be used to print any of the following arrays (or...
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method...
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method receives the following arrangement: [1,2,3,4,5] The invert method returns the array [5,4,3,2,1] Example 2: the print method receives the following array: [5,4,3,2,1] The print method prints: 5,4,3,2,1 (data separated by commas). TIP: for the print method use System.out.print () without the "ln".
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
Previous Lab Code files: PersonType.h: #include <string> using namespace std; class personType { public: void print()...
Previous Lab Code files: PersonType.h: #include <string> using namespace std; class personType { public: void print() const; void setName(string first, string middle, string last);    void setLastName(string last);    void setFirstName(string first);    void setMiddleName(string middle);    bool isLastName(string last) const;    bool isFirstName(string first) const; string getFirstName() const;    string getMiddleName() const;    string getLastName() const; personType(string first = "", string middle = "", string last = ""); private: string firstName; string middleName; string lastName; }; PersonTypeImp: #include <iostream>...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT