public class File_In_36
{
private String filename;
public int[][] matrix()
{
// 1. matrix will call scan_File to determine whether or not the file
// name entered by the user actually exists
// 2. matrix will call size_matrix to determine the number of integers
// in the input file and set the instance variable matrix_size to that
// number
// 3. matrix will call check_square to determine whether or not matrix_size
// has the exact number of integers required for a square matrix
// {4, 9, 16, 25, 36 ...}
// 4. If the matrix is square, method matrix will call form_matix and return
// a reference to a two dimensional square array; else it will return null
System.out.println("The filename passed to matrix = " + filename);
int[][] dummy = new int[4][4];
return dummy;
}
}
CODE:
import java.util.*;
import java.io.*;
public class a{
public static void main(String args[]){
File_In_36 f = new File_In_36();
int arr[][]=f.matrix();
if(arr!=null){
f.print_matrix(arr);
}
}
}
class File_In_36
{
private String fileName;
private int matrix_size;
public int[][] matrix()
{
// 1. matrix will call scan_File to determine whether or not the file
// name entered by the user actually exists
// 2. matrix will call size_matrix to determine the number of integers
// in the input file and set the instance variable matrix_size to that
// number
// 3. matrix will call check_square to determine whether or not matrix_size
// has the exact number of integers required for a square matrix
// {4, 9, 16, 25, 36 ...}
// 4. If the matrix is square, method matrix will call form_matix and return
// a reference to a two dimensional square array; else it will return null
scan_File();
System.out.println("The filename passed to matrix = " + fileName);
int count=size_matrix();
if(check_square(count)){
int arr[][]=form_matix();
return arr;
}
else{
System.out.println("The numbers in the file doesnot form a square matrix");
return null;
}
}
public void print_matrix(int arr[][]){
System.out.println();
for(int i=0;i<matrix_size;i++){
for(int j=0;j<matrix_size;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
public int[][] form_matix(){
int arr[][]=new int[matrix_size][matrix_size];
File f=null;
Scanner s=null;
try{
f=new File(fileName);
s=new Scanner(f);
}
catch(Exception e){
System.exit(0);
}
for(int i=0;i<matrix_size;i++){
for(int j=0;j<matrix_size;j++){
arr[i][j]=s.nextInt();
}
}
return arr;
}
public void scan_File(){
Scanner sc = new Scanner(System.in);
File f=null;
do{
if(f!=null)System.out.println("File doesnot exist. Enter again: ");
else System.out.println("Enter fileName: ");
fileName = sc.next();
fileName = "./"+fileName;
f = new File(fileName);
}while(!f.exists() || f.isDirectory());
}
public int size_matrix(){
int count=0;
File f=null;
Scanner s=null;
try{
f=new File(fileName);
s=new Scanner(f);
}
catch(Exception e){
System.exit(0);
}
System.out.println("Countinh numbers");
while(s.hasNext()){
count ++;
s.nextInt();
}
System.out.println(count);
matrix_size= (int)Math.sqrt(count);
return count;
}
public boolean check_square(int n){
double sr = Math.sqrt(n);
return ((sr - Math.floor(sr)) == 0);
}
}
Sample Output:
radas-macOS:Desktop radas$ javac a.java
radas-macOS:Desktop radas$ java a
Enter fileName:
test.txt
The filename passed to matrix = ./test.txt
Countinh numbers
9
1 2 3
4 5 6
7 8 9
Contents of file test.txt:
1
2
3
4
5
6
7
8
9
public class File_In_36 { private String filename; public int[][] matrix() { //...
Create a class called MazeSolver: public class MazeSolver { private char[][] maze; private int startx, starty; Public MazeSolver(String fileName) throws IOException { // create an object to read information from “fileName” // read the maze dimension (row col) from the file // Allocate the space for maze // initialize array maze with contents of the file // find startx and starty printMaze(); // a method that prints the maze // solveMaze() is a recursive method to solve the maze if(solveMaze(maze,startx,starty))...
public class Fish { private String species; private int size; private boolean hungry; public Fish() { } public Fish(String species, int size) { this.species = species; this.size = size; } public String getSpecies() { return species; } public int getSize() { return size; } public boolean isHungry() { return hungry; } public void setHungry(boolean hungry) { this.hungry = hungry; } public String toString() { return "A "+(hungry?"hungry":"full")+" "+size+"cm "+species; } }Define a class called Lake that defines the following private...
import java.util.*; public class Movie { private String movieName; private int numMinutes; private boolean isKidFriendly; private int numCastMembers; private String[] castMembers; // default constructor public Movie() { movieName = "Flick"; numMinutes = 0; isKidFriendly = false; numCastMembers = 0; castMembers = new String[10]; } // overloaded parameterized constructor public Movie(String movieName, int numMinutes, boolean isKidFriendly, String[] castMembers) { this.movieName = movieName; this.numMinutes = numMinutes; this.isKidFriendly = isKidFriendly; numCastMembers = castMembers.length; this.castMembers = new String[numCastMembers]; for(int i=0;i<castMembers.length;i++)...
Here is the code for the Infant class: public class Infant{ private String name; private int age; // in months public Infant(String who, int months){ name = who; age = months; } public String getName(){ return name;} public int getAge(){ return age;} public void anotherMonth() {age = age + 1;} } The code box below includes a live Infant array variable, thoseKids. You cannot see its declaration or initialization. Your job is to find out which Infant in the array...
public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary search tree public Buildbst(int data) { this.data = data; this.left = null; this.right =null; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Buildbst getLeft() { return left; } public void setLeft(Buildbst left) { this.left = left; } public Buildbst getRight() { return right; } public void setRight(Buildbst right) { this.right = right; } }...
public class Song { private String title; private String artist; private int duration; public Song() { this("", "", 0, 0); } public Song(String t, String a, int m, int s) { title = t; artist = a; duration = m * 60 + s; } public String getTitle() { return title; } public String getArtist() { return artist; } public int getDuration() { return duration; } public int getMinutes() { return duration / 60; } public int getSeconds() { return...
cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /** * default constructor -- * Creates an matrix that has 2 rows and 2 columns */ public Matrix(int [][] m) { boolean valid = true; for(int r = 1; r < m.length && valid; r++) { if(m[r].length != m[0].length) valid = false; } if(valid) matrix = m; else matrix = null; } public String toString() { String output = "[ "; for (int i...
For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() { super(); System.out.println(“B() called”);...
For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() { super(); System.out.println(“B() called”); } public...
departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore { private static final int DEFAULT_SIZE = 10; private StaffMember [] myEmployees; private int myNumberEmployees; private String myFileName; private StaffMember[] employee; public DepartmentStore (String filename){ myFileName = filename; myEmployees = employee; } public String toString(){ return this.getClass().toString() + ": " + myFileName; } public void addEmployee(Employee emp){ } /** * prints out all the employees in the array list held in this class */ public void print(){ for(int i =...