Instructions: Answer the following questions one at a time. After answering each question, check your answer (by clicking on the check-mark icon if it is available) before proceeding to the next question.
Getting Ready: Before going any further, you should:
DataUtilities.java
that contains the
following.
import java.io.*;
import java.util.*;
public class DataUtilities
{
public static int[][] create(int rows, int columns)
{
return data;
}
}
create()
method.
int[rows][columns] data;
create()
method to:
int[][] data;
create()
method
after the declaration of data
but before
the return
statement.
int[] furniture = {100, 95, 485, 290};
int[] appliances = {300, 100, 0, 500};
furniture
and
appliances
?
furniture
and appliances
.
data = new int[rows][];
data
data[0] = furniture;
data
?
data[0]
?
furniture
?
data
(after it is
instantiated)?
furniture
(after it is
initialized)?
data[0]
(after it is
initialized)?
data
data[1] = appliances;
create()
method?
DataUtilities.java
. (It should compile.)
static
method named print()
to the
DataUtilities
class that is passed
an int[][]
named table
,
an int
named rows
and
an int
named columns
and doesn't return
anything.
print()
method.
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < columns; c++)
{
System.out.printf("%8d ", table[r][c]);
}
System.out.printf("\n");
}
rows
is 2 and columns
is 4.
How many int
values will be printed?
DataUtilities.java
. (It should compile.)
Driver.java
that contains the
following.
public class Driver
{
public static void main(String[] args)
{
}
}
main()
method.
int[][] sales;
sales = DataUtilities.create(2, 4);
DataUtilities.print(sales, 2, 4);
Driver
.
create()
as follows.
sales = DataUtilities.create(3, 4);
Driver
compile?
Driver
execute properly? Why or why not?
create()
as follows.
sales = DataUtilities.create(2, 2);
Driver
compile?
Driver
execute properly? Why or why not?
create()
as follows.
sales = DataUtilities.create(1, 4);
Driver
compile?
Driver
execute properly? Why or why not?
create()
as follows.
sales = DataUtilities.create(2, 4);
print()
as follows.
DataUtilities.print(sales, 1, 3);
Driver
compile?
Driver
execute without throwing an exception?
print()
as follows.
DataUtilities.print(sales, 2, 5);
Driver
compile?
Driver
execute without throwing an exception?
print()
as follows.
DataUtilities.print(sales, 3, 4);
Driver
compile?
Driver
execute without throwing an exception?
length
attribute?
print()
method so that the outer
loop uses the length of table
.
Driver
execute without throwing an exception?
table[0]
?
table[0]
?
table[1]
?
table[r]
?
print()
method so that the inner loop no longer
uses the parameter columns
.
print()
method use the
parameters rows
and columns
?
rows
and
columns
from the print()
method and
the corresponding actual parameters from the call to this method.
create()
method in the
DataUtilities
class after
appliances
is assigned to data[1]
but before the return
statement.
appliances[500] = 999;
ArrayIndexOutOfBoundsException
?
appliances[2] = 999;
Driver
. What is output?
create()
to the following:
data[0] = appliances;
appliances[2] = 999;
data[1] = appliances;
create()
back
to the following:
data[0] = furniture;
data[1] = appliances;
Driver
. What was outupt?
create()
back
to the following:
data[0] = furniture;
data[1] = appliances;
furniture
as follows.
int[] furniture = {100, 95, 485};
Driver
. What was outupt?
furniture
and appliances
as follows:
as follows.
int[] furniture = {100, 95, 485, 290, 880, 730};
int[] appliances = {300, 100};
Driver
. What was outupt?
return
statement.
data[2] = furniture;
create()
method
in the DataUtilities
class and the call to
the create()
method in the Driver
class.
DataUtilities
class.
public static int[][] read(String name, int rows, int columns)
throws FileNotFoundException
{
int[][] data;
Scanner scanner;
scanner = new Scanner(new File(name));
scanner.useDelimiter(",|\\r\\n");
}
Note that the Scanner
in this method treats both the
comma character and the carriage return character followed by
the line feed character as delimiters. A comma is used between columns
and a carriage return followed by a line feed is used between rows.
data
so that it has
rows
elements, each of which contains an array
of column
elements. What code did you add?
for
loop that iterates over the rows
.
What code did you add?
for
loop that iterates over
the columns
. What code did you add?
nextInt()
method of scanner
and assigns the result to the appropriate
element of data
. What code did you add?
return
statement.
Driver
so that it now contains the following code.
import java.io.*;
public class Driver
{
public static void main(String[] args)
{
int[][] sales;
try
{
sales = DataUtilities.read(args[0], 12, 5);
DataUtilities.print(sales);
}
catch (FileNotFoundException fnfe)
{
System.out.println("Unable to open " + args[0]);
}
}
}
Driver
, passing
it sales_2009-2013.txt
as the command-line argument.
.txt
file into
a text editor or a spreadsheet to check.)
Driver
, passing
it sales_1992-2013.txt
as the command-line argument.
.txt
file into
a text editor or a spreadsheet to check.)
read()
method reads one element at a time
it must know the number of row and columns. One could, instead, create
a read()
method that reads a row at a time (as a line).
To that end, add the following code to the DataUtilities
class.
public static int[][] read(String name, int rows)
throws FileNotFoundException
{
int[][] data;
int[] row;
String line;
Scanner scanner;
scanner = new Scanner(new File(name));
}
read()
methods with different
signatures in the DataUtilities
class. What word is used to
describe this situation?
data
so that it has
rows
elements, each of which contains an
int[]
. What code did you add?
for
loop that iterates over the rows
.
What code did you add?
for
loop, add a statemen that reads the next line
from scanner
and assigns the result to the variable named
line
. What code did you add?
for
loop, add a statement that calls the
line
object's split()
method, passing
it the String
literal ","
, and assigning the
result to the variable rows
.
What code did you add?
for
loop, add a statement that
instantiates element r
of data
to be an int[]
that contains as many elements as
tokens
.
What code did you add?
for
loop that iterates over
the elements of tokens
, converts each to an
int
, and assigns that int
to the appropriate
element of data
. What code did you add?
Driver
so that it uses this version of the
read()
method. What code did you change?
Driver
, passing
it sales_2009-2013.txt
as the command-line argument.
.txt
file into
a text editor or a spreadsheet to check.)
Driver
, passing
it sales_1992-2013.txt
as the command-line argument.
.txt
file into
a text editor or a spreadsheet to check.)
Copyright 2019