《java 语言程序设计》第2章编程练习

2.1

public class test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double f, c;
        c = input.nextDouble();
        f = (9.0/5)*c+32;
        System.out.println(f);
    }
}

2.2

public class test {
    public static void main(String[] args) {
        double r, h;
        final double PI = 3.1415925;
        System.out.println("Enter the radius and length of a cylinder: ");
        Scanner input = new Scanner(System.in);
        r = input.nextDouble();
        h = input.nextDouble();
        System.out.println("The area is " + PI*r*r);
        System.out.println("The volume is " + PI*r*r*h);
    }
}

2.3

public class test {
    public static void main(String[] args) {
        double f, m;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a value for feet: ");
        f = input.nextDouble();
        System.out.println(f + " feet is " + 0.305 *f + " meters");
    }
}

2.4

public class test {
    public static void main(String[] args) {
        double p, k;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number in pounds: ");
        p = input.nextDouble();
        System.out.println(p + " pounds is " + 0.454 * p + " kilograms");
    }
}

2.6

public class test {
    public static void main(String[] args) {
        int n, sum, t;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number between 0 and 1000: ");
        n = input.nextInt();
        sum = 0;
        t = n % 10;
        while(t != 0) {
            sum += t;
            n /= 10;
            t = n % 10;
        }
        System.out.println("The sum of the digits is " + sum);
    }
}

2.7

public class test {
    public static void main(String[] args) {
        int m = 0;
        int years, days, t;
        System.out.println("Enter the number of minutes: ");
        Scanner input = new Scanner(System.in);
        m = input.nextInt();
        t = (m / 60) / 24;
        years = t / 365;
        days = t % 365;
        System.out.println(m + " minutes is approximately " + years + " years and " + days + "days.");
    }
}

2.8

public class test {
    public static void main(String[] args) {
        int n;
        char c;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an ASCII code: ");
        n = input.nextInt();
        c = (char)n;
        System.out.println("The character for ASCII code " + n + " is " + c);

    }
}

2.11

public class test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter employee‘s name: ");
        String name = input.next();
        System.out.println("Enter number of hours worked in a week: ");
        float hours = input.nextFloat();
        System.out.println("Enter hourly pay rate: ");
        float payRate = input.nextFloat();
        System.out.println("Enter federal tax withholding rate: ");
        float ftwr = input.nextFloat();
        System.out.println("Enter state tax withholding rate: ");
        float stwr = input.nextFloat();
        System.out.println("Employee Name " + name);
        System.out.println("Hours Worked " + hours);
        System.out.println("Pay Rate: $" + payRate);
        System.out.println("Gross Pay: $" + hours * payRate);
        System.out.println("Deductions:");
        System.out.println("  Federal Withholding (" + ftwr * 100 +"%): $" + payRate * ftwr);
        System.out.println("  State Withholding (" + stwr * 100 +"%): $" + payRate * stwr);
        System.out.println("  Total Deduction: $" +  payRate * (ftwr + stwr);
    }
}

2.12

public class test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter balance and interest rate (e.g., 3 for 3%): ");
        double balance = input.nextDouble();
        double rate = input.nextDouble();
        System.out.printf("The interest is %.4f", balance * (rate / 1200));
    }
}

2.13

public class test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        //System.out.println("Enter balance and interest rate (e.g., 3 for 3%): ");
        System.out.print("Enter investment amount: ");
        double investmount = input.nextDouble();
        System.out.print("Enter monthly interest rate: ");
        double rate = input.nextDouble();
        System.out.print("Enter number of years: ");
        int year = input.nextInt();
        double s = investmount * Math.pow((1 + rate / 100), (year * 12));
        System.out.println("Accumulated value is " + s);
    }
}

2.14

public class test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter weigth in pounds: ");
        float weigth = input.nextFloat();
        System.out.print("Enter heigth in inches: ");
        float height = input.nextFloat();
        System.out.println("BMI is " + 0.45359237 * weigth / Math.pow(height * 0.0254, 2));
    }
}

2.15

public class test {
    public static void main(String[] args) {
        double t, s;
        s = t = 0;
        Scanner input = new Scanner(System.in);
        for(int i = 0; i < 6; i++) {
            s = (100 + t) * (1 + 0.00417);
            t = s;
        }
        System.out.println("After six months, result is: " + s);
    }
}

2.16

public class test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the amount of water in kilogram: " );
        double m = input.nextDouble();
        System.out.print("Enter the initial temperature: " );
        double it = input.nextDouble();
        System.out.print("Enter the final temperature: " );
        double ft = input.nextDouble();
        System.out.println("The energy needed is " + m * (ft - it) * 4184);
    }
}

2.17

public class test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the temperature in Fahrenheit: " );
        double f = input.nextDouble();
        System.out.print("Enter the wind miles per hour: ");
        double speed = input.nextDouble();
        System.out.println("The wind chill index is " + (35.74 + 0.6215 * f - 35.75 * Math.pow(speed, 0.16) + 0.427 * f * Math.pow(speed, 0.16)));
    }
}

2.18

public class test {
    public static void print() {
        System.out.print("     ");
    }
    public static void main(String[] args) {
        System.out.println("a     b     pow(a, b)");
        for(int i = 1; i < 6; i++) {
            System.out.print(i);
            print();
            System.out.print(i + 1);
            print();
            System.out.println((int)Math.pow(i, i +1));
        }
    }
}
时间: 2024-10-15 09:48:51

《java 语言程序设计》第2章编程练习的相关文章

Java语言程序设计 上机实验5 异常

Java语言程序设计 上机实验5 实验目的: 理解异常的概念,掌握Java的异常处理机制.常见异常的捕获方法.自定义异常及其抛出和捕获方法. 实验内容: 常见异常的捕获: 编写一个程序,分别生成ArrayIndexOutOfBoundsException类型和NumberFormatException类型的异常:然后改写程序,对这两种类型的异常进行捕获并合理地处理. 另外,思考并回答问题:能否0同时抛出ArrayIndexOutOfBoundsException类型和NumberFormatEx

Java语言程序设计期中考试试题

Java语言程序设计期中考试试题 一.编程题 1. 抽象类 创建闭合图形抽象类(ClosedFigure),定义抽象方法计算面积:创建继承平面图形抽象类的两个子类椭圆(Ellipse)和矩形(Rectangle),在子类中实现计算图形面积的方法.设计测试类Test1进行相应的测试,根据不同类型的平面图形进行相应的面积计算.(30分) 提示:根据椭圆第一定义,用a表示椭圆长半轴的长,b表示椭圆短半轴的长,且a>b>0, 椭圆面积公式:L=πab 2.接口 创建接口Speakable和Runer,

《Java语言程序设计》大作业报告 九宫格游戏

    <Java语言程序设计>大作业报告     中国石油大学(北京)2015 - 2016 学年第二学期     班级:_____计算机14-1_______ 姓名:_____  许 恺_________________ 学号:______2014011329___________     题意分析 程序首先需要九个可以移动的格子,大小相等,有字符串标示,其次要可以相应鼠标和键盘方向键的控制,可以自由移动,并且与此同时记录步数,最后在满足条件时弹出对话框并显示步数以及是否打破记录,关于打破

Java语言程序设计 上机实验4 掌握声明接口、一个类实现接口的声明和使用方法

Java语言程序设计 上机实验4 实验目的: 理解接口的作用,理解接口和实现接口的类的关系,掌握声明接口.一个类实现接口的声明和使用方法:熟悉Java语言包和实用包中的常用类. 实验内容:(*)为选做 声明圆柱体类Cylinder,继承椭圆类Ellipse(实现周长Perimeter接口和面积Area接口),为圆柱体类设计较为全面的构造方法,并实现体积Volume接口,计算表面积和体积. 将Java的若干关键字(15个以上)保存在一个字符串数组中,对其按升序排列,再采用顺序查找和二分法查找,判断

java语言程序设计(一)-1

java 语言的特点是: 强类型,制定了比较多的语言规范,尽可能在编译阶段检测出更多的错误及警告. 编译和解释,首先将源代码编译成codebyte,运行时,java的运行系统装载和链接需要执行的类,并做必要的优化后,解释执行字节码程序. 自动无用内存回收功能,系统有个垃圾收集器,自动收集程序不再使用的内存. 面向对象,采用面向对象的思想方法.主要有封装,继承,多态等特点. 与平台无关,编译一次,可以跨平台运行,前提是平台上安装java解释器. 安全性,不允许使用指针,进行数组下标越界检测,内存自

java学习之第五章编程题示例(初学篇)

1 /* 2 Animal.java 3 */ 4 package animal; 5 6 public abstract class Animal { 7 public abstract void cry(); 8 public abstract String getanimalName(); 9 } 1 //Dog.java 2 package animal; 3 4 public class Dog extends Animal 5 { 6 7 String aa="旺旺"; 8

java语言程序设计(一)-2

(一)jdk安装及环境变量配置 1.jdk下载 下载地址http://www.oracle.com/technetwork/java/javase/downloads/index.html,下载SE标准企业版本,当前已经更新到1.8.X版本了,但是最新版本不支持XP系统,已被验证(无法在xp系统中安装),于是选择1.7版本. 2.环境变量配置 配置path和classpath路径,我的电脑->属性->高级->环境变量,在用户变量中添加java的安装路径,在path中添加bin路径,在cl

Java面向对象程序设计第14章3-8和第15章6

Java面向对象程序设计第14章3-8和第15章6 3.完成下面方法中的代码,要求建立一个缓冲区,将字节输入流中的内容转为字符串. import java.io.*; public class test { static String loadStream(InputStream in) throws IOException { StringBuffer buffer = new StringBuffer(); int count= 1,i=6; byte[] buf = new byte[co

JAVA语言程序设计(一)(1)

第一章 Java概述 1.JAVA语言特点: 1).语法简单,功能强大,安全可靠 2).与平台无关  java虚拟机(Java virtual machine,JVM)是实现这一特点的关键 3).解释编译两种运行方式 4).多线程 5).动态执行兼有丰富的API文档及类库 2.JDK(Java SE Development kit,Java语言软件开发工具包) 3.\bin目录中包含Java的开发工具,包括Java编译器javac.exe.Java解释器java.exe:\lib目录下是java

全国计算机等级考试二级Java语言程序设计考试大纲(2013年版)

转自: 全国计算机等级考试,计算机二级,免费培训,免费视频教程,永久免费,免费培训IT  基本要求 1.掌握Java语言的特点.实现机制和体系结构. 2.掌握Java语言中面向对象的特性. 3.掌握Java语言提供的数据类型和结构. 4.掌握Java语言编程的基本技术. 5.会编写Java用户界面程序. 6.会编写Java简单应用程序. 7.会编写Java小应用程序(Applet). 8.了解Java语言的广泛应用.    考试内容  一.Java语言的特点和实现机制 二.Java体系结构 1.