java 课程作业2

课本例题

chapter1

1.1 Welcom

public class Welcom {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        System.out.println("Welcome to java!");
	}

}

1.2 WelcomWithThreeMessages


public class WelcomWithThreeMessages {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        System.out.println("Programming is fun!");
        System.out.println("Fundamentals First");
        System.out.println("Problem Driven");
	}

}

1.3 Test

​

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        System.out.println("3.5 * 4 / 2 - 2.5 is ");
        System.out.println(3.5 * 4 / 2 - 2.5);
        
	}

}

​

1.4 ComputeExpression

​

public class ComputeExpression {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        System.out.print("(10.5 + 2 * 3) / (45 - 3.5) = ");
        System.out.println((10.5 + 2 * 3) / (45 - 3.5));
    }

}

​

1.5 ShoeRumtimeError


public class ShoeRumtimeError {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        System.out.println(1 / 1);//1 / 0 is wrong
	}

}

1.6 ShowLogicError


public class ShowLogicError {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        System.out.print("Celsius 35 is Fahrenheit degree ");
        System.out.println((9 / 5.0) * 35 + 32);
	}

}

chapter2

2.1 ComputerArea


public class ComputerArea {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        double radius;
        double area;
        radius = 20;
        area = radius * radius * 3.14159;
        System.out.println("The area for the circle of radius " + 
            radius + " is " + area);
	}

}

2.2 ComputerAreaWithConSoleInput

import java.util.Scanner;//Scanner is in the java.util.package
public class ComputerAreaWithConSoleInput {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner input = new Scanner (System.in);
        System.out.print("Enter a number for radius: ");
        double radius = input.nextDouble();
        double area = radius * radius * 3.14159;
        System.out.println("The area for the circle of radius " +
            radius + " is " + area);
	}

}

2.3 ComputeAverage

import java.util.Scanner;
public class ComputeAverage {

	public static void main(String[] args) {
   		// TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print("Enter three numbers: ");
        double number1 = input.nextDouble();
        double number2 = input.nextDouble();
        double number3 = input.nextDouble();
        double average = (number1 + number2 + number3) / 3;
        System.out.println("The average of " + number1 + " " + number2
            + " " + number3 + " is " + average);
	}

}

2.4 ComputeAreaWithConstant

import java.util.Scanner; 
public class ComputeAreaWithConstant {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        final double PI = 3.14159;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a number for radius ");
        double radius = input.nextDouble();
        double area = radius * radius * PI;
        System.out.println("The area of the cirle of radius " + radius +
            " is " + area);
	}

}

2.5 DisplyTime

import java.util.Scanner;
public class DisplyTime {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner input  = new Scanner(System.in);
        System.out.print("Enter a integer for seconds: ");
        int seconds = input.nextInt();
        int minutes = seconds / 60;
        int remainingSeconds = seconds % 60;
        System.out.println(seconds + " seconds is " + minutes +
        	" minutes and " + remainingSeconds + " seconds");
        
	}

}

 2.6  FahrenheitToCelsius

import java.util.Scanner;
public class FahrenheitToCelsius {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a degree in Fahrenhiet: ");
        double Fahrenhiet = input.nextDouble();
        double celsius = (5.0 / 9) * (Fahrenhiet - 32 );
        System.out.println("Fahrenhiet " + Fahrenhiet + " is " + celsius + "in celsius");
        
	}

}

2.7 ShowCurrentTime


public class ShowCurrentTime {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        long totalMilliseconds = System.currentTimeMillis();
        long totalSeconds = totalMilliseconds / 1000;
        long currentSecond = totalSeconds % 60;
        long totalMinutes = totalSeconds / 60;
        long currentMinute = totalMinutes % 60;
        long totalHours = totalMinutes / 60;
        long currentHour = totalHours % 24;
        System.out.println("Current time is " + currentHour + ":" 
        	+ currentMinute + ":" + currentSecond + " GMT");		
	}

}

2.8 SzleTax

import java.util.Scanner;
public class SzleTax {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("Enter purchase amount: ");
        double purchaseAmount = input.nextDouble();
        double tax = purchaseAmount * 0.06;
        System.out.println("Sales tax is $" + (int)(tax * 100) / 100.0);
	}

}

2.9 ComputeLoan

package hello;
import java.util.Scanner;

public class ComputeLoan {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print("Enter annul interest rate, e.g., 7.25: ");
        double annulInterestRate = input.nextDouble();
        double monthlyInterestRate = annulInterestRate / 1200;
        System.out.print("Enter number of years as an interge, e.d., 5: ");
        int numberOfYears = input.nextInt();
        System.out.print("Enter loan amount, e.g., 120000.95: ");
        double loanAmount = input.nextDouble();
        double monthlyPayment = loanAmount * monthlyInterestRate / (1 
        	- 1 / Math.pow(1 + monthlyInterestRate,  numberOfYears * 12));
        double totalPayment = monthlyPayment * numberOfYears * 12;
        System.out.println("The monthly payment is $" + (int)(monthlyPayment * 100) / 100.0);
        System.out.println("The total payment is $" + (int )(totalPayment * 100) / 100.0);
	}

}

2.10 ComputeChange

package hello;
import java.util.Scanner;
public class ComputeChange {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an amount in double, for example 11.56: ");
        double amount = input.nextDouble();
        int remainingAmount = (int)(amount * 100);
        int numberOfOneDollars = remainingAmount / 100;
        remainingAmount = remainingAmount % 100;
        int numberOfQuarters = (int)remainingAmount / 25;
        remainingAmount = remainingAmount % 25;
        int numberOfDimes = (int)remainingAmount / 10;
        remainingAmount = remainingAmount % 10;
        int numberOfNickels = (int)remainingAmount / 5;
        remainingAmount = remainingAmount % 5;
        int numberOfPennies = remainingAmount;
        System.out.println("Your amount " + amount + " consists of");
        System.out.println(" " + numberOfOneDollars + " dollars");
        System.out.println(" " + numberOfQuarters + " dollars");
        System.out.println(" " + numberOfDimes + " dollars");
        System.out.println(" " + numberOfNickels + " dollars");
        System.out.println(" " + numberOfPennies + " dollars");
        
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用来完成大作业的。文档内容: 1 Java技术体系 1.1 Java语言 1.2 Java平台 1.3 Java应用领域 2 Java语言的技术特点 2.1 1 2.2 2 2.3 3 3 Java语言与C++的异同分析总结。 4 选用C和java语言时编程算法程序有什么不同,有什么优势和劣势。 5 自己编程学习的级别和状态。以及自己以后的编程学习的计划和想法。 6 下面3道题目中选一道,给出算法分析和程序。 1)“黄金分割数”在我们的生活中很常见,但是在不同的应用领域,要求的精度也不一样。 例如:三位小数是0.618 现在我们需要你能求出保留100位小数的黄金分割数,采用的算法为“分层计算法”: 黄金数= 1 --------------- 1+ 1 ------------- 1+ 1 ----------- 1+ 1 --------- ..... 注意,计算出的结果,如果第100位为0也需要保留。 2)已知一个数列: 5,2,4,3,7,6 那么,在这个数列中存在这样一些“连续数”,例如:5,2,4,3这个子数列排序后是连续的。同样2,4,3也是连续的,为了方便表示 我们使用下标来标识,这样,这个数列中存在以下“连续数”: [1,1] [1,4] [1,6] [2,2] [2,4] [3,3] [3,4] [4,4] [5,5] [5,6] [6,6] 这样,他就存在11个“连续数”。现在需要你在用户找出一个数组中所有的“连续数”。 要求: 1、用户输入一个整数N,表示下面数组的个数 2、用户每输入一行作为一个数组 如: 用户输入: 1 5,2,4,3,7,6 程序输出: 11 3)有一种数我们称之为幸运数,它的特点是这样的,首先,由自然数按顺序排列: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 … 这样,1比较特殊, 1为第一个幸运数,那么,我们移除掉序号能被2整除的数(注意:是序号,而不是数本身,每次移除后都重新排序)就剩下: 1 3 5 7 9 11 13 15 17 19… 3为第二个幸运数,那么我们需要去掉序号能被3(下一次是除4,然后是5,每次加1)整除的数,5 11 17...剩下: 1 3 7 9 13 15 19… 那么7为第三个幸运数,后面的幸运数,依此类推,移除之后剩下的数字都是幸运数。 现在我们需要你求出给定的m和n之间的幸运数的个数: 例如:给定1 20,那么个数为:5(5个幸运数分别是1,3,7,13,19) 现在要求用户输入两个数m和n(m<n<=1000*1000),输出幸运数的个数。 例如: 用户输入: 1 20 程序输出: 5 格式:小四,1.5倍行距
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值