《Java语言程序设计与数据结构》编程练习答案(第三章)(三)

英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition

3.24

public class book {public static void main(String[] args){int num = (int)(Math.random()*13);int dick = (int)(Math.random()*4);String[] nums = {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};String[] dicks = {"Clubs","Diamonds","Hearts","Spades"};System.out.println("The card you picked is "+nums[num]+" of "+dicks[dick]);}
}

3.25

import java.util.Scanner;
public class book {public static void main(String[] args){System.out.print("Enter x1, y1, x2, y2, x3, y3, x4, y4: ");Scanner input = new Scanner(System.in);double x1 = input.nextDouble();double y1 = input.nextDouble();double x2 = input.nextDouble();double y2 = input.nextDouble();double x3 = input.nextDouble();double y3 = input.nextDouble();double x4 = input.nextDouble();double y4 = input.nextDouble();double a = y1-y2;double b = x2-x1;double c = y3-y4;double d = x4-x3;double e = (y1-y2)*x1-(x1-x2)*y1;double f = (y3-y4)*x3-(x3-x4)*y3;double jb = a*d-b*c;if(jb==0)System.out.println("The two lines are parallel");elseSystem.out.println("The intersecting point is at ("+(e*d-b*f)/jb+", "+(a*f-e*c)/jb+")");}
}

3.26

import java.util.Scanner;
public class book {public static void main(String[] args){System.out.print("Enter an integer: ");Scanner input = new Scanner(System.in);int n = input.nextInt();System.out.println("Is "+n+" divisible by 5 and 6? "+(n%5==0&&n%6==0));System.out.println("Is "+n+" divisible by 5 or 6? "+(n%5==0||n%6==0));System.out.println("Is "+n+" divisible by 5 or 6, but not both? "+(n%5==0^n%6==0));}
}

3.27

import java.util.Scanner;
public class book {public static void main(String[] args){System.out.print("Enter a point's x- and y- coordinates: ");Scanner input = new Scanner(System.in);double x = input.nextDouble();double y = input.nextDouble();if(x>=0&&x<=200&&y>=0&&y<=100-0.5*x)System.out.println("The point is in the triangle");elseSystem.out.println("The point is not in the triangle");}
}

3.28

import java.util.Scanner;
public class book {public static void main(String[] args){Scanner input = new Scanner(System.in);System.out.print("Enter r1's center x-, y-coordinates, width, and height: ");double x1 = input.nextDouble();double y1 = input.nextDouble();double w1 = input.nextDouble();double h1 = input.nextDouble();System.out.print("Enter r2's center x-, y0coordinates, width, and height: ");double x2 = input.nextDouble();double y2 = input.nextDouble();double w2 = input.nextDouble();double h2 = input.nextDouble();if((x1+w1/2>=x2+w2/2)&&(x1-w1/2<=x2-w2/2)&&(y1+h1/2>=y2+h2/2)&&(y1-h1/2<=y2-h2/2))System.out.println("r2 is inside r1");else if((x2-w2/2>x1+w1/2)||(x2+w2/2<x1-w1/2)||(y2-h2/2>y1+h1/2)||(y2+h2/2<y1-h1/2))System.out.println("r2 does not overlap r1");else if((x2+w2/2>=x1+w1/2)&&(x2-w2/2<=x1-w1/2)&&(y2+h2/2>=y1+h1/2)&&(y2-h2/2<=y1-h1/2))System.out.println("r1 is inside r2");elseSystem.out.println("r2 overlaps r1");}
}

3.29

import java.util.Scanner;
public class book {public static void main(String[] args){Scanner input = new Scanner(System.in);System.out.print("Enter circle's center x-, y-coordinates, and radius: ");double x1 = input.nextDouble();double y1 = input.nextDouble();double r1 = input.nextDouble();System.out.print("Enter circle2's center x-, y-coordinates, and radius: ");double x2 = input.nextDouble();double y2 = input.nextDouble();double r2 = input.nextDouble();if(Math.pow((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2),0.5)<=Math.abs(r1-r2))System.out.println("circle2 is inside circle1");else if(Math.pow((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2),0.5)<=r1+r2)System.out.println("circle2 overlaps circle1");elseSystem.out.println("circle2 does not overlap circle1");}
}

3.30

import java.util.Scanner;
public class book {public static void main(String[] args){Scanner input = new Scanner(System.in);System.out.print("Enter the time zone offset to GMT:");long offset = input.nextLong();long totalMilliseconds = System.currentTimeMillis();totalMilliseconds+=offset*60*60*1000;long totalSeconds = totalMilliseconds/1000;long currentSecond = totalSeconds%60;long totalMinutes = totalSeconds/60;long currentMinute = totalMinutes%60;long totalHours = totalMinutes/60;long currentHour = totalHours%24;String tail = "AM";if(currentHour>12){currentHour-=12;tail="PM";}System.out.println("Current time is "+currentHour+":"+currentMinute+":"+currentSecond+" "+tail);}
}

3.31

import java.util.Scanner;
public class book {public static void main(String[] args){Scanner input = new Scanner(System.in);System.out.print("Enter the exchange rate from dollar to RMB: ");double rate = input.nextDouble();System.out.print("Enter 0 to convert dollars to RMB and 1 vice versa: ");int sig = input.nextInt();double in = 0;double out = 0;String moneyType = "";if(sig==0)moneyType="dollar";else if(sig==1)moneyType="RMB";elseSystem.out.println("Incorrect input");if(sig==0||sig==1){System.out.print("Enter the "+moneyType+" amount: ");in = input.nextDouble();if(sig==0){out = in*rate;System.out.println("$"+in+" is "+out+" yuan");}else{out = in/rate;System.out.println(in+" yuan is $"+out);}}}
}

3.32

import java.util.Scanner;
public class book {public static void main(String[] args){System.out.print("Enter three points for p0, p1, and p2: ");Scanner input = new Scanner(System.in);double x0 = input.nextDouble();double y0 = input.nextDouble();double x1 = input.nextDouble();double y1 = input.nextDouble();double x2 = input.nextDouble();double y2 = input.nextDouble();double result = (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0);if(result>0)System.out.println("p2 is on the left side of the line");else if(result==0)System.out.println("p2 is on the same line");elseSystem.out.println("p2 is on the right side of the line");}
}

3.33

import java.util.Scanner;
public class book {public static void main(String[] args){Scanner input = new Scanner(System.in);System.out.print("Enter weight and price for package 1: ");double w1 = input.nextDouble();double p1 = input.nextDouble();System.out.print("Enter weight and price for package 2: ");double w2 = input.nextDouble();double p2 = input.nextDouble();double av1 = p1/w1;double av2 = p2/w2;if(av1<av2)System.out.println("Package 1 has a better price.");else if(av1==av2)System.out.println("Two packages have the same price.");elseSystem.out.println("Package 2 has a better price.");}
}

3.34

import java.util.Scanner;
public class book {public static void main(String[] args){Scanner input = new Scanner(System.in);System.out.print("Enter three points for p0, p1, and p2: ");double x0 = input.nextDouble();double y0 = input.nextDouble();double x1 = input.nextDouble();double y1 = input.nextDouble();double x2 = input.nextDouble();double y2 = input.nextDouble();if((x1-x0)*(y2-y0)-(x2-x0)*(y1-y0)==0&&x2>=x0&&x2<=x1)System.out.println("("+x2+", "+y2+") is on the line segment from ("+x0+", "+y0+") to ("+x1+", "+y1+")");elseSystem.out.println("("+x2+", "+y2+") is not on the line segment from ("+x0+", "+y0+") to ("+x1+", "+y1+")");}
}

第三章 完

《Java语言程序设计与数据结构》编程练习答案(第三章)(三)相关推荐

  1. 《Java语言程序设计与数据结构》编程练习答案(第七章)(一)

    <Java语言程序设计与数据结构>编程练习答案(第七章)(一) 英文名:Introduction to Java Programming and Data Structures, Comp ...

  2. 《Java语言程序设计与数据结构》编程练习答案(第四章)(二)

    <Java语言程序设计与数据结构>编程练习答案(第四章)(二) 英文名:Introduction to Java Programming and Data Structures, Comp ...

  3. 《Java语言程序设计与数据结构》编程练习答案(第四章)(一)

    <Java语言程序设计与数据结构>编程练习答案(第四章)(一) 英文名:Introduction to Java Programming and Data Structures, Comp ...

  4. 《Java语言程序设计与数据结构》编程练习答案(第二章)(二)

    <Java语言程序设计与数据结构>编程练习答案(第二章)(二) 英文名:Introduction to Java Programming and Data Structures, Comp ...

  5. java考试安徽工业大学_2011~2012《Java语言程序设计》试卷A及答案(安徽工业大学)...

    <2011~2012<Java语言程序设计>试卷A及答案(安徽工业大学)>由会员分享,可在线阅读,更多相关<2011~2012<Java语言程序设计>试卷A及 ...

  6. 南开大学java考试试题_2014秋学期南开大学《Java语言程序设计》在线作业附答案...

    2014秋学期南开大学<Java语言程序设计>在线作业附答案 1.下列代码中,将引起一个编译错误的行是(D). 1)public class Test{ 2) int m,n; 3) pu ...

  7. 自考04747《java语言程序设计(一)》课件_自考04747《java语言程序设计(一)》课后习题答案全集...

    <自考04747<java语言程序设计(一)>课后习题答案全集>由会员分享,可在线阅读,更多相关<自考04747<java语言程序设计(一)>课后习题答案全集 ...

  8. java语言程序设计期末考试试题及答案_《JAVA语言程序设计》期末考试试题及答案1-7.doc...

    <JAVA语言程序设计>期末考试试题及答案1-7 社望斡工耪纱访肝讫否稗仗族锯滥祥缀疏霹辐螟丁哥联退控罐绳屑铸氛券误滚烫周瞎鲸明垢礁蛀撞瓶蔬辗撞往件沽囊炕蔫消辉孽层束沁吨减攻匿少纳涡唆晚亦 ...

  9. (1 24) 3 java代码_《Java语言程序设计》测试题及参考答案(第1部分)

    一.单项选择题 1.Java Applet编程时用户的Applet程序类的正确定义是哪一种 ? (1)class MyApplet extends applet{ } (2)class MyApple ...

最新文章

  1. 《Android开发从零开始》——11.AbsoluteLayoutFrameLayout学习
  2. jbutton可以设置id吗_Java JButton.setHorizontalTextPosition方法代碼示例
  3. Android中通过SeekBar手动控制ProgressBar与模拟下载自动更新进度条
  4. C语言实现hash/xor8算法(附完整源码)
  5. mybatis example处理and、or关系的方法
  6. 4-2 ADO.NET-查询和检索数据13
  7. ModelMap和ModelAndView的作用
  8. 拯救天使 (BFS)
  9. matlab bdir 排序,命令行 - 如何获取按文件夹名称排序的子文件夹及其文件列表
  10. 鼠标经过背景图片变换
  11. 从0开始学习 GitHub 系列之「向GitHub 提交代码」
  12. css border设置为透明
  13. UESTC 1636 梦后楼台高锁,酒醒帘幕低垂 最小生成树Kruskal算法的扩展
  14. 腾讯校招20道选择题含答案
  15. week8-csp-B(HRZ学英语)
  16. UWB高精度室内定位系统
  17. 【C语言】scanf函数报错
  18. 20条技巧,让Chrome超越Firefox
  19. nlp-with-transformers实战-01_transformers简介
  20. 一周AI新闻 | 追踪眼球找BUG,AI可从眼球运动中学习

热门文章

  1. 法大大连获36氪“年度硬核企业”等4项大奖
  2. 打印1000~2000年之间的闰年
  3. 计算机接口参数在哪儿看,怎么看笔记本cpu接口参数
  4. java三次指数平滑_时间序列挖掘-预测算法-三次指数平滑法(Holt-Winters)
  5. SAP Data Service操作简介
  6. 儿子于靖洋15个月照片
  7. 学生用计算机重启,学生用计算器咋关机
  8. 支付宝sdk集成,报系统繁忙 请稍后再试(ALI64)
  9. 关于专利申请中发明内容和具体实施方法有什么本质区别?
  10. 如何用python更改图片的像素