The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Program description: Starting with the provided Java source code, you are going to make modifications that move the 3 features into their own class files.
In addition, for feature 2 and 3, you will move the code that prompts the user into the new class files as well. In doing this, all that will remain in the if/else will be the object creation and then the call to the methods you are going to create in the new classes.
Feature 1: Move the method code to a new class called feature1. In this new class you will have a method called run, which is what will be called from the if/else where the method was previously called from.
Feature 2: Move the method code to a new class called feature2. In this new class you will have a method called run, which is what will be called from the if/else where the method was previously called from. The run method will receive the double[][] as input and return of void. In addition, move the code that prompts the user for values as well, this will be a second method in the feature2 class, define it as getInput. As input you will need to pass the 4. Review the Chapter 09 Slides reference to the scanner object ( only scanner created will be in the main program ) and a return value, it will be the user input array[][]. As with feature1, the if/else will create the feature2 object and then call its getInput and the run methods
Feature 3: Move the method code to a new class called feature3. Follow the same steps as feature2, the only difference is that the getInput in addition to scanner needs a String ( part of the message prompting the user) in addition to the scanner reference and return would be int[][].
Â
Â
import java.util.Arrays;
import java.util.Scanner;
/*
Henry Cutler ID 1234 7/24/2015 Assignment #1 Starter code
Pseudocode:
Program has three features, all in a single program that are user
selectable.
Prompt with options and get user choice inside a while statement ( true )
If 1 is entered:
call method that creates an two dimensional array that has 200 random
chars with an allowed value of A-Z, and then prints out in rows of 20
If 2 is entered:
Prompt the user for 12 integers which are stored in an array of 3 rows by
4 columns , call a method that will and display them back as well as the totals
by column
If 3 is entered:
Call method that will Prompt the user to enter two lists of integers, the
first number is the size of the list. The method returns true/false which is
printed to user
Any other input entered program will exit using a break statement,
otherwise loops back to prompt user for feature selection.
*/
public class Module1 { public static void main(String args) {
// prompt the user
System.out.println("Welcome to Henry\'s 3 in 1 Programming Assignment (2D
Arrays) for Module 8");
// create Scanner Object
Scanner input = new Scanner(System.in);
// loop until break
while(true){
// new lines for readability
System.out.print("\n");
// Prompt user for choices
System.out.print("Enter 1 Random 2D Chars \nEnter 2 Sum Columns\nEnter 3
Identical Arrays\nAny other input will exit");
int choice = input.nextInt();
// Consume newline left-over
input.nextLine();
// first feature
if(choice == 1){
// newline for formating
System.out.println("\nYou Selected Random Array!");
// call the method of feature 1
feature1(); }
else if(choice == 2){
// Prompt user
System.out.println("You Selected Sum Columns");
// Enter array values
double myList = new double[3][4];
// 2D arrays are like spreadsheets ROW and Columns
System.out.println("Enter " + myList.length + " rows and " +
myList[0].length + " columns: ");
for (int i = 0; i < myList.length; i++){
for (int j = 0; j < myList[0].length; j++){
myList[i][j] = input.nextDouble();
}
}
// call the method of feature 2
feature2(myList);
}
else if(choice == 3){
// Prompt user
System.out.println("You Selected Identical Arrays");
final int ROW_SIZE = 3;
final int COLUMN_SIZE = 3;
System.out.print("Enter m1 (a 3 by 3 matrix) row by row: ");
int m1 = new int[ROW_SIZE][COLUMN_SIZE];
for (int i = 0; i < m1.length; i++)
for (int j = 0; j < m1[0].length; j++)
m1[i][j] = input.nextInt();
System.out.print("Enter m2 (a 3 by 3 matrix) row by row: ");
int m2 = new int[ROW_SIZE][COLUMN_SIZE];
for (int i = 0; i < m2.length; i++)
for (int j = 0; j < m2[0].length; j++)
m2[i][j] = input.nextInt();
// call the method of feature 3
if (feature3(m1,m2)) {
System.out.println("\nTwo lists are identical");
}
else {
System.out.println("\nTwo lists are not identical");
}
}
else{
// not 1 , 2 or 3
System.out.println("User didn't select 1,2 or 3. Exiting
Program\n");
// release object
input.close();
// exit while with break statement
break;
}
} // end of while } // end main
// feature 1
private static void feature1(){
// declare the array
int myList = new int[10][20];
// Nested loop
for (int i = 0; i < myList.length; i++) {
for (int j = 0; j < myList[0].length; j++) {
myList[i][j] = 'A' + (int)(Math.random() * ('Z'-'A' + 1));
}
}
// display to user
for (int i = 0; i < myList.length; i++) {
for (int j = 0; j < myList[0].length; j++) { } } // chars are just ints!
System.out.print((char)myList[i][j]); // every 20 chars which is the number columns
System.out.print("\n"); // every 20 chars which is the number columns
System.out.print("\n");
// New line at exit
System.out.print("\n");
}
// feature 2
private static void feature2(double userList ){
// display to user what they entered
System.out.println("You Entered:");
for (int i = 0; i < userList.length; i++) {
for (int j = 0; j < userList[0].length; j++) { } } // chars are just ints!
System.out.print(userList[i][j] + " "); // every row
System.out.print("\n"); System.out.println("\n The sums are:");
for (int column = 0; column < userList[0].length; column++) {
double total = 0.0;
for (int row = 0; row < userList.length; row++){
// sum column
total += userList[row][column];
} System.out.print(total + " ");
}
// space for formating
System.out.print("\n");
}
//feature 3
private static Boolean feature3( int m1,int m2){
// Hint: (1) first check if the two have the same size.
//
Compare the corresponding elements from list1 and list2.
//
return false, if not match. Return true if all matches.
if (m1.length * m1[0].length != m2.length * m2[0].length)
return false;
for (int i = 0; i < m1.length; i++)
for (int j = 0; j < m1[0].length; j++)
if (m1[i][j] != m2[i][j])
return false;
return true;
}
} // end method
-----------