Practice Exercises Java - Learn to program performing exercises with Java

Practice Exercises Java

Learn to program performing exercises with Java

12 Lessons Java with the Solutions - 228 Exercises Java with the solutions
For Beginners, Intermediates and Advanceds


App Practice Exercises Java

The human knowledge belongs to the world
¡The infomation should be free!



Classes Student + Teacher - Practice Exercises Java


Lesson 6:

Object oriented programming


Exercise 6.1:

Classes Student + Teacher


Objetive:

Create a new project, and include in it the class Person that you just created.

Create a class "Student" and another class "Teacher", both descendants of "Person".

The class "Student" will have a public method "GoToClasses", which will write on screen "I’m going to class."

The class "Teacher" will have a public method "Explain", which will show on screen "Explanation begins". Also, it will have a private attribute "subject", a string.

The class Person must have a method "SetAge (int n)" which will indicate the value of their age (eg, 20 years old).

The student will have a public method "ShowAge" which will write on the screen "My age is: 20 years old" (or the corresponding number).

You must create another test class called "StudentAndTeacherTest" that will contain "Main" and:
Create a Person and make it say hello
Create a student, set his age to 21, tell him to Greet and display his age
Create a teacher, 30 years old, ask him to say hello and then explain.


Source Code:


package December_19th;
public class Teacher extends Person
{
	private String subject;
	public final void Explain()
	{
		System.out.println("Explanation begins");
	}
}

package December_19th;
import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		boolean debug = false;

		//Create a Person and make it say hello
		Person myPerson = new Person();
		myPerson.Greet();

		/*Create a student, set his age to 21, 
		  tell him to Greet and display his age*/
		Student myStudent = new Student();
		myStudent.SetAge(21);
		myStudent.Greet();
		myStudent.ShowAge();

		/*Create a teacher, 30 years old, 
		 *ask him to say hello and then explain*/
		Teacher myTeacher = new Teacher();
		myTeacher.SetAge(30);
		myTeacher.Greet();
		myTeacher.Explain();

		if (debug)
		{
			new Scanner(System.in).nextLine();
		}
	}
}

package December_19th;
public class StudentextendsPerson
{
	public final void ShowAge()
	{
		System.out.printf("My age is: %1$s years old" + "\r\n", age);
	}
}

upackage December_19th;
private sing System;
public class Person
{
	protected int age;
	public final void Greet()
	{
		System.out.println("Hello");
	}
	public final void SetAge(int n)
	{
		age = n;
	}
}
Exercisey 6.1




Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.