TRANSPOSE: public class Transpose { int array[][]; public Transpose(int row, int
ID: 3845992 • Letter: T
Question
TRANSPOSE:
public class Transpose {
int array[][];
public Transpose(int row, int col)
{
array = new int[row][col];
}
// Creates the array
public void createPatterned2DArray()
{
for(int i= 0; i< array.length; ++i){
for (int j = 0; j < array[i].length; ++j){
array[i][j] = 10 + array.length * (i+1) + j;
}
}
}
// Two row array
public void print2DArray()
{
for(int i = 0; i< array.length; ++i){
for(int j = 0; j < array[i].length; ++j){
System.out.print(array[i][j]+ " ");
}
System.out.println();
}
}
// 2 column array
public void print2DArrayTransposed()
{
//System.out.println();
for(int j = 0; j< array[0].length; ++j){
System.out.println();
for(int i = 0; i < array.length; ++i){
System.out.print(array[i][j] + " ");
}
}
System.out.println();
System.out.println();
}
}
TEST:
import java.util.Scanner;
public class TestTranspose {
public String getIdentificationString()
{
String program="Program 2b", student= "XXX";
return program+", "+student;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//System.out.print(" ");
int row = sc.nextInt();
int cols = sc.nextInt();
if((row == 0) || (cols == 0)) {
System.out.print(" ");
return;
} else {
Transpose t = new Transpose(row, cols);
t.createPatterned2DArray();
t.print2DArray();
t.print2DArrayTransposed();
//System.out.println();
}
}
}
Instructions for Transpose 2. java class A common event in programming is to modify an existing program with different or new capabilities. In lab 2b you wrote a Java class Transpose java. The change we are making is to have the Transpose2 object store the array instead of the calling program (TestTranspose2) storing it The program printed output and functionality of the methods previously created will not change. We will, however, not allow the test class to call the method to create an array now. [Are you already thinking of how to restrict the access?l Steps to modify Transpose into Transpos Declare, but do not initialize, a private member variable to store a 2-dimensional int array. Change the access modifier for createPatterned2DArray() to private Add new constructor that will create an 2-dimensional array given row and column values as parameters. Do not rewrite creating the array! Allow the constructor to call createPatterned2DArray() and use the array it returns to initialize the class member array variable Add a new method getRows which returns the number of rows in the 2-dimensional array member variable. Add a new method getcols which returns the number of columns in the 2-dimensional array member variable. Modify getIdentificationstring to print this program. As in previous labs, this method returns a string and is not called from your program but graded by the autograder. Program 3b, StudentFirstName StudentLastName Overload print2DArray() by adding a no-arg method o It is tempting to cut and paste the entire method print2DArray(int 1[ 1 array), but let's not! This would give us two sections of nearly identical code: one that used the instance array and one that used the array passed in Instead, call the one-arg method from the no-arg method. Like this public void print2DArray this print2DArray (name OfInstanceArray) Done! Sweet. And no retesting, re-writing. The this isnt strictly necessary but good programming practice.Explanation / Answer
program
package com.ch.classes;
import java.util.Scanner;
public class Transpose2 {
int array[][];
int row;
int col;
public Transpose2(int row, int col) {
this.row = row;
this.col = col;
array = new int[row][col];
createPatterned2DArray();
}
/**
* @return the row
*/
public int getRow() {
return row;
}
/**
* @param row
* the row to set
*/
public void setRow(int row) {
this.row = row;
}
/**
* @return the col
*/
public int getCol() {
return col;
}
/**
* @param col
* the col to set
*/
public void setCol(int col) {
this.col = col;
}
// Creates the array
private void createPatterned2DArray() {
for (int i = 0; i < array.length; ++i) {
for (int j = 0; j < array[i].length; ++j) {
array[i][j] = 10 + array.length * (i + 1) + j;
}
}
}
// Two row array
public void print2DArray() {
for (int i = 0; i < array.length; ++i) {
for (int j = 0; j < array[i].length; ++j) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
// 2 column array
public void print2DArrayTransposed() {
// System.out.println();
for (int j = 0; j < array[0].length; ++j) {
System.out.println();
for (int i = 0; i < array.length; ++i) {
System.out.print(array[i][j] + " ");
}
}
System.out.println();
System.out.println();
}
public String getIdentificationString() {
Scanner scanner = new Scanner(System.in);
System.out.println("enter your program name");
String program = scanner.nextLine();
System.out.println("enter your first name");
String fname = scanner.nextLine();
System.out.println("enter your last name");
String lname = scanner.nextLine();
return program + " " + fname + " " + lname;
}
}
**********************************************************************************
package com.ch.classes;
import java.util.Scanner;
public class TestTranspose {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter row value");
int row = sc.nextInt();
System.out.println("enter column value");
int cols = sc.nextInt();
if ((row == 0) || (cols == 0)) {
System.out.print(" ");
return;
} else {
Transpose2 t = new Transpose2(row, cols);
System.out.println("the given row value is " + t.getRow());
System.out.println("the given column value is " + t.getCol());
System.out.println(t.getIdentificationString());
t.print2DArray();
t.print2DArrayTransposed();
}
}
}
output
enter row value
6
enter column value
3
the given row value is 6
the given column value is 3
enter your program name
program3b
enter your first name
mark
enter your last name
smith
program3b mark smith
16 17 18
22 23 24
28 29 30
34 35 36
40 41 42
46 47 48
16 22 28 34 40 46
17 23 29 35 41 47
18 24 30 36 42 48
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.