Lab 61a Create an input file Lab61atxt as follows 3 4 5 3 6
Lab 6_1a: Create an input file Lab6_1a.txt as follows. 3 4 5 3 6 -5 4 -2 0 9 6 -5 3 -1 The first number represents how many rows we want to read and the second number represents how many numbers we want to have in a row. Write a java program to read all those values using nested loops. Calculate the sum of each rows and the whole numbers. Name the class name as Lab6_1a The output should look like this: The matrix read is: 5 3 6 -5 4 -2 0 9 6 -5 3 -1 Sum of total numbers: 23 Hint: Use nested loop. Create a new output file Lab6_1a_out.txt to store the result as: java Lab6_1a < Lab6_1a.txt> Lab6_1a_out.txt Lab 6_1b: Write a java program to implement nested loops to generate output as in sample output. Name the class name as Lab6_1b Your program should ask the user to input an integer that determines the number of rows. Based on that input number, generate the ouput of X\'s and O\'s as show in below sample output. Sample output 1: How many rows ? 5 O O X O X O O X O X O X O X O Sample output 2: How many rows ? 10 O O X O X O O X O X O X O X O O X O X O X O X O X O X O O X O X O X O X O X O X O X O X O O X O X O X O X O X Create a new output file Lab6_1b_out.txt to store the result as: java Lab6_1b > Lab6_1b_out.txt Submit all 5 files: Lab6_1a.java, Lab6_1b.java, Lab6_1a.txt, Lab6_1a_out.txt and Lab6_1b_out.txt
Solution
//program one:-
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import static java.lang.System.exit;
import java.util.Scanner;
public class ReadNum {
public static void main(String args[]) throws FileNotFoundException, UnsupportedEncodingException
{
Scanner sc = new Scanner(new File(\"G:\\\\Lab6_1a.txt\"));
int row=0,col=0;
if(sc.hasNextInt())
{
row=sc.nextInt();
col=sc.nextInt();
}
else
{
System.out.println(\"no data in file\");
exit(0);
}
int[][] data = new int[row][col];//array to store data
for(int i=0;i<row;i++)
{
for(int j=0;j<row;j++)
{
if(sc.hasNextInt()) //reading data number by number from the file and store it to array
data[i][j]=sc.nextInt();
}
}
int sum=0;
for(int i=0;i<row;i++)
{
for(int j=0;j<row;j++)
{
sum+=data[i][j]; //caluclating sum
}
}
//below printwriter over rides the the file if it is aready exist
PrintWriter writer = new PrintWriter(\"Lab6_1a_out.txt\", \"UTF-8\");
for(int i=0;i<row;i++)
{
for(int j=0;j<row;j++)
{
System.out.print(data[i][j]); //printing to console
writer.print(data[i][j]+\" \"); //writing to file.
}
System.out.println();
writer.println();
}
writer.println(\"Sum of total numbers:\"+sum);
writer.close();
}
}
example output file data:-
5 3 6
-5 4 -2
0 9 6
Sum of total numbers:52
// program2:-
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
public class pyramid {
public static void main(String args[]) throws FileNotFoundException, UnsupportedEncodingException
{
Scanner sc = new Scanner(System.in);
int rows;
System.out.println(\"How many rows:\"); //enter rows in console...
rows=sc.nextInt();
PrintWriter writer = new PrintWriter(\"Lab6_1b_out.txt\", \"UTF-8\");
for(int i=1;i<=rows;i++)
{
for(int j=1;j<=i;j++)
{
if(j%2==1) //if j is odd print o
{
System.out.print(\"o\"); //printing to console
writer.print(\"o\"); //writing to file.
}
else
{ //even print x
System.out.print(\"x\"); //printing to console
writer.print(\"x\"); //writing to file.
}
}
System.out.println();
writer.println();
}
writer.close();
}
}
example output:-
How many rows:
10
o
ox
oxo
oxox
oxoxo
oxoxox
oxoxoxo
oxoxoxox
oxoxoxoxo
oxoxoxoxox


