CSE1325 OOP Midterm
Name: UTA ID: 1000
Instructions:
1. Read all of the instructions for each question and answer what is asked. Do not write down random stuff if you donÕt know the answer.
2. The test is worth 100 points and there will be 10 points extra credit available.
3. If you have a question during the test, raise your hand and ask the proctor. You may or may not get an answer, but you wonÕt know unless you ask.
4. Check for bonus questions.
1.a. Given the constructor below, declare the class that it is a constructor for, list all the data fields that the class would have, and list all the methods that it would need to have (you don't have to write them, just give the first line with access type, return type, name, and parameters). Basically, you are writing the class around the constructor. [You may use the back of another page if needed but make a note on this page if the rest of the answer is somewhere else.] (12 points)
public ImAClass(String department, int courseNumber, Semester courseSemester, int year, String instructor, boolean preProfessional, boolean labClass, int numberEnrolled, int maxCapacity, char studentStatus)
{
super(department, courseNumber, courseSemester, year);
this.instructor = instructor;
this.preProfessional = preProfessional;
this.labClass = labClass;
if (!(setNumberEnrolled(numberEnrollled, maxCapacity)))
{
setNumberEnrolled(0,30);
}
if (!(setStudentStatus(studentStatus)))
{
setStudentStatus( 'U');
}
}
1.b What is the name of the enumerated type used in question 1.a? Define the enumerated type and make sure it contains at least five unique values that correspond to UTA definitions.
{6 pts}
1.c For studentStatus, the following are valid values (ignoring case) : U for undergraduate student, M for Master's student, D for Doctoral (PhD) student, B for second Baccalaureate, 5 for fifth year student, P for special student, N for non-degree seeking, and Z for other special cases. Write the method setStudentStatus that checks the validity of an input value passed in. {10 pts}
1.d Write a toString function for the class defined in 1a. If you refer to another class or toString method, you must also define that class or method. {8 pts}
1.e. Give a short answer to explain why the constructor in 1.a assigns values directly for instructor and preProfessional but calls setNumberEnrolled and setStudentStatus methods for those two values.
{8 pts}
1.f For the enumerated type in 1.b , assuming that input data is to be read in from a file, give two different ways in which the enumerated type value might be represented in the file and then give pseudocode to define how the data from the file would be turned into the enumerated value and stored in the enum data field. In other words, tell what data type the input would be that is stored in the file, tell what valid values that input could have, and give pseudocode for a method that would take the input value and then find and assign the corresponding enum value to the object. Do this TWO different ways. {12 pts}
2. For the following questions, assume the enum is visible to the two methods and assume the two methods are both in the same class.
public enum Material { PAPER, CARDBOARD, POLY, GLOSSY_STOCK, MISC }
public void setMadeOf(String sMadeOf) {
madeOf = materialFinder(sMadeOf);
madeOfWord = toMadeOfWord(madeOf);
}
private Material materialFinder(String im) {
int ic =0;
if (im.equalsIgnoreCase("P")) { ic = 1; }
else if (im.equalsIgnoreCase("C") { ic = 2; }
else if (im.equalsIgnoreCase("O")) { ic = 3; }
else if (im.equalsIgnoreCase("G")) { ic = 4; }
switch (ic) {
case 1: return Material.PAPER;
case 2: return Material.CARDBOARD;
case 3: return Material.POLY;
case 4: return Material.GLOSSY_STOCK;
default: return Material.MISC;
}
}
2.a. In the given code, the setMadeOf method is trying to set what data field (or data value) based on the String input? (Give the name of the data field) {3 pts}
2.b. What is the data type of the field referred to in 2.b. ? {3 pts}
2.c. What does the method materialFinder appear to do? Describe the purpose in sentences. Do not just write the Java code as English. {7 pts}
2.d. What do you think the method toMadeOfWord (that is called after the call to materialFinder) would do? Describe the purpose in sentences. {4 pts}
2.e. How could the algorithm in the method materialFinder be simplified? {5 pts}
2.f. Rewrite the method materialFinder to implement your simplification. {8 pts}
2.g. Assuming setMadeOf is called from a testing class that gets user input, write a small number of Java statements that would be in the testing class that will get appropriate input from the user and then call the setMadeOf class to validate and set the appropriate value. Be sure to declare any variables you use. You may assume that there is a Scanner called input that is linked to System.in .
{8 pts}
2.bonus Write any additional statements that would be needed to read the input values from a file "input.txt" instead of from the keyboard. You may assume that the value to be read is the first value in the file. Show where your extra lines would go in relation to the previous answer. {up to 5 pts XC}
Short answer questions
3. All data in the Java Programming language is contained by/within two types of things. One is primitives, what is the other? {1 pts}
4. List and describe at least two benefits of using library classes in Java. {2 pts}
5. What is the logic error in this code?
String foo = new String(ÒjavaÓ);
String bar = new String(ÒjavaÓ);
If(foo == bar) {2 pts}
6. How is a class different from an object? {1 pts}
Extra Credit questions
XC1. What is a .class file? {2 pts}
XC2. What is the ÔGarbage CollectorÕ? {3 pts}
XC3. What common method (found in most classes) would probably use the toMadeOfWord method and why would your method use it? {3 pts}
XC4. What question would you have asked on this midterm? {Any meaningful answer will receive 2 points. ANY answer will receive at least 1 point.