Java Programming Learning For Daniel Liang ChapterOne

This is my answer of Java Programming 10th version written by Daniel Liang.

The Chapter one

  1. Display three messages
public class answer1_1 {
	public static void main(String [] args) {
		System.out.println("Welcome to Java");
		System.out.println("Welcome to Science");
		System.out.println("Programming is fun");
	}
}
  1. Display five messages
public class answer1_2 {
	public static void main(String[] args) {
		for (int i = 0; i < 5; i++) {
			System.out.println(i + 1 + " Welcome to Java");
		}
	}
}
  1. Display a pattern

public class answer1_3 {
	public static void main(String [] args) {
		System.out.println("    J     A     V     V    A");
		System.out.println("    J    A A     V   V    A A");
		System.out.println("J   J   AAAAA     V V    AAAAA ");
		System.out.println(" J J   A     A     V    A     A");
	}
}

  1. Print a table

public class answer1_4 {
	public static void main(String[] args) {
		System.out.println("a\ta^2\ta^3");
		int n = 5;
		for (int i = 1; i < n; i++) {
			System.out.println(i + "\t" + i * i + "\t" + i * i * i);
		}
	}

}

  1. Compute expressions

public class answer1_5 {
	public static void main(String [] args) {
		double member1, member2, member3;
		member1 = 9.5*4.5;
		member2 = 2.5*3;
		member3 = 45.5-3.5;
		System.out.println((member1-member2)/member3);
	}

}

  1. Summation of a series
public class answer1_6 {
	public static void main(String [] args) {
		int sum = 0;
		for (int i = 1; i< 10; i++) {
			sum+=i;
		}
		System.out.println("1+2+3+4+5+6+7+8+9="+sum);
	}
}
  1. Approximate π
import java.text.DecimalFormat;

public class answer1_7 {
	public static void main(String args[]) {
		test1();
		test2();
	}
	static void test1() {
		int coefficient = 4;
		double p = 0;
		for (int i = 1; i<6;i++){
			p = Math.pow(-1, i)*1.0/(2*i-1);
			p++;
		}
		p*=coefficient;
		DecimalFormat pai = new DecimalFormat("#.00");
		
		System.out.println("The pai is "+pai.format(p));
	}
	static void test2() {
		int coefficient = 4;
		double p = 0;
		for (int i = 1; i<6;i++){
			p = Math.pow(-1, i)*1.0/(2*i+1);
			p++;
		}
		p*=coefficient;
		DecimalFormat pai = new DecimalFormat("#.00");
		System.out.println("The pai is "+pai.format(p));
	}
}
  1. Area and perimeter of a circle
import java.text.DecimalFormat;

public class answer1_8 {
	public static void main(String[] args) {
		float radius = 5.5f;
		System.out.println("The area of a circle that has a radius of 5.5 is " + area(radius));
		System.out.println("The permimeter of a circle that has a radius of 5.5 is " + permimeter(radius));
	}

	static String permimeter(float radius) {
		DecimalFormat p = new DecimalFormat();
		double permimeter = 2 * radius * Math.PI;
		return p.format(permimeter);
	}

	static String area(float radius) {
		DecimalFormat a = new DecimalFormat();
		double area = Math.PI * radius * radius;
		return a.format(area);
	}
}
  1. Area and perimeter of a rectangle
import java.text.DecimalFormat;

public class answer1_9 {
	public static void main(String [] args) {
		float width = 4.5f;
		float height = 7.9f;
		System.out.println("The area of this rectangle is " + area(width, height));
		System.out.println("The permimeter of this rectangle is " + permimeter(width, height));
	}
	static String permimeter(float width, float height) {
		double perimeter = 2* (width + height);
		DecimalFormat p = new DecimalFormat();
		return p.format(perimeter);
	}
	static String area(float width, float height) {
		double area = width*height;
		DecimalFormat a = new DecimalFormat();
		return a.format(area);
	}
}
  1. Average speed in miles
import java.text.DecimalFormat;

public class answer1_10 {
	public static void main(String[] args) {
		float kilometers = 14f;
		float miles = kilometers / 1.6f;
		double hours = (45 * 60 + 30.0) / 3600;
		// System.out.println(hours);
		double speed = miles / hours;
		DecimalFormat s = new DecimalFormat();
		System.out.println("The average speed is " + s.format(speed) + " miles per hour;");
	}
}
  1. Population projection
public class answer1_11 {
	public static void main(String[] args) {
		double current = 312032486;
		double everyYSecond = 365 * 24 * 60 * 60;
		double increase = everyYSecond / 7;
		double decrease = everyYSecond / 13;
		double immigrantion = everyYSecond / 45;
		int n = 5;
		for (int i = 0; i < n; i++) {
			current = current + increase + immigrantion - decrease;
			System.out.println("The population in" + (i + 1) + " years is " + (long)current);
		}
	}
}
  1. Average speed in kilometers
import java.text.DecimalFormat;

public class answer1_12 {
	public static void main(String[] args) {
		double speed;
		double kilometer = 24 * 1.6;
		float time = (1 * 3600 + 40 * 60 + 35) / 3600;// Calculate the hours
		speed = kilometer / time;
		DecimalFormat speed1 = new DecimalFormat();  
		System.out.println("The average speed is " + speed1.format(speed) + " km/h");
	}
}
  1. Algebra: solve 2 x 2 linear equations
import java.util.Scanner;

public class answer1_13 {
	public static void main(String[] args) {
		double a = 3.4;
		double b = 50.2;
		double c = 2.1;
		double d = 0.55;
		double e = 44.5;
		double f = 5.9;
		double denominator = a * d - b * c;
		if (denominator != 0) {
			double x = (e * d - b * f) / denominator;
			double y = (a * f - e * c) / denominator;
			System.out.println("x is " + x + " and y is " + y);
		} else {
			System.out.println("The equation has no solution");
		}
	}
}

These are my answers, thank you for reading my blog.
As a new grad, not surprisingly I need your help. If you had some suggestions for me, please contact me.
Thanks.
By the way, this is my first blog.

發佈了28 篇原創文章 · 獲贊 34 · 訪問量 5362
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章