John Enloe Homework 1
3.1)
import java.util.ArrayList;
class HelloWorld
{
public static void main(String[] args)
{
ArrayList<Integer> arrlist = new ArrayList<Integer>(5);
arrlist.add(0);
arrlist.add(1);
arrlist.add(2);
arrlist.add(3);
arrlist.add(4);
for(int i=0; i<=1; i++)
{
for(Integer number : arrlist)
{
System.out.println("Number = " +number);
}
}
}
}
3.2)
The contents of list after this loop completes are: 1, 3, 6, 10, 15, 19, 22, 24, 25, 25
3.3)
for(Integer number : arrlist)
{
if(number < 0)
{
count++;
}
System.out.println(" Number = " +number);
}
System.out.println("count = " +count);
}
}
The count is:
Number = 1
Number = 2
Number = 3
Number = 4
Number = 5
Number = 4
Number = -1
Number = 2
John Enloe Homework 1
Number = 1
Number = 0
count = 1
3.4)
static int sum(ArrayList<integer> plist)
{
int sum = 0;
for (Integer d:plist)
sum = sum + d;
return sum;
}
}
3.5)
public E set(int index, Element E)
arraylist.set(0, 11);
3.6)
ArrayList<String> al = new ArrayList<String>();
al.add("Scott");
al.add("John");
al.add("Mike");
al.add("Dave");
al.add("Luke");
System.out.println("Elements of ArrayList of String Type: " + al);
3.7)
ArrayList<String> al = new ArrayList<String>();
al.add("AB");
al.add("CD");
al.add("EF");
al.add("GH");
al.add("AB");
al.add("YZ");
System.out.println("ArrayList before removal: ");
for(String var: al)
{
System.out.println(var);
}
al.remove(0);
al.remove(2);
John Enloe Homework 1
4.1)
The system throws an IOException
4.2)
The system throws an IOException
5.1)
Throwing an exception means you create a new exception that lets the program know when a condition
that was not expected occurs. Catching an exception allows the program to continue on executing by
handling that exception.
5.2)
A checked exception are those that are checked when the program is compiled. An example would be
an exception that does not compile after using the FileReader command when the desired file could not
be found.
5.3)
An unchecked exception is not checked when the program is compiled. Below is an example:
class Main {
public static void main(String args[]) {
int x = 0;
int y = 10;
int z = y/x;
}
}
5.4)
The type of uncaught exception that must be declared with the throws reserved word in a method
header is a checked exception.
5.5)
These do not have to be declared because these specific types of exceptions can be avoided by whoever
is writing the program. The could would become too complicated if you had to catch all of these
exceptions.
5.6)
Yes, the type of exception object is always the same as the type declared because the runtime system
will search for a section of code that can handle that particular exception.
5.7)
The purpose of the finally clause is to make the program capable of carrying out code whether or not an
exception is thrown or caught. An example of when it would be used is when connecting to a database
so that the connection can be closed and cleanup can be performed even if an exception was caused.
John Enloe Homework 1
5.8)
Next() throws NoSuchElementException and IllegalStateException – both of which are unchecked.
nextInt() throws InputMismatchException, NoSuchElementException, and IllegalStateException – all of
which are also unchecked.
6.1)
An instance method belongs to a class where an object is used to invoke. A static method can be
invoked without creating an object.
6.2)
If you do not write any constuctors, the compiler automatically generates a default constructor.
6.6)
C cObj1 = new C ();
6.7)
C cObj2 = new C(10);
6.8)
a) Illegal because a private variable can’t be accessed using its own class name in another class. mX is a
private variable
b) Illegal because a private variable can’t be accessed using its own class name in another class. mY is a
private variable
c) Illegal because a private variable can’t be accessed using its own class name in another class
d) Legal and the value of B is stored into the variable a4
e) Legal
f) Legal and it stores the value of cObj1 into the variable a5
g) Legal and it will set cObj1’s mX to 20
h) Legal and it will set cObj2’s mX to cObj1’s mX
i) Illegal because a non-static method can’t be accessed using its class name in another class. getX is not
static
j) Illegal because a non-static method can’t be accessed using its class name in another class. setX is not
static
k) Legal and it stores the value of cObj1 into the variable a7
l) Legal and it sets cObj1 mY value to 20
m) Illegal because a non-static method can’t be accessed using its class name in another class. getY is
not static
n) Illegal because a non-static method can’t be accessed using its class name in another class. The setY
method is not static
6.9)
a) It is legal and it sets both mX and mY to 0 when the method is executed
b) This is illegal. It sets mY to 0 but cannot set mX to 0 because it is a non-static variable and the method
it is in (g()) is static.
John Enloe Homework 1
7.1)
7.2)
a) The relationship between Course and Roster is composition
b) The relationship between Roster and Student is association
c) The relationship between Student and UnderGradStudent is generalization
d) The relationship between Student and GradStudent is generalization
e) There are no relationships