Java
Here is the template
public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return An integer array containing the newly generated row. */ public static int[] firstRow(int row){ // TODO return null; } /** * nextRow * * This will generate the following row of the matrix, given the previous row * * @param row int[] The array representing the previous row * @return An integer array containing the newly generated row. */ public static int[] nextRow(int[] row){ // TODO return null; } /** * display * * This will display a square 2D array (matrix) as shown in the assignment * specification. The elements within the matrix are constrained such that * each element is an integer between 1 and the size of the matrix. * * @param mat int[][] The 2D matrix to display */ public static void display(int[][] mat){ // TODO } }
public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size int[][] matrix = new int[5][5]; // TODO: Create first row matrix[0] = firstRow(5); // TODO: Generate remaining rows for (int i = 1; i < matrix.length; i++) { matrix[i] = nextRow(matrix[i-1]); } // TODO: Display matrix display(matrix); } /** * firstRow * <p> * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return An integer array containing the newly generated row. */ public static int[] firstRow(int size) { int[] arr = new int[size]; for (int i = 0; i < size; i++) { arr[i] = i+1; } return arr; } /** * nextRow * <p> * This will generate the following row of the matrix, given the previous row * * @param row int[] The array representing the previous row * @return An integer array containing the newly generated row. */ public static int[] nextRow(int[] row) { int[] arr = new int[row.length]; for (int i = 0; i < row.length-1; i++) { arr[i] = row[i+1]; } arr[row.length-1] = row[0]; return arr; } /** * display * <p> * This will display a square 2D array (matrix) as shown in the assignment * specification. The elements within the matrix are constrained such that * each element is an integer between 1 and the size of the matrix. * * @param mat int[][] The 2D matrix to display */ public static void display(int[][] mat) { System.out.print("+"); for (int i = 0; i < mat[0].length; i++) { System.out.print("-+"); } System.out.println(); for (int i = 0; i < mat.length; i++) { for (int j = 0; j < mat[i].length; j++) { System.out.print("|" + mat[i][j]); } System.out.println("|"); System.out.print("+"); for (int j = 0; j < mat[i].length; j++) { System.out.print("-+"); } System.out.println(); } } }
Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO:...
Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args) { int[] matrix = {{1,2},{3,4},{5,6},{7,8}}; int columnChoice; int columnTotal = 0; double columnAverage = 0; Scanner input = new Scanner(System.in); System.out.print("Which column would you like to average (1 or 2)? "); columnChoice = input.nextInt(); for(int row = 0;row < matrix.length;++row) { columnTotal += matrix[row][columnChoice]; } columnAverage = columnTotal / (float) matrix.length; System.out.printf("\nThe average of column %d is %.2f\n", columnAverage, columnAverage); } } This program...
import java.util.Scanner; public class Lab6d { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); // TODO: get user choice // TODO: call printTable method passing choice as the parameter } public static void printTable(int stop) { // TODO: print header // TODO: loop to print table rows up to stop value } Write a Java program where the main () method prompts the user to select an integer value between 1 and...
[JAVA] help —————— ListArrayMain.java ——————- public class ListArrayMain { public static void main (String[] args) { ListArray L1 = new ListArray(); // constructs the list L1 L1.InsertBegin(2); L1.InsertBegin(3); L1.InsertBegin(7); L1.InsertBegin(15); L1.InsertBegin(5); L1.InsertEnd(666); L1.InsertAfter(1000,7); // Now, let's print out the array to verify the insertions worked. System.out.println("The items in the list are:"); for (int i = 0; i<= L1.lastCell; i++) { System.out.println(L1.a[i]); } System.out.println("The last cell number is: " + L1.lastCell); }// end main } ———————————- ListArray.java ———————————— public class ListArray...
import java.util.Scanner; public class TriangleMaker { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle."); Scanner keyboard = new Scanner(System.in); int size = keyboard.nextInt(); for (int i = 1; i <= size; i++) { for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(); } for (int...
Q21 Read the following code: 8 Points public class Main { public static int[][] doStuff() { int[][] array2D = new int[3][2]; for (int i = 0; i < array2D.length; i++) { for (int j = 0; j < array2D[0].length; j++) { array2D[i][j] = (i+j)%2; } } return array2D; فه public static void main(String[] args) { int[][] a = doStuff(); مہ سره Q21.1 What are the contents of array a? 6 Points Write your answer as if printing the elements row-by-row....
Code in java Using the template below: public class Lab03 { public static void main(String[] args) { ArrayList<Integer> list1 = new ArrayList<Integer>(); Collections.addAll(list1, 1, 3, 5, 5 ); ArrayList<Integer> list2 = new ArrayList<Integer>(); Collections.addAll(list2, 3, 7, 3, 2, 4 ); ArrayList<Integer> result1= uniqueUnion(list1,list2); System.out.println(result1); Integer[] array = new Integer[]{ 29, 28, 27, 16, 15, -14, 3, -2, 2}; ArrayList<Integer> arrayList = new ArrayList<Integer>(Arrays.asList(array)); System.out.printf("The average is: %.2f%n", averagePositive(arrayList)); } // end main public static ArrayList<Integer> uniqueUnion(ArrayList<Integer> list1, ArrayList<Integer> list2) {...
public class PracticeExam { public static void main(String[] args) { int[][] data = { { 0, 1, 2, 3, 4, 5, 6 }, { 0, 1, 2, 3, 4, 5, 6 }, { 0, 1, 2, 3, 4, 5, 6 }, { 0, 1, 2, 3, 4, 5, 6 }, { 0, 1, 2, 3, 4, 5, 6 }, { 0, 1, 2, 3, 4, 5, 6 }, { 0, 1, 2, 3, 4, 5, 6 }, { 0, 1,...
1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...
Draw a flowchart for this program public class InsertionSort { public static void main(String a[]) { int[] array = {7, 1, 3, 2, 42, 76, 9}; insertionSort(array); } public static int[] insertionSort(int[] input) { int temp; for (int i = 1; i < input.length; i++) { for (int j = i; j > 0; j--) { if (input[j] < input[j - 1]) { temp = input[j]; input[j] = input[j - 1]; input[j - 1] = temp; } } display(input, i);...
Given main(). define the Artist class (in file Artist java) with constructors to initialize an artist's information, get methods, and a printlnfo() method. The default constructor should initialize the artist's name to "None' and the years of birth and death to 0. printinfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Define the Artwork class (in file Artwork.java) with constructors to initialize an artwork's information, get methods, and a printinfo() method. The constructor should...