Java 2 Practical Course (Fifth Edition) Exercises after Class - [Programming Question Answers]

Chapter II

1. Write an application program to give the position of the Chinese characters "you", "I" and "he" in the Unicode table.

/*
 * To observe the order of a character in the Unicode table, you can use int type conversion
 */
public class Test {

	public static void main(String[] args) {

		System.out.println((int)'you');
		System.out.println((int)'I');
		System.out.println((int)'he');
	}

}

2. Write a java application and output all Greek letters.

/*
 * To observe the characters in the Unicode table represented by the numbers between 0 and 65535, char type conversion must be used.
 */
public class Test {

	public static void main(String[] args) {

		 char cStart='α', cEnd='ω';
		 for(char c = cStart; c <= cEnd; c ++) {
			 System.out.print(c+" ");
		 }
	}

}

Chapter III

1. Write the application program for 1! + 2! +... + 10!

public class Test {

	public static void main(String[] args) {
		int sum = 0;

		for(int i = 1; i <= 10; i ++) {
			int f = 1;
			for(int j = 1; j <= i; j ++) {
				f *= j;
			}
			sum += f;
		}
		System.out.println(sum);
	}

}

2. Write an application program to find all prime numbers within 100.

public class Test {

	public static void main(String[] args) {
		
		for(int i = 2; i <= 100; i ++) {
			int flag = 0;
			for(int j = 2; j*j <= i; j ++) {
				if(i % j == 0) {
					flag = 1;
					break;
				}
			}
			if(flag == 0) {
				System.out.print(i+" ");
			}
		}
	}
}

3. Calculate 1+1/2 with do-while and for loops, respectively. + One third! + 1/4! +... The first 20 sums.

public class Test{
	public static void main(String[] args) {
		double sum = 0.0;
		for(int i = 1; i <= 20; i ++) {
			int f = 1;
			for(int j = 1; j <= i; j ++) {
				f *= j;
			}
			sum += 1.0/f;
		}
		System.out.println(sum);
	}
}

4. If a number is exactly equal to the sum of its factors, it becomes a perfect number. Write an application to find all completions within 1000.

public class Test {

	public static void main(String[] args) {

		for(int i = 1; i <= 1000; i ++) {
			int sum = 0;
			for(int j = 1; j < i; j ++) {
				if(i % j == 0) {
					sum += j;
				}
			}
			if(sum == i) {
				System.out.print(i+" ");
			}
		}
	}
}

5. Write the application program and use for loop statement to calculate 8+88+888+. The sum of the first 10 items.

public class Test {

	public static void main(String[] args) {

		long sum = 0;
		long ans = 0;
		for(int i = 1; i <= 10; i ++) {
			ans = ans*10 + 8;
			sum += ans;
		}
		System.out.println(sum);
	}
}

6. Write the application program, the output satisfies 1+2+3+. + n < 8888, the largest positive integer.  

public class Test {

	public static void main(String[] args) {

		int sum = 0;
		int i = 0;
		while(sum < 8888) {
			i ++;
			sum += i;
		}
		System.out.println(i-1);
	}
}

Chapter IV

Classes are used to describe the speed of CPU and the capacity of hard disk in a computer.

class PC {
	CPU cpu;
	HardDisk HD;
	
	public void setCpu(CPU cpu) {
		this.cpu = cpu;
	}
	
	public void setHD(HardDisk hD) {
		HD = hD;
	}
	
	public void show() {
		System.out.println("CPU Speed:"+cpu.speed+"   The capacity of hard disk:"+HD.amount);
	}
	
}
class CPU{
	
	int speed;
	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}
	
}

class HardDisk{
	
	int amount;
	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}
}

public class Test {

	public static void main(String[] args) {
		CPU cpu = new CPU();
		cpu.setSpeed(2200);
		HardDisk disk = new HardDisk();
		disk.setAmount(200);
		PC pc = new PC();
		pc.setCpu(cpu);
		pc.setHD(disk);
		pc.show();
	}
}

Chapter V

Designing an animal sound simulator, hoping that the simulator can simulate the sound of many animals:

/*
 * Abstract animal class and two abstract methods
 */
abstract class Animal{
	abstract void  cry();
	abstract String getAnimalName();
	
}
/*
 * Analog class
 */
class Simulator{
	
	Animal animal;
	public void playSound(Animal animal) {
		this.animal = animal;
		
	}
	public void show() {
		System.out.println(animal.getAnimalName());
		animal.cry();
	}
}

class Dog extends Animal{
	@Override
	void cry() {
		System.out.println("A dog's cry is a whine.~~~");
	}

	@Override
	String getAnimalName() {
		return "Doggy";
	}
}
class Cat extends Animal{

	@Override
	void cry() {
		System.out.println("The crying of a cat is meowing.~~~");
	}

	@Override
	String getAnimalName() {
		return "Kitty";
	}
	
}
public class Test02 {

	public static void main(String[] args) {

		Simulator simulator = new Simulator();
		simulator.playSound(new Dog());
		simulator.show();
		simulator.playSound(new Cat());
		simulator.show();
	}

}

 

Keywords: simulator Java

Added by shazly on Wed, 31 Jul 2019 04:57:52 +0300