Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
1ECE 39595 Java Lab, Spring 2021, Exam 2
The answer sheet is a separate sheet and can be edited, and therefore annotated with your answers.
Not counting the answer sheet, this exam has 7 pages and 34 questions each worth the same number of points.
You may begin the exam whenever it becomes available. I will give a 10 minute warning at the end of that the
exam answer sheet needs to have already been uploaded to Brightspace.
Answering questions. Most questions are something like // Q28. If the statement causes a compile time or
runtime error, answer “Err”. For the remainder of the program execution, assume the statement that gave an error
is not longer part of the program. If the statement has no output and does not cause an error, answer “Ok”. If the
statement does not cause an error and has output, give the output. If a statement has newlines, you do not need to
include those in your answer.
If you are not in zoom with video turned on you may recieve a 0 on the exam. I will be recording the exam.
Programs are given without import statements for brevity. Assume all needed imports are present.
2This page has questions 1 – 6, below. Note that Integer implements Comparable, and toString( ) for Integer returns
the value held by an Integer object as a String.
public class MyList {
private List list;
public MyList( ) {
list = new ArrayList( );
}
public void insert(E e) {
list.add(e);
}
public boolean contains(E t) {
boolean returnVal = false;
for (E e : list) {
if (e.compareTo(t) == 0) {
returnVal = true;
break;
}
}
return returnVal;
}
public E getHead(E e) {
return list.get(0);
}
public String toString( ) {
String str = "list: ";
for (E e : list) {
str += e + " ";
}
return str;
}
}
public class Obj implements Comparable {
private int data;
public Obj(int i) {
data = i;
}
// Obj.java continued
@Override
public int compareTo(Object t) {
Obj myT = (Obj) t;
if (data < myT.data) return -1;
if (data == myT.data) return 0;
if (data > myT.data) return 1;
return 0; // keeps javac
// happy, never executed!
}
public String toString( ) {
return "" + data;
}
}
public class Main {
public static void main(String[ ] args) {
MyList intList =
new MyList( ); // S1
MyList objList =
new MyList( ); // S2
for (int i=0; i<4; i++) {
intList.insert(Integer.valueOf(i));
}
System.out.println(intList); // Q1
intList.insert(new Obj(1)); // Q2
for (int i=0; i<4; i++) {
objList.insert(new Obj(i));
}
System.out.println(objList); // Q3
if (objList.contains(new Obj(2))) // Q4
System.out.println("true");
else System.out.println("false");
}
}
ˆ Q5. What is the type of data in the list of S1?
ˆ Q6. What is the type of data in the list of S2?
3This page has questions 7 – 8, below.
public interface Op {
int op(int a, int b);
}
public class MyList {
private int[ ] list = {0, 1, 2, 3};
public void update(int arg, Op update) {
for (int i=0; i<4; i++) {
list[i] = update.op(list[i], arg);
}
}
public void reset( ) {
for (int i=0; i<4; i++) {
list[i] = i;
}
}
@Override
public String toString( ) {
String str = "";
for (int i=0; i<4; i++) {
str += list[i] + " ";
}
return str;
}
}
public class Main {
public static void main(String[ ] args) {
Op op1 = (a,b) -> a + b;
Op op2 = (a,b) -> a * b;
MyList ml = new MyList( );
ml.update(1, op1);
System.out.println(ml); // Q7
ml.reset( );
ml.update(2, op2);
System.out.println(ml); // Q8
}
}
4This page has questions 9 – 10, below.
public interface Animal {
public String identify( );
}
public class Cat implements Animal {
@Override
public String identify( ) {
return "cat";
}
}
public class Dog implements Animal {
public Dog( ) { }
@Override
public String identify( ) {
return "dog";
}
}
public class AnimalMaker {
public static Animal make(String kind) {
Class cls = null;
Constructor constructor = null;
Animal animal = null;
try {
cls = Class.forName(kind);
constructor = cls.getConstructor( );
animal = (Animal) constructor.newInstance( );
} catch (Exception e) {
e.printStackTrace( );
}
return animal;
}
}
public class Main {
public static void main(String[ ] args) {
Animal animal = AnimalMaker.make("Dog");
System.out.println(animal.identify( )); // Q9
animal = AnimalMaker.make("dog"); // Q10
System.out.println(animal.identify( ));
}
}
5This page has questions 11 – 13, below.
public class Obj {
private int data;
public Obj(int i) {
i = data;
}
}
public class Main {
static void foo(Obj[ ] listParm) {
Obj o1 = new Obj(999);
for (int i=0; i