作者lucky0509 (默)
看板FCUProblems
标题[考古] 物件导向设计/张贵忠/991-期中考
时间Thu Nov 4 00:53:36 2010
[开课学院]:资电学院 (ex:金融学院,商学院,理工学院,资电学院,建设学院,文学院...)
[开课系所]:资讯系 (ex:中文系,外文系,电机系,财税系..)
[课程名称]:物件导向设计
[老师名称]:张贵忠 老师
[开课学期]:991
[类型]:99-1 期中考 ex:(第n次)小考/98-2期中考/98-2期末考
Totally 100 points
1. (Basic Concept - 10 pts) Just a brief explanation
(a) Class and Object
(b) Overloading
2. (Encapsulation - 20 pts)
(a).Discuss the public and private modifiers in context of methods
and instance variables.
(b).Discuss the importance of accessor and mutator methods and how they
apply to the abstraction concept.
3. (Defining Class - 25 pts) Write a Java class that represents a Student with
String-type instance variables name and id. In addition, please define at
least 2 constructors, 2 accessor methods and 2 mutator methods for the
class. Write a simple main program to create 3 Student object.
4. (Basic Flow Control - 20 pts) Write a complete Java program that uses a
"while loop" to compute the sum of the even numbers and the sum of the odd
numbers between 1 and 25(include 1 and 25).
5.(Programming Style - 10 pts)The following program will compile and run,but
it uses porr programming style.Modify the program so that it uses the
spelling conventions, constant naming conventions,and formatting style
recommended in the book.
-----------------------------------------------------------------------------
public class messy{
public static final double distance =8.21;
public static void main(String[] args)
{
double TIME;
double PACE;
TIME = 45.5;
PACE=TIME/distance;
System.out.println("Your pace is"+PACE+"miles per hour.");
}
}
-----------------------------------------------------------------------------
6. (Advanced Class & Object Concept - 25 pts)
(a) Find all errors in the following code, and give brief explanation.
(b) Please write down the output of the program when you correct all errors.
------------------------------------------------------------------------------
public calss PClass
{
private String name;
private int number;
public PClass(int n)
{
name = "";
number = n;
}
public void set(String newName, int newNumber)
{
name = newName;
number = newNumber;
}
public void set(String newName1, double newNumber1)
{
name = newName1;
}
public boolean set(String newName2, double newNumber2)
{
name = newName2;
return true;
}
public String toString()
{
return (name + " " + number );
}
public static int getNumber()
{
return number;
}
public void makeEqual(PClass anObject)
{
anObject.name = this.name;
anObject.number = this.number;
}
}//End of class
------------------------------------------------------------------------------
------------------------------------------------------------------------------
public class MidExam
{
public static void main(String[] args)
{
int objNumber;
int objPrice;
PClass object1 = new PClass(),
PClass object2 = new PClass();
object1.set("AAA",1);
object2.set("BBB",2);
System.out.println("Value of object2 before call:");
System.out.println(object2);
object1.makeEqual(object2);
objNumber = object1.number;
System.out.println("Value of object2 after call:");
System.out.println(object2);
}//End of main
}//End of Class
------------------------------------------------------------------------------
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 118.170.2.251
1F:推 k26435022 :推好老师!! 11/04 01:39