In: Computer Science
Write a JAVA program to display your complete names, your matric number and your course of study, using the two access modifiers stated in (a) and (b)
(a) Use static access modifier for the method declaration of the program class, also use class name Eee532MakeUp. Use any method name of your choice.
(b) Use public access modifier for the method declaration of the program class, also use class name EceCourseJava.
(2) Which of the programs in (a) or (b) is easier to write, give reason for your response?
Program:-
(a) Using static access modifier:-
Code:-
import java.util.Scanner;
class Eee532MakeUp{
/* Method to display Complete name, Matric number
& Course of Study using static
access modifier */
static void display(String fullName, int matricNumber,
String studyCourse){
System.out.println("\nYour Complete Name = " + fullName);
System.out.println("Your Matric Number = " + matricNumber);
System.out.println("Your Course of Study = " + studyCourse);
}
public static void main(String[] ar){
//variable declaration for name,
study course and matric number
String fullName, studyCourse;
int matricNumber;
//creating input object using Scanner class
Scanner sc = new Scanner(System.in);
//Taking user's input
System.out.print("Enter your Complete Name = ");
fullName = sc.nextLine();
System.out.print("Enter your Matric Number = ");
matricNumber = sc.nextInt();
System.out.print("Enter your Course of Study = ");
studyCourse = sc.next();
//calling display method without instance of class
display(fullName, matricNumber, studyCourse);
}
}
-
-
Output:-
-
-
(b) Using public access modifier:-
Code:-
import java.util.Scanner;
class EceCourseJava{
/* Method to display Complete name, Matric number & Course
of Study using public
access modifier */
public void display(String fullName, int matricNumber,
String studyCourse){
System.out.println("\nYour Complete Name = " + fullName);
System.out.println("Your Matric Number = " + matricNumber);
System.out.println("Your Course of Study = " + studyCourse);
}
//main method
public static void main(String[] ar){
//variable declaration for name, study course and matric
number
String fullName, studyCourse;
int matricNumber;
//creating instance of class
EceCourseJava ob = new EceCourseJava();
//creating input object using Scanner class
Scanner sc = new Scanner(System.in);
//Taking user's input
System.out.print("Enter your Complete Name = ");
fullName = sc.nextLine();
System.out.print("Enter your Matric Number = ");
matricNumber = sc.nextInt();
System.out.print("Enter your Course of Study = ");
studyCourse = sc.next();
//calling display method using object of class
ob.display(fullName, matricNumber, studyCourse);
}
}
-
-
Output:-
-
-
ANS -- (2):-
Program (a) using static access modifier is easy to write because if any method is static then no need to create instance or object of that class to call that method, method will call without object that means don't need to have object to access that method as shown in the program. On the other hand, using public access modifier we need to have at least one object to access method, without object program won't run and will give error.
So, Using static access modifier is easy to write program as compared to using public access modifier.