Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class Assignment7 { public static void main(String[] args) { int number,

ID: 3550914 • Letter: P

Question

public class Assignment7

{

public static void main(String[] args)

{

int number, size;

String choice;

char command;


Scanner keyboard = new Scanner(System.in);

// ask a user for a array size

System.out.println("Please enter a size for the array. ");

size = keyboard.nextInt();


// instantiate a NumberCollection object

NumberCollection collection = new NumberCollection(size);


// print the menu

printMenu();


do

{

// ask a user to choose a command

System.out.println(" Please enter a command or type ?");

choice = keyboard.next().toLowerCase();

command = choice.charAt(0);


switch (command)

{

case 'a': // add a number

System.out.println(" Please enter an integer to add.");

number = keyboard.nextInt();

if(collection.addNumber(number))

System.out.println(" " + number + " successfully added.");

else

System.out.println(" " + number + " is already in the array. " + number + " was not added.");

break;

case 'b': // print the collection

System.out.println(" " + collection);

break;

case 'c': // compute and display the maximum

System.out.println(" The maximum is: " + collection.findMax());

break;

case 'd': // compute and display the minimum

System.out.println(" The minimum is: " + collection.findMin());

break;

case 'e': // compute and display the sum

System.out.println(" The sum is: " + collection.computeSum());

break;

case '?':

printMenu();

break;

case 'q':

break;

default:

System.out.println("Invalid input!");


}


} while (command != 'q');


} //end of the main method



// this method prints out the menu to a user

public static void printMenu()

{

System.out.print(" Command Options "

+ "----------------------------------- "

+ "a: add an integer in the array "

+ "b: display the array "

+ "c: compute and display the maximum "

+ "d: compute and display the minimum "

+ "e: compute and display the sum "

+ "?: display the menu again "

+ "q: quit this program ");


} // end of the printMenu method



} // end of the Assignment7 class

Explanation / Answer


package com.steves.chegg.java;

import java.util.ArrayList;
import java.util.Scanner;

class NumberCollection {
    int ara[];
    int index;

    public NumberCollection(int size) {
        this.ara = new int[size];

    }

    public boolean addNumber(int a){
        boolean flag=false;
        this.ara[index++]=a;
        flag=true;
        return flag;
            }
    public String toString(){
        StringBuffer str = new StringBuffer();
        str.append("The Collection is :");
        str.append(" ");
        for(int i=0;i<index;i++){
            str.append(this.ara[i]);
            str.append(" ");
        }
        return str.toString();
    }

    public int findMax(){
        int max=0;
        for(int i=0;i<this.ara.length;i++){
            if(this.ara[i]>max){
                max=this.ara[i];
            }
        }
        return max;
    }
    public int computeSum(){
       
        int sum=0;
        for(int i=0;i<this.ara.length;i++){
           
                sum=sum+this.ara[i];
           
        }
        return sum;
    }
    public int findMin(){
        int min=0;
        for(int i=0;i<this.ara.length;i++){
            if(this.ara[i]<min){
                min=this.ara[i];
            }
        }
        return min;
    }
}

public class Assignment6 {
    public static void main(String[] args) {
        int number, size;
        String choice;
        char command;

        Scanner keyboard = new Scanner(System.in);
        // ask a user for a array size
        System.out.println("Please enter a size for the array. ");
        size = keyboard.nextInt();

        // instantiate a NumberCollection object
        NumberCollection collection = new NumberCollection(size);

        // print the menu
        printMenu();

        do {
            // ask a user to choose a command
            System.out.println(" Please enter a command or type ?");
            choice = keyboard.next().toLowerCase();
            command = choice.charAt(0);

            switch (command) {
            case 'a': // add a number
                System.out.println(" Please enter an integer to add.");
                number = keyboard.nextInt();
                if (collection.addNumber(number))
                    System.out.println(" " + number + " successfully added.");
                else
                    System.out.println(" " + number
                            + " is already in the array. " + number
                            + " was not added.");
                break;
            case 'b': // print the collection
                System.out.println(" " + collection.toString());
                break;
            case 'c': // compute and display the maximum
                System.out.println(" The maximum is: " + collection.findMax());
                break;
            case 'd': // compute and display the minimum
                System.out.println(" The minimum is: " + collection.findMin());
                break;
            case 'e': // compute and display the sum
                System.out.println(" The sum is: " + collection.computeSum());
                break;
            case '?':
                printMenu();
                break;
            case 'q':
                break;
            default:
                System.out.println("Invalid input!");

            }

        } while (command != 'q');

    } // end of the main method

    // this method prints out the menu to a user
    public static void printMenu() {
        System.out.print(" Command Options "
                + "----------------------------------- "
                + "a: add an integer in the array " + "b: display the array "
                + "c: compute and display the maximum "
                + "d: compute and display the minimum "
                + "e: compute and display the sum "
                + "?: display the menu again " + "q: quit this program ");

    } // end of the printMenu method

} // end of the Assignment7 class