import java.util.Arrays; public class CS225_Final_Review { public static void ma
ID: 3693836 • Letter: I
Question
import java.util.Arrays;
public class CS225_Final_Review {
public static void main(String[] args)
{
// 1) Create a 2D array of doubles with 4 values in
// the first row, 3 values in second row & 5 values
// in third row. Use the {} method and provide random.
// Call this array a1
double a1[][] = { { 1, 2, 3, 4 },
{ 5, 6, 7},
{ 8, 9, 10, 11, 12 } };
// 2) Write code to program-atically print a1
/*for (int r = 0; r < a1.length; r++)
{
System.out.print(Arrays.toString(a1[r]));
System.out.println();
}*/
for (int r = 0; r < a1.length; r++)
{
for (int c = 0; c < a1[r].length; c++)
System.out.print(a1[r][c] + " ");
System.out.println();
}
System.out.println();
// 3) Create a NEW array of integers called a2 with
// all 0s that has 3 rows and 4 columns
int [][] a2 = new int[3][4];
// 4) Write code to programatically print a2
for (int r = 0; r < a2.length; r++)
{
for (int c = 0; c < a2[r].length; c++)
System.out.print(a2[r][c] + " ");
System.out.println();
}
//int [][] a2 = new int[3][4];
// 5) Draw the contents of a2 after the following operations...
a2[2][2] = 1;
a2[0][0] = 2;
a2[1][0] = 3;
PrintArray(a2);
// 6) Draw the contents of a2 after the following operations...
for (int r = 0; r < a2.length; r++)
for (int c = 0; c < a2[r].length; c++)
a2[r][c]++;
PrintArray(a2);
// 7) Draw the contents of a2 after the following operations...
for (int c = 1; c < a2[1].length; c++)
a2[1][c] = a2[1][c-1] + 1;
PrintArray(a2);
// 8) Draw the contents of a2 after the following operations...
for (int i = 0; i < a2.length; i++)
a2[i][i] = 9;
PrintArray(a2);
// 9) Create a custom class which extends NullPointerException
// (this should only be a few lines of code)
/*public class IllegalPrimeNumberException extends NumberFormatException
{
IllegalPrimeNumberException (String message)
{
super(message);
}
}*/
// 10) Write code which writes the Family class to a file with this schema:
// FamilyName, numChildren, allowanceRate (e.g., 3.50)
// 11) Write code which reads the file back in from 10) and prints out the
// results (objects)
// 12) Write code which writes the Family class (above) to a file
// using serialize techniques
// 13) Write code which reads the serialized file back in from 12) and
// prints out the results (objects)
}
public static void PrintArray(int [][] a)
{
System.out.println();
System.out.println();
for (int r = 0; r < a.length; r++)
{
for (int c = 0; c < a[r].length; c++)
System.out.print(a[r][c] + " ");
System.out.println();
}
}
}
Explanation / Answer
Hello there ,
Please find beow code.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class CS225_Final_Review {
public static void main(String[] args) {
// 1) Create a 2D array of doubles with 4 values in
// the first row, 3 values in second row & 5 values
// in third row. Use the {} method and provide random.
// Call this array a1
double a1[][] = { { 1, 2, 3, 4 }, { 5, 6, 7 }, { 8, 9, 10, 11, 12 } };
// 2) Write code to program-atically print a1
/*
* for (int r = 0; r < a1.length; r++) {
* System.out.print(Arrays.toString(a1[r])); System.out.println(); }
*/
for (int r = 0; r < a1.length; r++) {
for (int c = 0; c < a1[r].length; c++)
System.out.print(a1[r][c] + " ");
System.out.println();
}
System.out.println();
// 3) Create a NEW array of integers called a2 with
// all 0s that has 3 rows and 4 columns
int[][] a2 = new int[3][4];
// 4) Write code to programatically print a2
for (int r = 0; r < a2.length; r++) {
for (int c = 0; c < a2[r].length; c++)
System.out.print(a2[r][c] + " ");
System.out.println();
}
// int [][] a2 = new int[3][4];
// 5) Draw the contents of a2 after the following operations...
a2[2][2] = 1;
a2[0][0] = 2;
a2[1][0] = 3;
PrintArray(a2);
// 6) Draw the contents of a2 after the following operations...
for (int r = 0; r < a2.length; r++)
for (int c = 0; c < a2[r].length; c++)
a2[r][c]++;
PrintArray(a2);
// 7) Draw the contents of a2 after the following operations...
for (int c = 1; c < a2[1].length; c++)
a2[1][c] = a2[1][c - 1] + 1;
PrintArray(a2);
// 8) Draw the contents of a2 after the following operations...
for (int i = 0; i < a2.length; i++)
a2[i][i] = 9;
PrintArray(a2);
// 9) Create a custom class which extends NullPointerException
// (this should only be a few lines of code)
/*
* public class IllegalPrimeNumberException extends
* NumberFormatException { IllegalPrimeNumberException (String message)
* { super(message); } }
*/
// 10) Write code which writes the Family class to a file with this
// schema:
// FamilyName, numChildren, allowanceRate (e.g., 3.50)
System.out.println("Writing Family to text file.");
writeToFile(new Family("Family3", 2, 3.5));
// 11) Write code which reads the file back in from 10) and prints out
// the
// results (objects)
System.out.println("Reading back from text file.");
List<Family> list = ReadFile();
System.out.println(list.toString());
// 12) Write code which writes the Family class (above) to a file
// using serialize techniques
System.out.println("Serializing ....");
searilizeFamilies(list);
// 13) Write code which reads the serialized file back in from 12) and
// prints out the results (objects)
System.out.println("Deserializing ....");
List<Family> familyList = deserialize();
System.out.println("Family List from Serialize file:" + familyList.toString());
}
// 10) Write code which writes the Family class to a file with this
// schema:
// FamilyName, numChildren, allowanceRate (e.g., 3.50)
public static void writeToFile(Family family) {
try (FileWriter fw = new FileWriter("family.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)) {
out.println(family.familyName + "," + family.numChildren + "," + family.allowanceRate);
} catch (IOException e) {
// exception handling left as an exercise for the reader
}
}
// 11) Write code which reads the file back in from 10) and prints out
// the
// results (objects)
public static List<Family> ReadFile() {
List<Family> listOfFamily = new ArrayList<Family>();
try (BufferedReader br = new BufferedReader(new FileReader("family.txt"))) {
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
String[] strArray = sCurrentLine.split(",");
listOfFamily.add(new Family(strArray[0], Integer.parseInt(strArray[1]), Double.parseDouble(strArray[2])));
}
} catch (IOException e) {
e.printStackTrace();
}
return listOfFamily;
}
// 12) Write code which writes the Family class (above) to a file
// using serialize techniques
public static void searilizeFamilies(List<Family> family) {
try {
FileOutputStream fileOut = new FileOutputStream("family.dat");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(family);
out.close();
fileOut.close();
} catch (IOException ex) {
}
}
// 13) Write code which reads the serialized file back in from 12) and
// prints out the results (objects)
public static List<Family> deserialize() {
List<Family> families = null;
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("family.dat"));
families = (List<Family>) in.readObject();
in.close();
} catch (Exception e) {
}
return families;
}
public static void PrintArray(int[][] a) {
System.out.println();
System.out.println();
for (int r = 0; r < a.length; r++) {
for (int c = 0; c < a[r].length; c++)
System.out.print(a[r][c] + " ");
System.out.println();
}
}
class MyException extends NullPointerException {
/**
*
*/
private static final long serialVersionUID = 1L;
Exception ep;
public MyException() {
}
public MyException(String message) {
super(message);
}
}
}
======O/P========
1.0 2.0 3.0 4.0
5.0 6.0 7.0
8.0 9.0 10.0 11.0 12.0
0 0 0 0
0 0 0 0
0 0 0 0
2 0 0 0
3 0 0 0
0 0 1 0
3 1 1 1
4 1 1 1
1 1 2 1
3 1 1 1
4 5 6 7
1 1 2 1
9 1 1 1
4 9 6 7
1 1 9 1
Writing Family to text file.
Reading back from text file.
[Family [familyName=Family1, numChildren=2, allowanceRate=3.5], Family [familyName=Family2, numChildren=1, allowanceRate=2.5], Family [familyName=Family3, numChildren=2, allowanceRate=3.5]]
Serializing ....
Deserializing ....
Family List from Serialize file:[Family [familyName=Family1, numChildren=2, allowanceRate=3.5], Family [familyName=Family2, numChildren=1, allowanceRate=2.5], Family [familyName=Family3, numChildren=2, allowanceRate=3.5]
Thank you .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.