In: Computer Science
You will create a program with 3 methods. For the previous homework with three problems, you will create a method for each problem.
In the main method, you will declare the three arrays and create the memory space for each of them. The first method will receive a one dimension array and then it will assign the values ( e.g., a[4] = 4*4*4*4;). The second method receives a 2 dimensional array and creates the calendar. The third method receives a two dimensional array and inverts row 1 in row.
Please note that you already have the bodies of the methods. You are only creating the methods and calling them in the main method.
You will also create methods printOneDimArray; printArrayCalendar; and printTwoDimArray to print each of the arrays.
Previous Homework Instructions
arrayNum [0] = 0
arrayNum [1] = 1
arrayNum [2] = 2*2
arrayNum [3] = 3*3*3
arrayNum [4] = 4*4*4*4
…
arrayNum [10] = 10*10*10…..*10
Hints: a) arrayNum [2] has a value of 4 and arrayNum [3] has a value of 27; b) use one loop to traverse the array and another to calculate the value based on the current index.
Hint: a) use one loop to manage the weeks and another to manage the days of the weeks
previous homework attached below
1) public class Test{
public static void main(String []args){
int[] arrayNum=new int[11];
for(int i=0;i<=10;i++)
{
arrayNum[i]=i;
for(int j=0;j<i-1;j++)
{
arrayNum[i]=arrayNum[i]*i;
}
}
System.out.println("Array is");
for(int i=0;i<=10;i++)
{
System.out.println("arrayNum["+i+"]="+arrayNum[i]);
}
}
}
2) import java.util.Scanner;
import java.util.Arrays;
public class MyClass
{
public static void main(String args[])
{
int [ ] [ ] matrix = new int [5][7];
Scanner sc = new Scanner(System.in);
int days=1;
System.out.println("Assigning Values");
for (int i = 0; i <5; i++)
{
for (int j = 0; j < 7; j++)
{
matrix[i][j] = days++;
if(days==32)
{
break;
}
}
//because day can be 31 only.
if(days==31)
{
break;
}
}
for (int i = 0; i < 5; i++)
{
// Loop through all elements of current row
for (int j = 0; j < 7; j++)
{
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
3)
import java.util.Scanner;
import java.util.Arrays;
public class MyClass
{
public static void main(String args[])
{
int [ ] [ ] myBiDimArrayNum = {
{ 1, 3, 5 ,7},
{ 0, 0, 0, 0 }
};
Scanner sc = new Scanner(System.in);
int days=1;
System.out.println("Manipulating Array");
for (int i = 0; i <2; i++)
{
//take this variable to copy elements from end of row first.
int last=3;
for (int j = 0; j < 4; j++)
{
//because indexing start from zero 1 will represent row second now
we have to change its content.
if(i==1)
{
//As we have to manipulate the second from ending of row first we
are using ***(i-1) and assign it to second row that is **(i).
myBiDimArrayNum[i][j]=myBiDimArrayNum[i-1][last];
last--;
}
}
}
for (int i = 0; i < 2; i++)
{
// Loop through all elements of current row
for (int j = 0; j < 4; j++)
{
System.out.print(myBiDimArrayNum[i][j] + " ");
}
System.out.println();
}
}
}
This is modified java code.
import java.util.Scanner;
import java.util.Arrays;
class one{
void one1(int arr1[]){
for(int i=0;i<=10;i++)
{
arr1[i]=i;
for(int j=0;j<i-1;j++)
{
arr1[i]=arr1[i]*i;
}
}
System.out.println("Array is");
for(int i=0;i<=10;i++)
{
System.out.println("arrayNum["+i+"]="+arr1[i]);
}
}
}
class two{
void two1(int matrix[][])
{
int days=1;
System.out.println("Assigning Values");
for (int i = 0; i <5; i++)
{
for (int j = 0; j < 7; j++)
{
matrix[i][j] = days++;
if(days==32)
{
break;
}
}
//because day can be 31 only.
if(days==31)
{
break;
}
}
for (int i = 0; i < 5; i++)
{
// Loop through all elements of current row
for (int j = 0; j < 7; j++)
{
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
class three{
void three1(int myBiDimArrayNum[][])
{
int days=1;
System.out.println("Manipulating Array");
for (int i = 0; i <2; i++)
{
//take this variable to copy elements from end of row first.
int last=3;
for (int j = 0; j < 4; j++)
{
//because indexing start from zero 1 will represent row second now
we have to change its content.
if(i==1)
{
//As we have to manipulate the second from ending of row first we
are using ***(i-1) and assign it to second row that is **(i).
myBiDimArrayNum[i][j]=myBiDimArrayNum[i-1][last];
last--;
}
}
}
for (int i = 0; i < 2; i++)
{
// Loop through all elements of current row
for (int j = 0; j < 4; j++)
{
System.out.print(myBiDimArrayNum[i][j] + " ");
}
System.out.println();
}
}
}
public class Main
{
Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int[] arr1=new int[11];
int [ ] [ ] matrix = new int [5][7];
int [ ] [ ] myBiDimArrayNum = {
{ 1, 3, 5 ,7},
{ 0, 0, 0, 0 }
};
one obj1=new one();
two obj2=new two();
three obj3=new three();
System.out.println("method one\n\n");
obj1.one1(arr1);
System.out.println("method two\n\n");
obj2.two1(matrix);
System.out.println("method three\n\n");
obj3.three1(myBiDimArrayNum);
}
}
Output