Name: ____________________________________________________ Alpha: _____________________
Describe help received: _________________________________________________________________
| HWEX1.java | Rat.java |
Identify each of the following as either primitive,
an array-reference or a class-reference:
args __________________ subj1 __________________ sc __________________ nm __________________ wt __________________ wtInGrams __________________ r __________________ r.name __________________ r.weight __________________ |
import java.util.*;
public class HWEX1
{
public static Rat read()
{
Scanner sc = new Scanner(System.in);
String nm = sc.next();
double wt = sc.nextDouble();
double wtInGrams = wt*28.3495;
Rat r = new Rat();
r.name = nm;
r.weight = wtInGrams;
return r;
}
public static void main(String[] args)
{
Rat subj1 = read();
}
} |
class Rat
{
String name;
double weight;
} |
Rat are created when this program is run?
How many instances of class String are created
when the function read() is called, assuming the
user inputs name & weight properly.
Explain your answers! [Note: "instance" is a technical term.
Read the notes!]
public class HWEX2
{
public static int fact(int n)
{
return n < 2 ? 1 : n*fact(n-1);
}
public static void main(String[] args)
{
for(int i = 1; i < 10; i++)
System.out.println(i + ": " + fact(i));
}
} |
How would you call the function fact to compute 15 factorial
from the main function of another .java file (assume it is
HW.java)? If you run the program from HW.java (i.e. give the
command java HW), how does the JVM know where to
find the code for the fact function?
|
Create a file entitled Mid.java and put this class definition in it:
public class Mid
{
public String alpha;
public String firstName;
public String lastName;
public int company;
}
Then write a program entitled HW3.java with the following attributes:
createMid method that takes a Scanner as
argument and asks the user for the information necessary to
construct a Mid. It then returns that Mid.printMid method that takes a Mid as an
argument and prints the information
about that Mid to the screen in the format shown below.
main method that 1) reads in a number of mids,
2) reads info for that many mids, 3) reads in a company number, 4)
prints the info for the mids from that company in the same order
as read in. You should, of course, use "createMid" and "printMid".
| Example Runs | ||
|---|---|---|
$ java HW3
5
230531 Noah Taylor 3
233159 Sophia White 17
236234 Jackson Miller 3
237198 Isabella Wilson 17
234735 Aiden Brown 3
3
230531 Noah Taylor 3
236234 Jackson Miller 3
234735 Aiden Brown 3 |
$ java HW3
5
230531 Noah Taylor 3
233159 Sophia White 17
236234 Jackson Miller 3
237198 Isabella Wilson 17
234735 Aiden Brown 3
17
233159 Sophia White 17
237198 Isabella Wilson 17 |
$ java HW3
5
230531 Noah Taylor 3
233159 Sophia White 17
236234 Jackson Miller 3
237198 Isabella Wilson 17
234735 Aiden Brown 3
12
|
submit -c=IC211 -p=hw03 HW3.java Mid.java