QuestionQuestion

Use the following Guidelines:
• Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc).
• Keep identifiers to a reasonably short length.
• User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects).
• Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent.
• Use white space to make your program more readable.

# 1: Writing Exercise:
Consider the following array:
int[] a = { 3, 4, 5, 6, 7, 8};
Write the contents of the array a after the following loops, and explain the reason in your words. Use the original data above for each question.
a) for (int i = 1; i < 6; i++) { a[i] = a[i - 1]; }
b) for (int i = 5; i > 0; i--) { a[i] = a[i - 1]; }
c) for (int i = 0; i < 5; i++) { a[i] = a[i + 1]; }
d) for (int i = 4; i >= 0; i-=2) { a[i] = a[i + 1]; }
e) for (int i = 1; i < 6; i++) { a[i] = a[5 - i]; }

Note: The answers to the 5 questions (a through e) above should be typed in the block of comments in the Numbers.java file such as;
/*
a) {3, 4, 5, 6, 7, 8}: The array does not change anything because…
b) …

*/

Part#2: Programming
Your assignment is to write and submit two java files. One is a class definition named NumberList (NumberList.java), and another is the test driver program, Assignment7.java.

1) The NumberList is a class to keep the numbers, and it has the two instance variables: numbers, and offset.

NumberList
- numbers : int[]
- offset : int
+ NumberList(int, int)
+ flip()         : void
+ shift(int)    : void
+ shuffle()      : void
- hasAdjacent() : boolean
+ printInfo()    : String

The numbers is an array object storing the integers from (offset) to (offset + numbers.length – 1). For example, when the numbers has 5 elements and the offset is 0, then the numbers are 0, 1, 2, 3, and 4. The order is not specified.

The class must include the constructors and methods below: (If your class does not contain any of the following methods, points will be deducted). The public and private access modifiers are represented as "+" and "-" respectively.

1. NumberList(int length, int offset):
The constructor is to
a. Create an int-type array with the length of first input parameter. Each element in the array is initialized as the index value such as numbers[0] = offset+0, numbers[1] = offset+1, and numbers[2]= offset+2.
b. The offset is set as the second input.

2. flip():void
This method is to flip the order of elements in numbers. For example, the numbers is flipped from {3, 4, 5, 6, 7, 8} to {8, 7, 6, 5, 4, 3} when this method is called.

3. shift(int):void
This method is to shift all elements of numbers with the input value. For example, the numbers {5, 7, 8, 6, 4, 3} is updated to {6, 4, 3, 5, 7, 8} by shift(3).

4. shuffle():void
This method is to shuffle all elements in numbers randomly. Any two elements MUST NOT be adjacent. For example, the numbers is shuffled from {0, 1, 2, 3, 4} to {4, 2, 1, 0, 3}.

5. hasAdjacent():boolean
This method is to check whether any two elements in numbers are adjacent. For example, if it has {… 3, 4, …} then it returns true because 3 and 4 are adjacent. Use this private method in shuffle().

6. printInfo(): String
Return the element of numbers as a string. Note that it has at most 5 values per line and that the values are separated by three spaces. If the numbers has {3, 6, 7, 4, 8, 2, 5} in the array, then the output is
3   6   7   4   8   
2   5

2) Write the test driver called Assignment7.java. Assignment7.java contains the main method to create a NumberList instance object and test the methods in the class. The program will ask a user to enter one of the commands. Based on the user's choice, the program needs to perform corresponding operation. This will be done by calling (invoking) one of the methods you defined in the class. It will terminate when the user enters 'q'. The tester program provided in the last assignments may help. In addition to the main method, the Assignment7 class has a static method, printMenu(), to print out the following commands.
Command Options
-----------------------------------
a: Create new data
b: flip the elements
c: shift the elements
d: shuffle the elements
e: Display the elements
?: Display the menu again
q: Quit this program

Here is the description for each option:
a: ask and read the size of array and offset, and create and display the elements
b: flip and display the elements of array
c: ask and read the shift value, and shift and display the elements
d: shuffle and display the elements of array
e: display the elements of array
?: displays the menu
q: quits the program
Output Example
Program Output
****** Tester Program ******
Command Options
-----------------------------------
a: Create new data
b: flip the elements
c: shift the elements
d: shuffle the elements
e: Display the elements
?: Display the menu again
q: Quit this program

Please enter a command or type ?: a
n [Create new numbers]
[Input the size of numbers]: 5
[Input the offset]: 0
Please enter a command or type ?: e
e [Display the elements]:
0   1   2   3   4      
Please enter a command or type ?: b
b [flip the elements]:
4   3   2   1   0      
Please enter a command or type ?: c
c [shift the elements]:
[Input the size of numbers]: 2
1   0   4   3   2      
Please enter a command or type ?: d
d [shuffle the elements]:
3   0   1   4   2      
Please enter a command or type ?: ?
Command Options
-----------------------------------
a: Create new data
b: flip the elements
c: shift the elements
d: shuffle the elements
e: Display the elements
?: Display the menu again
q: Quit this program

Please enter a command or type ?: a
n [Create new numbers]
[Input the size of numbers]: 8
[Input the offset]: 3
Please enter a command or type ?: e
e [Display the elements]:
3   4   5   6   7
8   9 10      
Please enter a command or type ?: b
b [flip the elements]:
10   9   8   7   6
5   4   3
Please enter a command or type ?: c
c [shift the elements]:
[Input the size of numbers]: 4
6   5   4   3   10
9   8   7
Please enter a command or type ?: d
d [shuffle the elements]:
3 10   4   6   9      
8   7   5

Please enter a command or type ?: q
****** End of Program ******

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

public class Number {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
      
       System.out.print("a) ");
       qa();
       System.out.print("b) ");
       qb();
       System.out.print("c) ");
       qc();
       System.out.print("d) ");
       qd();
       System.out.print("e) ");
       qe();
    }
   
    /**
    * a) {3, 3, 3, 3, 3, 3} the value of this item is the value of the previous one, the first one is 3 so all item are 3
    */
    private static void qa(){
       int[] a = { 3, 4, 5, 6, 7, 8};
       for (int i = 1; i < 6; i++) { a[i] = a[i - 1]; }...
$40.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats