Question

In: Computer Science

Write a JAVA program to display your complete names, your matric number and your course of...

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?

Solutions

Expert Solution

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.


Related Solutions

Write a JAVA program that prompts the user for the number of names they’d like to...
Write a JAVA program that prompts the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names     &
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close Do you...
Q1: Write a Java program that will display different messages depending on your age. Your program...
Q1: Write a Java program that will display different messages depending on your age. Your program should ask the user for his/her name and their age in years and give one or more answers from the following ones below: if the age of the user is less than 16, the program should print on the screen “You are not allowed to drive at the moment”. if the age of the user is less than 18, the program should print on...
Java Program Use for loop 1.) Write a program to display the multiplication table of a...
Java Program Use for loop 1.) Write a program to display the multiplication table of a given integer. Multiplier and number of terms (multiplicand) must be user's input. Sample output: Enter the Multiplier: 5 Enter the number of terms: 3 5x0=0 5x1=5 5x2=10 5x3=15 2 Create a program that will allow the user to input an integer and display the sum of squares from 1 to n. Example, the sum of squares for 10 is as follows: (do not use...
Write a complete program in java that will do the following:
Write a complete program in java that will do the following:Sports:             Baseball, Basketball, Football, Hockey, Volleyball, WaterpoloPlayers:           9, 5, 11, 6, 6, 7Store the data in appropriate arraysProvide an output of sports and player numbers. See below:Baseball          9 players.Basketball       5 players.Football           11 players.Hockey            6 players.Volleyball        6 players.Waterpolo       7 players.Use Scanner to provide the number of friends you have for a team sport.Provide an output of suggested sports for your group of friends. If your...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1....
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps:...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods...
Write a program in java that uses methods to input data for calculation, calculate, and display...
Write a program in java that uses methods to input data for calculation, calculate, and display the results of the calculations. That is, there are at least three methods. The problem is to write a program that calculates the area of a rectangle. This action should be repeatable.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT