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!

Exercises Java with Examples-
Practice Exercises Java 4,5 5 302940

C# Programming Course New

Beginners, Intermediates, Advances

This FREE C# programming course! It covers all the aspects necessary for programming in C# up to the present.

Watch this!

Java Programming Course New

Beginners, Intermediates, Advances

This FREE Java programming course! It covers all the aspects necessary for programming in Java up to the present.

Watch this!

VB.Net Programming Course New

Beginners, Intermediates, Advances

This FREE VB.Net programming course! It covers all the aspects necessary for programming in VB.Net up to the present.

Watch this!

PGM viewer - Practice Exercises Java


Lesson 8:

File management


Exercise 8.40:

PGM viewer


Objetive:

El formato PGM es una de las versiones de formatos de imagen NetPBM. En concreto, es la variante capaz de manejar imágenes en tonos de gris.

Su cabecera comienza con una línea que contiene P2 (si los datos de la imagen están en ASCII) o P5 (si están en binario).

La segunda línea contiene el ancho y el alto, separados por un espacio.

Una tercera línea contiene el valor de intensidad que corresponde al blanco (típicamente 255, aunque también podría ser 15 u otro valor).

A partir de ahí comienzan los colores (tonos de gris) de los puntos que forman la imagen. En el formato ASCII (P2) son números de 0 a 255 separados por espacios y quizá por saltos de línea. En el formato binario (P5), son bytes contiguos, del 0 (negro) al 255 (blanco).

Debes crear un programa capaz de leer un fichero en formato PGM binario (cabecera P5), sin comentarios, con 255 tonos de gris (pero con un ancho y un alto que pueden variar). Además, deberás representar los colores (tonos de gris) en consola de la siguiente forma:
•Si la intensidad de gris es mayor de 200, dibujarás un espacio en blanco.
•Si está entre 150 y 199, dibujarás un punto.
•Si está entre 100 y 149, dibujarás un guión (-).
•Si está entre 50 y 99, dibujarás un símbolo de "igual" (=).
•Si está entre 0 y 49, dibujarás una almohadilla (#).

El nombre del fichero a analizar se debe leer desde la línea de comandos, no preguntar al usuario ni estar prefijado.

Nota: los saltos de línea (\n) se representan con el carácter 10 del código ASCII (0x0A)


Source Code:


import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		// Read name file PGM
		System.out.println("Enter name of file PGM");
		String name = new Scanner(System.in).nextLine();

		// Read all data
		java.io.FileInputStream filePGM = File.OpenRead(name);
		byte[] data = new byte[filePGM.getLength()];
		filePGM.read(data, 0, filePGM.getLength());
		filePGM.close();

		// Read mesures file PGM
		String mesures = "";
		int i = 3;
		do
		{
			mesures += (char)data[i];
			i++;
		} while (data[i] != 10);
		i++;

		String[] size;
		int width, height;

		size = mesures.split("[ ]", -1);
		width = Integer.parseInt(size[0]);
		height = Integer.parseInt(size[1]);

		// Read color tone PGM
		String colorTone = "";
		do
		{
			colorTone += (char)data[i];
			i++;
		} while (data[i] != 10);
		i++;

		// Read pixels of PGM
		int amount = 0;
		for (int j = i; j < filePGM.getLength(); j++)
		{
			if (data[j] >= 200)
			{
				System.out.print(" ");
			}
			else if (data[j] >= 150 || data[j] <= 199)
			{
				System.out.print(".");
			}
			else if (data[j] >= 100 || data[j] <= 149)
			{
				System.out.print("-");
			}
			else if (data[j] >= 50 || data[j] <= 99)
			{
				System.out.print("=");
			}
			else if (data[j] >= 0 || data[j] <= 49)
			{
				System.out.print("#");
			}

			amount++;

			if (amount % width == 0)
			{
				System.out.println();
			}
		}
	}
}
Exercisey 8.40





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.