Title | Objects Classes Tut 3 |
---|---|
Course | Model-Based Software Development |
Institution | University College Cork |
Pages | 3 |
File Size | 202.7 KB |
File Type | |
Total Downloads | 100 |
Total Views | 147 |
Objects Classes Tut 3...
Tutorial 3 Objects, Classes & Methods
Question 1: What is a method signature?
Question 2:
What is a constructor and what is its purpose?
Question 3: What is a noargs(default) constructor?
Question 4:
What does the following do when executed? Student s1 = new Student();
Question 5: What does the following imply for the class Student? Student s2 = new Student(“Jane Austin”, 1, “SDev”);
Write the appropriate constructor header for above.
Question 6:
Edit the class Rectangle’s constructor so that it accepts in length and width but if either dimension is less than 0.0 the member fields are set to 0.
Question 7:
Consider the following class declaration. class QuestionOne { public final int A = 345; public int b; private float c; private void methodOne( int a ) { b = a; } public float methodTwo() { return 23; } }
Identify invalid statements in the following main class. For each invalid statement, state why it is invalid. class Q1Main { public static void main( String[] args ) { QuestionOne q1; q1 = new QuestionOne(); 1
q1.A = 12; q1.b = 12; q1.c = 12;
2 3 4 5 12 ) ); 6
q1.methodOne( 12 ); q1.methodOne(); System.out.println( q1.methodTwo( q1.c = q1.methodTwo(); }
}
Question 8:
What will be the output from the following code? class Q2Main { public static void main( String[] args ) { QuestionTwo q2; q2 = new QuestionTwo( ); q2.init(): q2.increment(); q2.increment(); System.out.println( q2.getCount() ); } } }
class QuestionTwo { private int count; public void init( ) { count = 1; } public void increment( ) { count = count + 1; } public int getCount() { return count;} }
Question 9:
What will be the output from the following code? Q3Main and QuestionThree classes are the slightly modified version of Q2Main and QuestionTwo. class Q3Main { public static void main( String[] args ) { QuestionThree q3; q3 = new QuestionThree( ); q3.init() ; q3.count = q3.increment() + q3.increment(); System.out.println( q3.increment() ); } } }
class QuestionThree { public int count; public void init( ) { count = 1; } public int increment() { count = count + 1; return count; } }
Question 10:
Define a new class named Temperature. The class has two accessors/getters—toFahrenheit and toCelsius—that return the temperature in the specified unit and two mutators/setters—setFahrenheit and setCelsius— that assign the temperature in the specified unit. Maintain the temperature internally in degrees Fahrenheit. Using this class, write a program that asks the user to input temperature in degrees Fahrenheit and outputs the temperature in equivalent degrees Celsius. Formula to convert from Fahrenheit to celsius : Celsius = (5.0 /9.0) * ( fahrenheit -32) Formula to set in degrees Celsius: Fahrenheit = 1.8 * degreesIn + 32...