Topic 3 - Basic Classes
Topic 3 - Basic Classes
Basic Classes
Data members
Methods definition
Static fields
In OOP, the central modules are classes where data and the procedure to manipulate the data are
encapsulated
A Java program is composed of one or more classes.
One of the classes in a program must be designated as the main class
To design a class, you must know what data need to manipulate and what operations need to
manipulate the data
Class Definition
Class header
Accessor /
Constructor Mutator
Retriever
String
StringBuffer
Math
Vector
Hashtable
Predefined Classes
Instead of using the data type specific methods such as nextInt, nextDouble, and
others of the Scanner class, we can input a numerical value in a string format and
convert it to an appropriate data type by ourselves. For example, we can use the
class method parseInt of the Integer class to convert a string to an int. Here’s a
statement that converts "14" to an int value 14:
int num = Integer.parseInt("14");
So, the statement
int num = Integer.parseInt(scanner.next( ));
is equivalent to
int num = scanner.nextInt( );
Sample Program
public class Actor Class header : modifier, reserved word class and class name
{
private String actName;
Attributes / data members/ variables
private String movieName; declaration: identify attributes of an object
private double hourWork; and its data type
public void setData(String n, String m, double h) Mutator method : to set the object’s value
Method definition {
actName = n;
movieName = m;
hourWork = h; }
public class ActorApp Class header : modifier, reserved word class and class name
{
public static void main(String[] args) Main method
{
Create input object : to input
Scanner a = new Scanner(System.in); using I/O console