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!



House - Practice Exercises Java


Lesson 7:

More on classes


Exercise 7.2:

House


Objetive:

Create a class "House", with an attribute "area", a constructor that sets its value and a method "ShowData" to display "I am a house, my area is 200 m2" (instead of 200, it will show the real surface). Include getters an setters for the area, too.

The "House" will contain a door. Each door will have an attribute "color" (a string), and a method "ShowData" wich will display "I am a door, my color is brown" (or whatever color it really is). Include a getter and a setter. Also, create a "GetDoor" in the house.

A "SmallApartment" is a subclass of House, with a preset area of 50 m2.

Also create a class Person, with a name (string). Each person will have a house. The method "ShowData" for a person will display his/her name, show the data of his/her house and the data of the door of that house.

Write a Main to create a SmallApartment, a person to live in it, and to show the data of the person.


Source Code:


package Houses;
import java.util.*;

public class House
{
	protected int area;
	protected Door door;

	public House(int area)
	{
		this.area = area;
		door = new Door();

	}
	public final int getArea()
	{
		return area;
	}
	public final void setArea(int value)
	{
		area = value;
	}
	public final Door getDoor()
	{
		return door;
	}
	public final void setDoor(Door value)
	{
		door = value;
	}

	public void ShowData()
	{
		System.out.printf("I am a house, my area is %1$s m2." + "\r\n", area);
	}
}

public class Door
{
	protected String color;

	public Door()
	{
		color = "Brown";
	}
	public Door(String color)
	{
		this.color = color;
	}

	public final String getColor()
	{
		return color;
	}
	public final void setColor(String value)
	{
		color = value;
	}

	public final void ShowData()
	{
		System.out.printf("I am a door, my color is %1$s." + "\r\n", color);
	}

}

public class SmallApartment extends House
{
	public SmallApartment()
	{
		super(50);

	}
	@Override
	public void ShowData()
	{
		System.out.println("I am an apartment, my area is " + area + " m2");
	}
}

public class Person
{
	protected String name;
	protected House house;

	public Person()
	{
		name = "Juan";
		house = new House(150);
	}
	public Person(String name, House house)
	{
		this.name = name;
		this.house = house;
	}

	public final String getName()
	{
		return name;
	}
	public final void setName(String value)
	{
		name = value;
	}
	public final House getHouse()
	{
		return house;
	}
	public final void setHouse(House value)
	{
		house = value;
	}

	public final void ShowData()
	{
		System.out.printf("My name is %1$s." + "\r\n", name);
		house.ShowData();
		house.getDoor().ShowData();
	}
}

public class Main
{
	public static void main(String[] args)
	{
		boolean debug = true;

		SmallApartment mySmallApartament = new SmallApartment();
		Person myPerson = new Person();

		myPerson.setName("Juan");
		myPerson.setHouse(mySmallApartament);
		myPerson.ShowData();

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




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.