Java程序基础_第5-10讲 第3章 结构化编程
- 格式:ppt
- 大小:1.63 MB
- 文档页数:29
Java语言是面向对象技术成功应用的典范。
诞生于1955年的Java语言在短短的几年间便席卷全球,以20世纪末网络科技和网络经济所特有的令人瞠目结舌的速度迅速发展,是21世纪开发信息系统的主流技术。
由于Java语言所具有的简洁性、纯属面向对象等特征,也使得它非常适合于在大学中面向对象程序设计的教学。
本学期我们开设了《Java语言与面向对象程序设计》这门课,通过对Java 一段时间的自学,我想在此谈谈我自己摸索学习Java的心得体会。
学习Java切忌浮躁,要注意以下几点:1.欲速则不达,初学者请不要被新技术迷惑,先把基础学扎实。
2.要扎扎实实,一步一个脚印的逐步学习,不要想着一步登天。
给自己定一个学习流程,按照流程学习。
3.多查API,它是Java编程的基本方法,也是编程过程中所不断利用的资源。
Java的学习过程不仅仅是基本语法的学习,更多的是去学习和掌握它所提供的API类库。
对于所接触到的类,方法,都去仔细去阅读文档的说明,再用自己编写的实例去比较一下。
4.看再多的书是学不全脚本的,要多实践,学习脚本最好的方法之一就是多练习;不要只看不练,一定要把书上的例子亲手敲到电脑上实践,千万不要照抄,更不要复制粘贴;看得懂代码,不代表会写代码。
学编程语言不仅仅是从理论上的学习,更重要的是要利用这门语言为你的思想服务。
理解这门语言是首要的,但是要达到心领神会、融会贯通就必须勤动手,多去时间,多编一些例子。
计算机科学是注重实践的学科,成功的软件开发人员无不经过大量的上机锻炼,只有理论和实践相结合才能真正掌握只是和技能。
5.看得懂的书,请仔细看;看不懂的书,请硬着头皮看;书读百遍,其义自现。
第一遍书看不懂就看第二遍、第三遍。
6.当你用脚本到一半却发现自己用的方法很拙劣时,请不要马上停手;请尽快将余下的部分粗略的完成以保证这个代码的完整性,然后分析自己的错误并重新编写和工作。
7.遇到问题,先自己想办法解决,查查文档,不要什么都不看就发帖子问。
结构化编程的三种基本结构
结构化编程是一种程序设计风格,强调程序的结构清晰、可读性强、易于维护。
它采用三种基本结构,分别是顺序结构、选择结构和循环结构。
顺序结构是程序中最基本的结构,它表示指令的执行顺序是从上往下依次执行。
在顺序结构中,每一条指令都必须按照特定的顺序依次执行,否则程序会出现错误。
选择结构是根据条件判断来选择执行哪一段程序。
常见的选择结构有if语句和switch语句。
if语句根据条件判断执行哪一段程序,而switch语句根据变量值执行哪一段程序。
循环结构是在满足一定条件下重复执行一段程序。
循环结构可以使程序更加灵活,让程序能够处理大量的数据。
常见的循环结构有
for循环、while循环和do-while循环。
for循环适合用于已知循环次数的情况,while循环适合用于不确定循环次数的情况,而
do-while循环则保证循环至少执行一次。
结构化编程的三种基本结构为程序员提供了一种清晰、可读性强、易于维护的程序设计方法。
程序员可以根据不同的需求和情况选择适当的结构来编写程序。
- 1 -。
第3章对象和类在当今的计算机大型应用软件开发领域,面向对象技术正在逐步取代面向过程的程序设计技术。
本章将介绍面向对象的基本知识和Java实现面向对象程序设计的主要工具--类。
如果读者缺乏关于面向对象程序设计的背景,一定要仔细地阅读本章。
如果读者有C++编程经验,也要注意二者之间的区别,毕竟Java在类的具体实现上与C++有较大的差别。
3.1 面向对象的基本概念面向对象(Object Oriented,OO)是当前计算机界关心的重点,它是90年代软件开发方法的主流。
面向对象的概念和应用已超越了程序设计和软件开发,扩展到很广的范围。
例如,数据库系统、交互式界面、应用结构、应用平台、分布式系统、网络管理结构、CAD技术、人工智能等领域。
谈到面向对象,这方面的文章非常多。
但是,明确地给出"面向对象"的定义却非常少。
最初,"面向对象"是专指在程序设计中采用封装、继承、抽象等设计方法。
可是,这个定义显然不能再适合现在的情况。
面向对象的思想已经涉及到软件开发的各个方面。
例如,面向对象的分析(Object Oriented Analysis,OOA),面向对象的设计(Object Oriented Design,OOD)以及经常说的面向对象的编程(Object Oriented Programming,OOP)。
许多有关面向对象的文章,都只是讲述在面向对象的开发中所需要注意的问题,或所采用的比较好的设计方法。
看这些文章只有真正懂得什么是对象,什么是面向对象,才能最大程度地收获知识。
说明:在本章中,着重讨论OOP,有关OOA和OOD请读者查阅有关软件工程的书籍。
OOP从所处理的数据入手,以数据为中心而不是以服务(功能)为中心来描述系统。
它把编程问题视为一个数据集合,因为数据相对于功能而言,具有更强的稳定性。
OOP同结构化程序设计相比最大的区别就在于:前者首先关心的是所要处理的数据,而后者首先关心的是功能。
课程名称:java程序设计时间:2015-2016学年第二学期授课教师:****授课班级:151软件(30人)、151计网(25人)Java程序设计:第一章:Java语言概述2课时第二章:java语言基础4课时第三章流程控制10课时第四章数组4课时第五章字符串2课时第六章类及对象4课时第七章java语言类的特性12课时第八章继承、抽象类和接口22课时复习、机动4课时合计:64课时推荐用书(图):1、Java程序设计基础(第4版)实验指导邹林达陈国君主编清华大学出版社ISBN 978—7—302—35279—22、Java程序设计基础(第4版)陈国君主编清华大学出版ISBN 978—7—302-33142-1Java程序设计授课计划:第1周第一章:Java语言概述2课时2课时第1周第二章:java语言基础—数据类型2课时第2周第二章:java语言基础—运算符及表达式2课时4课时第2周第三章流程控制—选择结构2课时第3周第三章流程控制-选择结构之多分支2课时第3周第三章流程控制-while循环嵌套2课时第4周第三章流程控制-for循环嵌套2课时第4周第三章流程控制-循环嵌套2课时10课时第5周第四章数组-一维数组2课时第一章:Java语言概述(2课时)教学目标:●课程所讲述的内容●Java语言的由来●Java语言的版本说明●Java语言的跨平台●Java开发环境配置教学重难点:●配置开发环境➢什么是Java语言:java语言的发展及开发环境➢Java语言的基本结构:java语言的基本结构➢Java语言的基本要素:java基本语法/类及对象/继承及接口✓Java语言高级知识:➢异常、IO、多线程➢Java异常处理及捕获机制➢Java输入及输出流➢Java多线程➢Java图形用户组建Swing类2.计算机语言发展的四个阶段1)机器语言2)汇编语言3)高级语言4)结构化程序设计语言3.java发展背景1)sun公司在很多领域有前瞻性,提前预判到一些“科技”及人类生活发展的要素。
结构化编程教案教案标题:结构化编程教案教学目标:1. 了解结构化编程的概念和原则;2. 掌握结构化编程的基本概念和技巧;3. 能够运用结构化编程的思维方式设计和编写简单的程序;4. 培养学生的逻辑思维和问题解决能力。
教学准备:1. 计算机和投影仪;2. 结构化编程相关的教材和参考资料;3. 编程软件(如Python、C等);4. 练习题和实例。
教学过程:一、导入(5分钟)1. 向学生介绍结构化编程的概念,解释其重要性和应用领域;2. 引导学生思考结构化编程与其他编程方法的区别和优势。
二、理论讲解(15分钟)1. 解释结构化编程的基本原则,如顺序、选择和循环结构;2. 介绍结构化编程的基本概念,如顺序结构、分支结构和循环结构;3. 给出实例,解释如何使用结构化编程思维解决问题。
三、示范演示(20分钟)1. 通过编程软件展示结构化编程的实际应用;2. 按照结构化编程的原则,演示如何设计和编写简单的程序;3. 解释每个步骤的目的和意义,引导学生理解结构化编程的思维方式。
四、练习与实践(30分钟)1. 分发练习题,让学生独立或小组完成编程任务;2. 引导学生运用结构化编程的思维方式解决问题;3. 鼓励学生在实践中发现问题并尝试解决,培养问题解决能力。
五、讨论与总结(10分钟)1. 让学生分享他们的编程经验和遇到的问题;2. 引导学生总结结构化编程的优势和局限性;3. 结合实例,再次强调结构化编程的重要性和应用价值。
六、作业布置(5分钟)1. 布置结构化编程的相关作业,要求学生进一步巩固所学知识;2. 提供参考资料和练习题,鼓励学生主动学习和探索。
教学评估:1. 在练习与实践环节观察学生的编程过程和结果;2. 针对学生的作业和讨论表现进行评价;3. 分析学生对结构化编程概念和技巧的掌握程度。
教学延伸:1. 鼓励学生参与编程竞赛或项目实践,进一步提升结构化编程能力;2. 推荐相关的编程书籍和网上资源,供学生深入学习和研究。
第3章面向对象程序设计基础【1】什么是Java程序使用的类?什么是类库?[解答]:Java程序的基本单位是类。
对象是对事物的抽象,而类是对对象的抽象和归纳,找出事物的共性,把具有共同性质的事物归结为一类,得出一个抽象的概念——类。
类是具有相同属性和方法的一组对象的集合,类是在对象之上的抽象,对象则是类的具体化,一旦建立类之后,就可用它来建立许多你需要的对象。
Java的类库是系统提供的已实现的标准类的集合,是Java编程的API(Application Program Interface),它可以帮助开发者方便、快捷地开发Java程序。
【2】如何定义方法?在面向对象程序设计中方法有什么作用?[解答]:方法的定义由两部分组成:方法声明和方法体。
方法声明的基本格式如下:返回类型方法名(形式参数){… //方法体内容}方法声明包括方法名、返回类型和形式参数,方法的小括号是方法的标志;方法体为实现方法行为的Java语句。
在面向对象程序设计中,方法所起的作用是完成对类和对象属性操作。
【3】简述构造方法的功能和特点。
下面的程序片段是某学生为student类编写的构造方法,请指出其中的错误。
void Student(int no,String name){studentNo=no;studentName=name;return no;}[解答]:构造方法是一个特殊的方法,主要用于初始化新创建的对象。
构造方法的方法名要求与类名相同,用户不能直接调用,只能通过new运算符调用,而且构造方法是不返回任何数据类型,甚至也不返回void数据类型,即不能在构造方法前加void。
以上的代码段出错于:①构造方法Student()前不能加void ②不能用return语句【4】定义一个表示学生的student类,包括的域有学号、姓名、性别、年龄,包括的方法有获得学号、姓名、性别、年龄及修改年龄。
编写Java程序创建student类的对象及测试其方法的功能。
《Java语言程序设计(基础篇)》(第10版梁勇著)第三章练习题答案《Java语言程序设计(基础篇)》(第10版梁勇著)第三章练习题答案3.1public class Exercise03_01 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter a, b, c: ");double a = input.nextDouble();double b = input.nextDouble();double c = input.nextDouble();double discriminant = b * b - 4 * a * c;if (discriminant < 0) {System.out.println("The equation has no real roots");}else if (discriminant == 0) {double r1 = -b / (2 * a);System.out.println("The equation has one root " + r1);}else { // (discriminant > 0)double r1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);double r2 = (-b - Math.pow(discriminant, 0.5)) / (2 * a);System.out.println("The equation has two roots " + r1 + " and " + r2);}}}3.1附加public class Exercise03_01Extra {public static void main(String args[]) {Scanner input = new Scanner(System.in);System.out.print("Enter a numerator: ");int numerator = input.nextInt();System.out.print("Enter a denominator: ");int denominator = input.nextInt();if (numerator < denominator) {System.out.println(numerator + " / " + denominator + " is a proper fraction");}else if (numerator % denominator == 0) {System.out.print(numerator + " / " + denominator + " is an improper fraction ");System.out.println("and it can be reduced to " + numerator / denominator);}else {System.out.print(numerator + " / " + denominator + " is an improper fraction ");System.out.println("and its mixed fraction is " + numerator / denominator + " + " +numerator % denominator + " / " + denominator);}}}3.2public class Exercise03_02 {public static void main(String[] args) {Scanner input = new Scanner(System.in);int number1 = (int)(System.currentTimeMillis() % 10);int number2 = (int)(System.currentTimeMillis() * 7 % 10);int number3 = (int)(System.currentTimeMillis() * 3 % 10);System.out.print("What is " + number1 + " + " + number2 + " + " +number3 + "? ");int answer = input.nextInt();System.out.println(number1 + " + " + number2 + " + " + number3 +" = " + answer + " is " +(number1 + number2 + number3 == answer));}}3.2附加public class Exercise03_02Extra {public static void main(String args[]) {Scanner input = new Scanner(System.in);System.out.print("Enter the coordinates for two points: ");double x1 = input.nextDouble();double y1 = input.nextDouble();double x2 = input.nextDouble();double y2 = input.nextDouble();double m = (y2 - y1) / (x2 - x1);double b = y1 - m * x1;System.out.print("The line equation for two points (" + x1 + ", " + y1 + ") and (" + x2 + ", " + y2 + ") is " + "y = ");if (m == -1)System.out.print("-x");else if (m == 1)System.out.print("x");elseSystem.out.print(m + "x");if (b > 0)System.out.println(" + " + b);else if (b < 0)System.out.println(" - " + (-1 * b));else// b is 0System.out.println();}}3.3public class Exercise03_03 {public static void main(String[] args) {Scanner input = new Scanner(System.in); System.out.print("Enter a, b, c, d, e, f: ");double a = input.nextDouble();double b = input.nextDouble();double c = input.nextDouble();double d = input.nextDouble();double e = input.nextDouble();double f = input.nextDouble();double detA = a * d - b * c;if (detA == 0) {System.out.println("The equation has no solution"); }else {double x = (e * d - b * f) / detA;double y = (a * f- e * c) / detA;System.out.println("x is " + x + " and y is " + y);}}}3.3附加public class Exercise03_03Extra {public static void main(String[] args) {final double RADIUS = 5;double angle = Math.random() * 2 * Math.PI;double x = RADIUS * Math.random() * Math.cos(angle);double y = RADIUS * Math.sin(angle);double distance = Math.pow(x * x + y * y, 0.5);System.out.println("The point is (" + x + ", " + y + ") and its distance to the center is " + distance);}}3.4public class Exercise03_04 {public static void main(String[] args) {int number = (int)(Math.random() * 12) + 1;// or int number = (int)(System.currentTimeMillis() % 12 + 1);// or int number = (int)(Math.random() * 12) + 1;if (number == 1)System.out.println("Month is Januaray");else if (number == 2)System.out.println("Month is Feburary");else if (number == 3)System.out.println("Month is March");else if (number == 4)System.out.println("Month is April");else if (number == 5)System.out.println("Month is May");else if (number == 6)System.out.println("Month is June");else if (number == 7)System.out.println("Month is July");else if (number == 8)System.out.println("Month is August");else if (number == 9)System.out.println("Month is September");else if (number == 10)System.out.println("Month is October");else if (number == 11)System.out.println("Month is November");else// if (number == 12)System.out.println("Month is December");}}3.5public class Exercise03_05 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Prompt the user to enter an integer for todaySystem.out.print("Enter today抯 day: ");int today = input.nextInt();System.out.print("Enter the number of days elapsed since today: ");int elapsedDays = input.nextInt();String nameForToday;if (today == 0)nameForToday = "Sunday";else if (today == 1)nameForToday = "Monday";else if (today == 2)nameForToday = "Tuesday";else if (today == 3)nameForToday = "Wednesday";else if (today == 4)nameForToday = "Thursday";else if (today == 5)nameForToday = "Friday";else// if (today == 6)nameForToday = "Saturday";int futureDay = (today + elapsedDays) % 7; String nameForFutureDay;if (futureDay == 0)nameForFutureDay = "Sunday";else if (futureDay == 1) nameForFutureDay = "Monday";else if (futureDay == 2) nameForFutureDay = "Tuesday";else if (futureDay == 3) nameForFutureDay = "Wednesday";else if (futureDay == 4) nameForFutureDay = "Thursday";else if (futureDay == 5) nameForFutureDay = "Friday";else// if (futureDay == 6) nameForFutureDay = "Saturday";System.out.println("Today is " + nameForToday+ " and the future day is " + nameForFutureDay); } }3.6public class Exercise03_06 {public static void main(String[] args) {Scanner input = new Scanner(System.in);// Prompt the user to enter weight in pounds System.out.print("Enter weight in pounds: "); double weight = input.nextDouble();// Prompt the user to enter heightSystem.out.print("Enter feet: ");double feet = input.nextDouble();System.out.print("Enter inches: ");double inches = input.nextDouble();double height = feet * 12 + inches;// Compute BMIdouble bmi = weight * 0.45359237 / ((height * 0.0254) * (height * 0.0254));// Display resultSystem.out.println("BMI is " + bmi);if (bmi < 18.5)System.out.println("Underweight");else if (bmi < 25)System.out.println("Normal");else if (bmi < 30)System.out.println("Overweight");elseSystem.out.println("Obese");}}3.7/** Break down an amount into smaller units* Display the non-zero denominations only, and display singular* words for single units like 1 dollars, 1 penny, and display plural * words for more than one unit like 2 dollars, 3 pennies.*/public class Exercise03_07 {// Main methodpublic static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Receive the amount entered from the keyboardSystem.out.print("Enter an amount in double, for example 11.56: ");double amount = input.nextDouble();int remainingAmount = (int)(amount * 100);// Find the number of one dollarsint numberOfOneDollars = remainingAmount / 100;remainingAmount = remainingAmount % 100;// Find the number of quarters in the remaining amountint numberOfQuarters = remainingAmount / 25;remainingAmount = remainingAmount % 25;// Find the number of dimes in the remaining amountint numberOfDimes = remainingAmount / 10;remainingAmount = remainingAmount % 10;// Find the number of nickels in the remaining amountint numberOfNickels = remainingAmount / 5;remainingAmount = remainingAmount % 5;// Find the number of pennies in the remaining amountint numberOfPennies = remainingAmount;// Display resultsif (amount < 0) {System.out.println("Your amount is negative");System.exit(1);}else if (amount < 0) {System.out.println("Your amount is zero");System.exit(2);}System.out.println("Your amount " + amount + " consists of ");if (numberOfOneDollars > 1)System.out.println(numberOfOneDollars + "\ dollars");else if (numberOfOneDollars == 1)System.out.println(numberOfOneDollars + "\ dollar");if (numberOfQuarters > 1)System.out.println(numberOfQuarters + "\ quarters");else if (numberOfQuarters == 1)System.out.println(numberOfQuarters + "\ quarter");if (numberOfDimes > 1)System.out.println(numberOfDimes + "\ dimes");else if (numberOfDimes == 1)System.out.println(numberOfDimes + "\ dime");if (numberOfNickels > 1)System.out.println(numberOfNickels + "\ nickels");else if (numberOfNickels == 1)System.out.println(numberOfNickels + "\ nickel");if (numberOfPennies > 1)System.out.println(numberOfPennies + "\ pennies");else if (numberOfPennies == 1)System.out.println(numberOfPennies + "\ penny");}}3.8public class Exercise03_08 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in); // Enter three numbersSystem.out.print("Enter three integers: ");int number1 = input.nextInt();int number2 = input.nextInt();int number3 = input.nextInt();if (number1 > number2) {int temp = number1;number1 = number2;number2 = temp;}if (number2 > number3) {int temp = number2;number2 = number3;number3 = temp;}if (number1 > number2) {int temp = number1;number1 = number2;number2 = temp;}System.out.println("The sorted numbers are "+ number1 + " " + number2 + " " + number3);}}public class Exercise03_09 {public static void main(String[] args) {Scanner input = new Scanner(System.in);// Prompt the user to enter an integerSystem.out.print("Enter the first 9 digits of an ISBN as integer: ");int number = input.nextInt();// Calculate checksum (You may write a loop to simplify it in Ch4int checksum =((number / 100000000 % 10) * 1 +(number / 10000000 % 10) * 2 +(number / 1000000 % 10) * 3 +(number / 100000 % 10) * 4 +(number / 10000 % 10) * 5 +(number / 1000 % 10) * 6 +(number / 100 % 10) * 7 +(number / 10 % 10) * 8 +(number % 10) * 9) % 11;System.out.print("The ISBN-10 number is ");// Display leading zeros, improve the solution using loops in the next chapterif (number / 100000000 == 0) {System.out.print("0");if (number / 10000000 == 0) {System.out.print("0");if (number / 1000000 == 0) {System.out.print("0");if (number / 100000 == 0) {System.out.print("0");if (number / 10000 == 0) { System.out.print("0");if (number / 1000 == 0) { System.out.print("0");if (number / 100 == 0) {System.out.print("0");if (number / 10 == 0) {System.out.print("0");if (number == 0) {System.out.print("0");}}}}}}}}}System.out.print(number);if (checksum == 10)System.out.print("X");elseSystem.out.print(checksum);}}3.10public class Exercise03_10 {public static void main(String[] args) {// 1. Generate two random single-digit integersint number1 = (int)(Math.random() * 10);int number2 = (int)(Math.random() * 10);// 2. Prompt the student to answer 搘hat is number1 + number2?? System.out.print("What is " + number1 + " + " + number2 + "? "); Scanner input = new Scanner(System.in);int answer = input.nextInt();// 4. Grade the answer and display the resultString replyString;if (number1 + number2 == answer)replyString = "You are correct!";elsereplyString = "Your answer is wrong.\" + number1 + " + "+ number2 + " should be " + (number1 + number2);System.out.println(replyString);}}3.11public class Exercise03_11 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Prompt the user to enter inputSystem.out.print("Enter a month in the year (e.g., 1 for Jan): ");int month = input.nextInt();System.out.print("Enter a year: ");int year = input.nextInt();int numberOfDaysInMonth = 0;switch (month) {case 1:System.out.print("January " + year);numberOfDaysInMonth = 31;break;case 2:System.out.print("February " + year);if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { numberOfDaysInMonth = 29;}else {numberOfDaysInMonth = 28;}break;case 3:System.out.print("March " + year);numberOfDaysInMonth = 31;break;case 4:System.out.print("April " + year);numberOfDaysInMonth = 30;break;case 5:System.out.print("May " + year);numberOfDaysInMonth = 31;break;case 6:System.out.print("June " + year);numberOfDaysInMonth = 30;break;case 7:System.out.print("July " + year);numberOfDaysInMonth = 31;break;case 8:System.out.print("August " + year);numberOfDaysInMonth = 31;break;case 9:System.out.print("September " + year);numberOfDaysInMonth = 30;break;case 10:System.out.print("October " + year);numberOfDaysInMonth = 31;break;case 11:System.out.print("November " + year);numberOfDaysInMonth = 30;break;case 12:System.out.print("December " + year);numberOfDaysInMonth = 31;break;}System.out.print(" has " + numberOfDaysInMonth + " days"); }}3.12public class Exercise03_12 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter a three-digit integer: ");int number = input.nextInt();if (number / 100 == number % 10)System.out.println(number + " is a palindrome");elseSystem.out.println(number + " is not a palindrome");}}3.13public class Exercise03_13 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Prompt the user to enter filing statusSystem.out.print("(0-single filer, 1-married jointly or qualifying widow(er),"+ "\2-married separately, 3-head of household)\" + "Enter the filing status: ");int status = input.nextInt();// Prompt the user to enter taxable incomeSystem.out.print("Enter the taxable income: ");double income = input.nextDouble();// Compute taxdouble tax = 0;if (status == 0) { // Compute tax for single filersif (income <= 8350) {tax = income * 0.10;} else if (income <= 33950) {tax = 8350 * 0.10 + (income - 8350) * 0.15;} else if (income <= 82250) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950)* 0.25; } else if (income <= 171550) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (income - 82250) * 0.28;} else if (income <= 372950) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (income - 171550) * 0.33;} else {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (372950 - 171550) * 0.33 + (income - 372950) * 0.35;}} else if (status == 1) { // Compute tax for married file jointly if (income <= 16700) {tax = income * 0.10;} else if (income <= 67900) {tax = 16700 * 0.10 + (income - 16700) * 0.15;} else if (income <= 137050) {tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (income - 67900) * 0.25; } else if (income <= 208850) {tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (income - 137050) * 0.28;} else if (income <= 372950) {tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (income - 208850) * 0.33;} else {tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (171950 - 137050) * 0.28 + (372950 - 208850) * 0.33+ (income - 372950) * 0.35;}} else if (status == 2) { // Compute tax for married separately if (income <= 8350) {tax = income * 0.10;} else if (income <= 33950) {tax = 8350 * 0.10 + (income - 8350) * 0.15;} else if (income <= 68525) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25; } else if (income <= 104425) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (income - 68525) * 0.28;} else if (income <= 186475) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (income - 104425) * 0.33;} else {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (186475 - 104425) * 0.33 + (income - 186475) * 0.35;}} else if (status == 3) { // Compute tax for head of household if (income <= 11950) {tax = income * 0.10;} else if (income <= 45500) {tax = 11950 * 0.10 + (income - 11950) * 0.15;} else if (income <= 117450) {tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (income - 45500) * 0.25; } else if (income <= 190200) {tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (income - 117450) * 0.28;} else if (income <= 372950) {tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (income - 190200) * 0.33;} else {tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (372950 - 190200) * 0.33+ (income - 372950) * 0.35;}} else {System.out.println("Error: Wrong filing status");System.exit(1);}// Display the resultSystem.out.println("Tax is " + (int) (tax * 100) / 100.0);}}3.14public class Exercise03_14 {public static void main(String[] args) {// Obtain the random number 0 or 1int number = (int)(Math.random() * 2);// Prompt the user to enter a guessjava.util.Scanner input = new java.util.Scanner(System.in);System.out.print("Guess head or tail? " +"Enter 0 for head and 1 for tail: ");int guess = input.nextInt();// Check the guessif (guess == number)System.out.println("Correct guess");else if (number == 0)System.out.println("Sorry, it is a head");elseSystem.out.println("Sorry, it is a tail");}}3.15public class Exercise03_15 {public static void main(String[] args) {// Generate a lotteryint lottery = (int)(Math.random() * 1000);// Prompt the user to enter a guessjava.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter your lottery pick (three digits): ");int guess = input.nextInt();// Get digitsint l1 = lottery / 100;int l2 = (lottery % 100) / 10; // l2 = (lottery / 10) % 10int l3 = lottery % 10;int g1 = guess / 100;int g2 = (guess % 100) / 10;int g3 = guess % 10;System.out.println("Lottery is " + lottery);// Check the guessif (guess == lottery)System.out.println("Exact match: you win $10,000");else if (g1 == l1 && g3 == l2 && g2 == l3 ||g2 == l1 && g1 == l2 && g3 == l3 ||g2 == l1 && g3 == l2 && g1 == l3 ||g3 == l1 && g1 == l2 && g2 == l3 ||g3 == l1 && g2 == l2 && g1 == l3)System.out.println("Match all digits: you win $3,000"); else if (g1 == l1 || g1 == l2 || g1 == l3 ||g2 == l1 || g2 == l2 || g2 == l3 ||g3 == l1 || g3 == l2 || g3 == l3)System.out.println("Match one digit: you win $1,000"); elseSystem.out.println("Sorry, no match");}}3.16public class Exercise03_16 {public static void main(String[] args) {double x = Math.random() * 100 - 50;double y = Math.random() * 200 - 100;System.out.println(x + ", " + y);}}3.17public class Exercise03_17 {public static void main(String[] args) {// Generate scissor, rock, paperint computerNumber = (int)(Math.random() * 3);// Prompt the user to enter scissor, rock, or paper java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("scissor (0), rock (1), paper (2): ");int userNumber = input.nextInt();// Check the guessswitch (computerNumber) {case 0:if (userNumber == 0)System.out.print("The computer is scissor. You are scissor too. It is a draw");else if (userNumber == 1)System.out.print("The computer is scissor. You are rock. You won");else if (userNumber == 2)System.out.print("The computer is scissor. You are paper. You lost");break;case 1:if (userNumber == 0)System.out.print("The computer is rock. You are scissor. You lost");else if (userNumber == 1)System.out.print("The computer is rock. You are rock too. It is a draw");else if (userNumber == 2)System.out.print("The computer is rock. You are paper. You won");break;case 2:if (userNumber == 0)System.out.print("The computer is paper. You are scissor. You won");else if (userNumber == 1)System.out.print("The computer is paper. You are rock. You lost");else if (userNumber == 2)System.out.print("The computer is paper. You are paper too.It is a draw");break;}}}3.18public class Exercise03_18 {public static void main(String args[]) {Scanner input = new Scanner(System.in);System.out.print("Enter package weight: ");double w = input.nextDouble();if (w <= 1) {System.out.println("The shipping cost is $3.5");}else if (w <= 3) {System.out.println("The shipping cost is $5.5");}else if (w <= 10) {System.out.println("The shipping cost is $8.5");}else if (w <= 20) {System.out.println("The shipping cost is $10.5");}else {System.out.println("The package cannot be shipped");}}}3.19public class Exercise03_19 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in); // Enter three edgesSystem.out.print("Enter three edges (length in double): ");double edge1 = input.nextDouble();double edge2 = input.nextDouble();double edge3 = input.nextDouble();。