2.4.5.1. Class Name.
Solution.
The name of the class is
Riddle
.access modifier | method call and return |
class-level variable | null pointer |
default value | null pointer exception |
delimiter | pointer |
empty string | reference |
flow of control | reference variable |
interface | static modifier |
local variable | user interface |
public
can be accessed by other objects. Elements that are declared private
are hidden from other objects.private
so they cannot be accessed directly by other objects.new
operator in conjunction with a constructor method.public
), and its place in the Java class hierarchy (extends Object
). The class body contains declarations of the class’s variables and definitions of its methods.Object
.static
, such as the main()
method, are associated with the class(not with its instances).main()
method, which is where it begins execution.private
.new
operator and a constructor method.Riddle
.question
, answer
.Riddle()
, getQuestion()
, getAnswer()
.Riddle
instances: riddle1
, riddle2
.Riddle
objects in the program: Riddle("What is black and white and red all over?", "An embarrassed zebra.")
Riddle("What is black and white and read all over?","A newspaper.")
riddle1.getQuestion()
riddle1.getAnswer()
riddle2.getQuestion()
riddle2.getAnswer()
riddle1.getQuestion()
and riddle1.getAnswer()
.OneRowNim
Class
Riddle
class: private String hint;
getHint()
method of the Riddle
class, which should be a public
method, is: public String getHint()
{
return hint;
}
setHint()
method of the Riddle
class, with the result type void
. is: public void setHint(String aHint)
{
hint = aHint;
}
Student
class is given below. public class Student
{ private String firstName;
private String lastName;
private int studentID;
public void setStudent(String fName, String lName,int anID)
{
firstName = fName;
lastName = lName;
studentID = anID;
}
public int getStudentID() { return studentID; }
public String getStudentName() (
return firstName + " " + lastName;
}
}
java.util.Scanner
public static void main(String[] args)
{ // Create Scanner object
Scanner sc = Scanner.create(System.in);
System.out.print("Input a real number:"); // Prompt
double realNum= sc.nextDouble(); // Read a double
System.out.println(num + " squared = " + realNum*realNum);
} //main()