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:
downloads
directory/folder). In most browsers/OSs, the
easiest way to do this is by right-clicking/control-clicking on
each of the links above and then selecting
.java
Files: In most IDEs, .java
files
(i.e., source files) can just be copied into the project.
See the course "Help" page on your IDE for more information.
.class
and .jar
Files:
In some IDEs it is easier to use .class
files and in others it is easier to use a .jar
file
that contains the .class
files. Hence, you have been
provided with both.
See the course "Help" page on your IDE for more information.
Resources: In some IDEs, resources (e.g., images, data files) need to be in a special directory whereas in others they need to be in the working directory. See the course "Help" page on your IDE for more information.
Account
class.
Account.java
, for example, in the main()
method of a source file named AccountDriver.java
).
Account[] customers;
int[] sales;
String[] months;
sales = new int[12];
months = new String[12];
customers = new Account[1000];
sales
a primitive type or a reference type?
sales[0]
a primitive type or a reference type?
months
a primitive type or a reference type?
months[0]
a primitive type or a reference type?
customers
a primitive type or a reference type?
customers[0]
a primitive type or a reference type?
int
variable initialized to?
System.out.println(sales[0]);
Will this statement compile? If so, will it execute properly? If so, what is printed by the additional statement?
sales
. Would the line you just added compile?
System.out.println(months.length);
Will this statement compile? If so, will it execute properly? If so, what is printed by the additional statement?
System.out.println(months[0].length());
Will this statement compile? If so, will it execute properly? If so, what is printed by the additional statement?
length
and length()
in this fragment?
months[0].length()
.
months[0] = new String("December");
Now what will be printed by the last statement?
System.out.println(customers[0].getBalance());
Will this statement compile? If so, will it execute properly? If so, what is printed by the additional statement?
customers[0].getBalance()
.
customers[0] = new Account();
Now what will be printed by the last statement?
Be careful!
When a question begins with "What is printed by" you are starting a
fragment from scratch (in a source file other
than Account.java
, for example, in a source file
named ArrayDriver.java
). When a question begins with
"Add the following" you are continuing an existing fragment.
double temp;
double[] balances;
balances = new double[3];
System.out.printf("\n\nDuring initialization:\n");
// Balance 0
temp = 0.00;
balances[0] = temp;
System.out.printf("0: %6.2f\n", balances[0]);
// Balance 1
temp = 100.00;
balances[1] = temp;
System.out.printf("1: %6.2f\n", balances[1]);
// Balance 2
temp = 200.00;
balances[2] = temp;
System.out.printf("2: %6.2f\n", balances[2]);
System.out.printf("\n\nBefore exiting:\n");
for (int i=0; i<balances.length; i++)
{
System.out.printf("%1d: %6.2f\n", i, balances[i]);
}
What is printed by the additional statements?
Account temp;
Account[] accounts;
accounts = new Account[3];
System.out.printf("\n\nDuring initialization:\n");
// Account 0
temp = new Account();
temp.setID("Iggy Azalea");
temp.setBalance(0.00);
accounts[0] = temp;
System.out.printf("0: %15s %6.2f\n",
accounts[0].getID(), accounts[0].getBalance());
// Account 1
temp.setID("Common");
temp.setBalance(100.00);
accounts[1] = temp;
System.out.printf("1: %15s %6.2f\n",
accounts[1].getID(), accounts[1].getBalance());
// Account 2
temp.setID("Rihanna");
temp.setBalance(200.00);
accounts[2] = temp;
System.out.printf("2: %15s %6.2f\n",
accounts[2].getID(), accounts[2].getBalance());
System.out.printf("\n\nBefore exiting:\n");
for (int i=0; i<accounts.length; i++)
{
System.out.printf("%1d: %15s %6.2f\n", i,
accounts[i].getID(), accounts[i].getBalance());
}
What is printed by the additional statements?
Account
objects in the array contain the same attribute
values. Why is this happening?
Account
objects as in the following fragment.
Account temp;
Account[] accounts;
accounts = new Account[3];
System.out.printf("\n\nDuring initialization:\n");
// Account 0
temp = new Account();
temp.setID("Iggy Azalea");
temp.setBalance(0.00);
accounts[0] = temp;
System.out.printf("0: %15s %6.2f\n",
accounts[0].getID(), accounts[0].getBalance());
// Account 1
temp = new Account();
temp.setID("Common");
temp.setBalance(100.00);
accounts[1] = temp;
System.out.printf("1: %15s %6.2f\n",
accounts[1].getID(), accounts[1].getBalance());
// Account 2
temp = new Account();
temp.setID("Rihanna");
temp.setBalance(200.00);
accounts[2] = temp;
System.out.printf("2: %15s %6.2f\n",
accounts[2].getID(), accounts[2].getBalance());
System.out.printf("\n\nBefore exiting:\n");
for (int i=0; i<accounts.length; i++)
{
System.out.printf("%1d: %15s %6.2f\n", i,
accounts[i].getID(), accounts[i].getBalance());
}
What is printed by this fragment?
temp
is completely unnecessary. Without
changing the constructor that is being used, change the following
temp = new Account();
temp.setID("Iggy Azalea");
temp.setBalance(0.00);
accounts[0] = temp;
to a more elegant implementation that does not use temp
.
Account nelly;
Account[] accounts;
accounts = new Account[10];
nelly = new Account();
nelly.setID("Nelly");
accounts[0] = nelly;
System.out.printf("%15s %15s\n", nelly.getID(), accounts[0].getID());
nelly.setID("Lil Nelly");
System.out.printf("%15s %15s\n", nelly.getID(), accounts[0].getID());
What is printed by the additional statements?
accounts[0].setID("Nelly Dogg");
System.out.printf("%15s %15s\n", nelly.getID(), accounts[0].getID());
What is printed by the additional statements?
Account cordozar;
Account[] accounts;
accounts = new Account[10];
accounts[0] = new Account();
accounts[0].setID("Snoop Dogggy Dog");
cordozar = accounts[0];
System.out.printf("%20s %20s\n", cordozar.getID(), accounts[0].getID());
cordozar.setID("Snoop Dogg");
System.out.printf("%20s %20s\n", cordozar.getID(), accounts[0].getID());
What is printed by the additional statements?
accounts[0].setID("Snoop Lion");
System.out.printf("%20s %20s\n", cordozar.getID(), accounts[0].getID());
What is printed by the additional statements?
public static void main(String[] args)
{
int[] creditScores = {650, 750};
System.out.printf("Before process(): %3d %3d\n",
creditScores[0], creditScores[1]);
process(creditScores);
System.out.printf("After process(): %3d %3d\n",
creditScores[0], creditScores[1]);
}
public static void process(int[] scores)
{
System.out.printf("In process(): %3d %3d\n",
scores[0], scores[1]);
}
process()
method
(i.e., before the call to printf()
).
scores[0] = 300;
What is printed now?
public static void main(String[] args)
{
double temp;
double[] balances;
balances = new double[5];
for (int i=0; i<balances.length; i++)
{
balances[i] = i * 100.0;
}
System.out.printf("\n\nBefore reverse():\n");
for (int i=0; i<balances.length; i++)
{
System.out.printf("%d: %6.2f\n", i, balances[i]);
}
}
public static void reverse(double[] a)
{
double temp;
int n;
n = a.length;
for (int i=0; i<n/2; i++)
{
temp = a[i];
a[i] = a[n-i-1];
a[n-i-1] = temp;
}
}
main()
.
reverse(balances);
System.out.printf("\n\nAfter reverse():\n");
for (int i=0; i<balances.length; i++)
{
System.out.printf("%d: %6.2f\n", i, balances[i]);
}
What is printed by the additional statements?
public static void main(String[] args)
{
double[] oneDigit, twoDigit;
oneDigit = new double[5];
twoDigit = new double[5];
for (int i=1; i<oneDigit.length; i++)
{
oneDigit[i] = i;
twoDigit[i] = i * 10;
}
System.out.printf("%8s %8s\n", "oneDigit", "twoDigit");
for (int i=1; i<oneDigit.length; i++)
{
System.out.printf("%8.0f %8.0f\n", oneDigit[i], twoDigit[i]);
}
}
public static void swap(double[] a, double[] b)
{
double[] temp;
temp = a;
a = b;
b = temp;
}
main()
method:
swap(oneDigit, twoDigit);
System.out.printf("\n\nAfter call to swap()\n");
System.out.printf("%8s %8s\n", "oneDigit", "twoDigit");
for (int i=1; i<oneDigit.length; i++)
{
System.out.printf("%8.0f %8.0f\n", oneDigit[i], twoDigit[i]);
}
What is printed by the additional statements?
reverse()
method. That is, we must swap individual elements. To do so.
change the swap()
method to the following:
public static void swap(double[] a, double[b])
{
double temp;
for (int i=0; i<a.length; i++)
{
temp = a[i];
a[i] = b[i];
b[i] = temp;
}
Now what is printed by the application?
Copyright 2024