Program Assignment

Goal: Learning Method Calling, Accessing static, non-static variables from other classes, private modifier.

  1. Create a class Called School.
  2. Have non-static variables as below. int mark; private int salary;
  3. Have static variable as below. static String school_name = “St. Antony’s Primary School”;
  4. Define non-static methods mentioned below void conduct_exams() – Have a print statement inside this method void publish_results(int mark) – Have a print statement inside this method to print mark
  5. Now, create another class called Teacher
  6. Create main method inside Teacher class
  7. Create an instance(object) for School class [Object name -> teacher]
  8. Using ‘teacher’ object, call conduct_exams() method
  9. Using ‘teacher’ object, call publish_results() method and pass 75 as argument here.
  10. Print school_name
  11. Try to access private variable salary in Teacher class and note down the error message.

Solution:

Schoo.java

class School
{
int mark;
private int salary;
static String school_name="St.Antony's Primary School";
	
void conduct_exames()
{
  System.out.println("Conduct exames");
}
void publish_results(int mark)
{
  System.out.println("Publish results " + mark);
 }
	
}
 
 

Teacher.java

class Teacher
{
public static void main(String[] args)
{
School teacher= new School();
teacher.conduct_exames();
teacher.publish_results(75);
	System.out.println(teacher.school_name);
	}
	
}

Output:

 javac School.java 
 javac Teacher.java 
 java Teacher 
Conduct exames
Publish results 75
St.Antony's Primary School

Leave a Reply

Create your own website, free on WordPress.com.Create your own website.
Start free