SlideShare ist ein Scribd-Unternehmen logo
1 von 6
Write a Java program to solve the following problem using
modularity: Write a method that will initialize a 2-dimensional
array as follows: the array is split into 4 equal quadrants, the
North-West and South-East quadrants are initialized with 1s and
the North-East and the South-West quadrants are initialized
with 0s. Write another method to print a 2-dimensional array.
Your program will invoke the method to initialize the array and
the method to print it. Use dynamic arrays and ask the user for
the row size and the column size - both should be even numbers.
Write your program so that it will generate output similar to the
sample output below: Do you want to start(Y/N): y Enter row
size: 5 ERROR! Should be positive and even. REENTER: 6
Enter column size: 9 ERROR! Should be positive and even.
REENTER: 8 The matrix is: 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0
1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 Do
you want to continue (Y/N): y Enter row size: 4 Enter column
size: 6 The matrix is: 1 1 0 0 1 1 0 0 1 1 0 0 0 0 1 1 0 0 1 1 0 0
1 1 Do you want to continue (Y/N): n
Solution
Test2_1.java
import java.util.Scanner;
public class Test2_1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int row, col;
char yesNo;
Scanner input = new Scanner(System.in);
System.out.println(" Do you want to start (Y/N)");
yesNo = input.next().charAt(0);
if(yesNo =='y' || yesNo == 'Y'){
while(true){
System.out.println("Enter row size : ");
row = input.nextInt();
while((row % 2 != 0 && row > 1) || row ==0 ){
System.out.println("Error: Should be positive and
even. RENETER : ");
row = input.nextInt();
if(row % 2 == 0) break;
}
System.out.println("Enter Column size : ");
col = input.nextInt();
while((col % 2 != 0 && col > 1) || col ==0 ){
System.out.println("Error: Should be positive and
even. RENETER : ");
col = input.nextInt();
if(col % 2 == 0) break;
}
int a[][] = new int[row][col];
int output[][] = readArray(a);
printArray(output);
System.out.println("Do you want to contnue (Y/N)");
yesNo = input.next().charAt(0);
if(yesNo =='n' || yesNo == 'N'){
break;
}
}
}
}
public static int[][] readArray(int a[][]){
int col = a[0].length;
int row = a.length;
for(int i=0; i<a.length; i++){
for(int j=0; j<a[i].length; j++){
if(i < row/2){
if(j < col/2){
a[i][j] = 1;
}
else{
a[i][j] = 0;
}
}
else{
if(j < col/2){
a[i][j] = 0;
}
else{
a[i][j] = 1;
}
}
}
}
return a;
}
public static void printArray(int a[][]){
System.out.println("The matrix is : ");
for(int i=0; i<a.length; i++){
for(int j=0; j<a[i].length; j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
Output:
Do you want to start (Y/N)
y
Enter row size :
4
Enter Column size :
8
The matrix is :
1 1 1 1 0 0 0 0
1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
Do you want to contnue (Y/N)
y
Enter row size :
4
Enter Column size :
6
The matrix is :
1 1 1 0 0 0
1 1 1 0 0 0
0 0 0 1 1 1
0 0 0 1 1 1
Do you want to contnue (Y/N)
y
Enter row size :
8
Enter Column size :
6
The matrix is :
1 1 1 0 0 0
1 1 1 0 0 0
1 1 1 0 0 0
1 1 1 0 0 0
0 0 0 1 1 1
0 0 0 1 1 1
0 0 0 1 1 1
0 0 0 1 1 1
Do you want to contnue (Y/N)
n

Weitere ähnliche Inhalte

Ähnlich wie Write a Java program to solve the following problem using modular.docx

ma project
ma projectma project
ma project
Aisu
 

Ähnlich wie Write a Java program to solve the following problem using modular.docx (9)

Java Unit 1 Project
Java Unit 1 ProjectJava Unit 1 Project
Java Unit 1 Project
 
Parameters
ParametersParameters
Parameters
 
Psuedocode1, algorithm1, Flowchart1.pptx
Psuedocode1, algorithm1, Flowchart1.pptxPsuedocode1, algorithm1, Flowchart1.pptx
Psuedocode1, algorithm1, Flowchart1.pptx
 
Csci101 lect03 algorithms_i
Csci101 lect03 algorithms_iCsci101 lect03 algorithms_i
Csci101 lect03 algorithms_i
 
ma project
ma projectma project
ma project
 
Java file
Java fileJava file
Java file
 
Java file
Java fileJava file
Java file
 
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdfMagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
 

Mehr von ajoy21

Please choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docxPlease choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docx
ajoy21
 
Please answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docxPlease answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docx
ajoy21
 
Please answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docxPlease answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docx
ajoy21
 
Please answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docxPlease answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docx
ajoy21
 

Mehr von ajoy21 (20)

Please complete the assignment listed below.Define and explain, us.docx
Please complete the assignment listed below.Define and explain, us.docxPlease complete the assignment listed below.Define and explain, us.docx
Please complete the assignment listed below.Define and explain, us.docx
 
Please cite sources for each question. Do not use the same sources f.docx
Please cite sources for each question. Do not use the same sources f.docxPlease cite sources for each question. Do not use the same sources f.docx
Please cite sources for each question. Do not use the same sources f.docx
 
Please choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docxPlease choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docx
 
Please check the attachment for my paper.Please add citations to a.docx
Please check the attachment for my paper.Please add citations to a.docxPlease check the attachment for my paper.Please add citations to a.docx
Please check the attachment for my paper.Please add citations to a.docx
 
Please answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docxPlease answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docx
 
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
Please attach Non-nursing theorist summaries.JigsawExecutive .docxPlease attach Non-nursing theorist summaries.JigsawExecutive .docx
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
 
Please answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docxPlease answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docx
 
Please answer the following questions. Please cite your references..docx
Please answer the following questions. Please cite your references..docxPlease answer the following questions. Please cite your references..docx
Please answer the following questions. Please cite your references..docx
 
Please answer the following questions.1.      1.  Are you or.docx
Please answer the following questions.1.      1.  Are you or.docxPlease answer the following questions.1.      1.  Are you or.docx
Please answer the following questions.1.      1.  Are you or.docx
 
Please answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question with 200-300 words.Q. Discu.docxPlease answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question with 200-300 words.Q. Discu.docx
 
Please answer the following question Why do you think the US ha.docx
Please answer the following question Why do you think the US ha.docxPlease answer the following question Why do you think the US ha.docx
Please answer the following question Why do you think the US ha.docx
 
Please answer the following questions. Define tunneling in the V.docx
Please answer the following questions. Define tunneling in the V.docxPlease answer the following questions. Define tunneling in the V.docx
Please answer the following questions. Define tunneling in the V.docx
 
Please answer the following questions1. How can you stimulate the.docx
Please answer the following questions1. How can you stimulate the.docxPlease answer the following questions1. How can you stimulate the.docx
Please answer the following questions1. How can you stimulate the.docx
 
Please answer the following questions very deeply and presicely .docx
Please answer the following questions very deeply and presicely .docxPlease answer the following questions very deeply and presicely .docx
Please answer the following questions very deeply and presicely .docx
 
Please answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in an informal 1 ½ - 2-page es.docxPlease answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in an informal 1 ½ - 2-page es.docx
 
Please answer the following questions in a response of 150 to 200 wo.docx
Please answer the following questions in a response of 150 to 200 wo.docxPlease answer the following questions in a response of 150 to 200 wo.docx
Please answer the following questions in a response of 150 to 200 wo.docx
 
Please answer these questions regarding the (TILA) Truth in Lending .docx
Please answer these questions regarding the (TILA) Truth in Lending .docxPlease answer these questions regarding the (TILA) Truth in Lending .docx
Please answer these questions regarding the (TILA) Truth in Lending .docx
 
Please answer the following question pertaining to psychology. Inc.docx
Please answer the following question pertaining to psychology. Inc.docxPlease answer the following question pertaining to psychology. Inc.docx
Please answer the following question pertaining to psychology. Inc.docx
 
Please answer the following questions in a response of 250 to 300 .docx
Please answer the following questions in a response of 250 to 300 .docxPlease answer the following questions in a response of 250 to 300 .docx
Please answer the following questions in a response of 250 to 300 .docx
 
Please answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docxPlease answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docx
 

Kürzlich hochgeladen

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Kürzlich hochgeladen (20)

Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 

Write a Java program to solve the following problem using modular.docx

  • 1. Write a Java program to solve the following problem using modularity: Write a method that will initialize a 2-dimensional array as follows: the array is split into 4 equal quadrants, the North-West and South-East quadrants are initialized with 1s and the North-East and the South-West quadrants are initialized with 0s. Write another method to print a 2-dimensional array. Your program will invoke the method to initialize the array and the method to print it. Use dynamic arrays and ask the user for the row size and the column size - both should be even numbers. Write your program so that it will generate output similar to the sample output below: Do you want to start(Y/N): y Enter row size: 5 ERROR! Should be positive and even. REENTER: 6 Enter column size: 9 ERROR! Should be positive and even. REENTER: 8 The matrix is: 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 Do you want to continue (Y/N): y Enter row size: 4 Enter column size: 6 The matrix is: 1 1 0 0 1 1 0 0 1 1 0 0 0 0 1 1 0 0 1 1 0 0 1 1 Do you want to continue (Y/N): n Solution Test2_1.java import java.util.Scanner; public class Test2_1 { /**
  • 2. * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int row, col; char yesNo; Scanner input = new Scanner(System.in); System.out.println(" Do you want to start (Y/N)"); yesNo = input.next().charAt(0); if(yesNo =='y' || yesNo == 'Y'){ while(true){ System.out.println("Enter row size : "); row = input.nextInt(); while((row % 2 != 0 && row > 1) || row ==0 ){ System.out.println("Error: Should be positive and even. RENETER : "); row = input.nextInt(); if(row % 2 == 0) break; } System.out.println("Enter Column size : "); col = input.nextInt(); while((col % 2 != 0 && col > 1) || col ==0 ){ System.out.println("Error: Should be positive and even. RENETER : ");
  • 3. col = input.nextInt(); if(col % 2 == 0) break; } int a[][] = new int[row][col]; int output[][] = readArray(a); printArray(output); System.out.println("Do you want to contnue (Y/N)"); yesNo = input.next().charAt(0); if(yesNo =='n' || yesNo == 'N'){ break; } } } } public static int[][] readArray(int a[][]){ int col = a[0].length; int row = a.length; for(int i=0; i<a.length; i++){ for(int j=0; j<a[i].length; j++){ if(i < row/2){ if(j < col/2){ a[i][j] = 1; } else{
  • 4. a[i][j] = 0; } } else{ if(j < col/2){ a[i][j] = 0; } else{ a[i][j] = 1; } } } } return a; } public static void printArray(int a[][]){ System.out.println("The matrix is : "); for(int i=0; i<a.length; i++){ for(int j=0; j<a[i].length; j++){ System.out.print(a[i][j]+" "); } System.out.println(); } } }
  • 5. Output: Do you want to start (Y/N) y Enter row size : 4 Enter Column size : 8 The matrix is : 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 Do you want to contnue (Y/N) y Enter row size : 4 Enter Column size : 6 The matrix is : 1 1 1 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 1 1 1 Do you want to contnue (Y/N)
  • 6. y Enter row size : 8 Enter Column size : 6 The matrix is : 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 Do you want to contnue (Y/N) n