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!



Insects + persistence - Practice Exercises Java


Lesson 9:

Object persistence


Exercise 9.4:

Insects + persistence


Objetive:

Create a new version of the "insects" exercise (april 17th), which must save its data using persistence.


Source Code:


package Insects;

public class Ant extends NonFlyingInsect
{
	@Override
	public void ShowData()
	{
		System.out.println("I am an ant.");
	}
}

package Insects;
public class Bee extends FlyingInsect
{
	@Override
	public void ShowData()
	{
		System.out.println("I am a bee.");
	}
}

package Insects;

public class Fly extends FlyingInsect
{
	@Override
	public void ShowData()
	{
		System.out.println("I am a fly.");
	}
}

package Insects;

public class FlyingInsect extends Insect
{
	@Override
	public void ShowData()
	{
		System.out.println("I am a flying insect.");
	}
}

package Insects;
public class Insect implements Serializable
{
	public void ShowData()
	{
		System.out.println("I am an insect.");
	}

	public static void Guardar(String nombre, Insect insect)
	{
		IFormatter formatter = new BinaryFormatter();
		Stream stream = new java.io.FileOutputStream(nombre);
		formatter.Serialize(stream, insect);
		stream.Close();
	}

	public static Insect Cargar(String nombre)
	{
		Insect objeto;
		IFormatter formatter = new BinaryFormatter();
		Stream stream = new java.io.FileInputStream(nombre);
		objeto = (Insect)formatter.Deserialize(stream);
		stream.Close();
		return objeto;
	}
}

package Insects;
public class Mosquito extends FlyingInsect
{
	@Override
	public void ShowData()
	{
		System.out.println("I am a mosquito.");
	}
}

package Insects;
public class NonFlyingInsect extends Insect
{
	@Override
	public void ShowData()
	{
		System.out.println("I am a non flying insect.");
	}
}

package Insects;
import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		Random rand = new Random();
		int amount = 10;
		Insect[] data = new Insect[amount];
		for (int i = 0; i < amount - 1; i++)
		{
			switch (rand.nextInt(1, 5))
			{
				case 1:
					data[i] = new Fly();
					break;
				case 2:
					data[i] = new Mosquito();
					break;
				case 3:
					data[i] = new Bee();
					break;
				case 4:
					data[i] = new Ant();
					break;
				case 5:
					data[i] = new Spider();
					break;
			}
		}

		for (int i = 0; i < amount - 1; i++)
		{
			data[i].ShowData();
		}

	}
}

package Insects;
public class Spider extends NonFlyingInsect
{
	@Override
	public void ShowData()
	{
		System.out.println("I am a spider.");
	}
}
Exercisey 9.4




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.