Exam 4: Defining Classes I
Write a Java method that prints the phrase "Five purple people eaters were seen munching Martians".
/** void method that prints a phrase */ public void printPhrase() { System.out.println("Five purple people eaters were seen munching Martians!"); }
Add two constructors to the Appointment class created in question #9.Include a default constructor and a constructor to initialize an Appointment to suitable arguments.
/** Class constructors */ public Appointment() { startTime = 0; endTime = 0; dayOfWeek = ""; month = ""; day = 0; year = 0; } public Appointment(int st,int et,String dow,String m,int d,int y) { setStartTime(st); setEndTime(et); setDayOfWeek(dow); setMonth(m); setDay(d); setYear(y); }
Write a method called power the computes xn where x and n and positive integers.The method has two integer parameters and returns a value of type long.
/** x and n are nonnegative integers */ public long power(int x,int n) { long result = 1; /** check for positive numbers */ if((x >= 0)&& (n >= 0)) { /** raise x to the nth power */ for(int i = n; i > 0; i--) result *= x; } else { result = 0; System.out.println("Fatal error.......positive integers required!"); } return result; }
A _________ states what is assumed to be true when the method is called.
Discuss the importance of accessor and mutator methods and how they apply to the abstraction concept.
Two methods that are expected to be in all Java classes are:
An invocation of a method that returns a value can be used as an expression any place that a value of the Type_Returned can be used.
Discuss the public and private modifiers in context of methods and instance variables.
The body of a method that returns a value must contain at least one _________ statement.
It is considered good programming practice to validate a value passed to a mutator method before setting the instance variable.
Write a Java class that represents a Student with instance variables name,id,and gpa.Include constructors,accessor,mutator and any facilitator methods you may need.
A variable whose meaning is confined to a method definition is called an/a
A method that performs some action other than returning a value is called a __________ method.
Boolean expressions may be used to control if-else or while statements.
Write a driver program to test your Student class created in question #14.
Filters
- Essay(0)
- Multiple Choice(0)
- Short Answer(0)
- True False(0)
- Matching(0)