云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > java web编程技术(沈泽刚,清华大学出版社)双星习题部分解答

java web编程技术(沈泽刚,清华大学出版社)双星习题部分解答

  • 62 次阅读
  • 3 次下载
  • 2025/7/9 19:22:13

if (le.IsSolvable()){//其实还有一种情况是交点不在线段上,在此暂不考虑 System.out.println(\交点为:(\ }else{ System.out.println(\两条线段不相交!\ } } }

class LinearEquation{ private double a,b,c,d,e,f; public LinearEquation(double x1,double y1, double x2,double y2,double x3,double y3,double x4,double y4){ this.a = y1 - y2; this.b = x2 - x2; this.e = y1 * x2 - x1 * y2; this.c = y3 - y4; this.d = x4 - x3; this.f = y3 * x4 - x3 * y4; } public boolean IsSolvable(){ boolean isSolvable = true; if (a*d - b*c < Math.pow(10,-6)){ isSolvable = false; } return isSolvable; } public double GetX(){ return (e * d - b * f)/(a * d - b * c); } public double GetY(){ return (a * f - e * c)/(a * d - b * c); } } 8_13 /**

(位置类Location)设计一个名为Location的类,定位二维数组中的最大值及其位置。这个类包括公共的数据域row、 column和maxValue,二维数组中的最大值及其下标用int型的row和column以及double型的maxValue存储。

编写下面的方法,返回一个二维数组中最大值的位置。 public static Location LocateLargest(double[][] a)

返回值是一个Location的实例。编写一个测试程序,提示用户输入一个二维数组,然后显

示这个数组中的最大元素。 */

import java.util.Scanner; public class Chap8_13{ public static void main(String[] args){ int row,col; Scanner sc = new Scanner(System.in); System.out.print(\请输入二维数组的行列数:\ row = sc.nextInt(); col = sc.nextInt(); System.out.println(\在下面输入数组的数据:\ double[][] a = new double[row][col]; for (int i = 0; i < row; i++){ for (int j = 0; j < col; j++){ a[i][j] = sc.nextDouble(); } } Location location = new Location(a); System.out.println(\最大元素是:\ \,位置为:(\)\ } }

class Location{ private int row; private int column; private double maxValue; public Location(double[][] a){ this.row = 0; this.column = 0; this.maxValue = a[0][0]; for (int i = 0; i < a.length; i++){ for (int j = 0; j < a[0].length; j++){ if (a[i][j] > maxValue){ this.row = i; this.column = j; this.maxValue = a[i][j]; }

} } } public int GetRow(){ return this.row; } public int GetCol(){ return this.column; } public double GetMax(){ return this.maxValue; } } 9_2 /**

(检测子串)可以使用String类中的indexOf方法检测一个字符串是否是另一个字符串子串。 编写自己的方法实现这个功能。

编写一个程序,提示用户输入两个字符串,检测第一个字符串是否是第二个字符串的子串。 */

import java.util.Scanner; public class Chap9_2{ public static void main(String[] args){ String str1,str2; Scanner sc = new Scanner(System.in); System.out.print(\请输入第一个字符串:\ str1 = sc.next(); System.out.print(\请输入第二个字符串:\ str2 = sc.next(); if (isSubStr(str1,str2)) System.out.println(str1 + \是\的子串。\ else System.out.println(str1 + \不是\的子串。\ } public static boolean isSubStr(String str1,String str2){ if (-1 == str2.indexOf(str1)) return false; else return true; } } 9_3 /**

(检验密码)一些网站设定了一些制定密码的规则。编写一个方法,检验一个字符串是否是合法的密码。

假设密码规则如下:

1、密码必须至少有8个字符 2、密码只能包括字母和数字

3、密码必须至少有2个数字 编写一个程序,提示用户输入密码,如果该密码符合规则就显示“有效密码”,否则显示“无效密码” 分析:

用三个boolean返回值的函数分别做判断 1、isLengthValid(String) 2、isCharValid(String) 3、isNumberValid(String) */

import java.util.Scanner; public class Chap9_3{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String str; System.out.print(\请输入密码:\ str = sc.next(); if (isValidPass(str)) System.out.println(\有效密码\ else System.out.println(\无效密码\ } private static boolean isValidPass(String str){ if (isLengthValid(str) && isCharValid(str) && isNumberValid(str)) return true; else return false; } private static boolean isLengthValid(String str){ if (str.length() >= 8) return true; else return false; } private static boolean isCharValid(String str){ boolean isCharValid = true; for (int i = 0; i < str.length() ; i++) { if (!Character.isLetterOrDigit(str.charAt(i))){ isCharValid = false; break; } } return isCharValid; } private static boolean isNumberValid(String str){ int count = 0; for (int i = 0; i < str.length(); i++){ if (Character.isDigit(str.charAt(i))) count++; } if (count >= 2) return true; else return false;

  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

if (le.IsSolvable()){//其实还有一种情况是交点不在线段上,在此暂不考虑 System.out.println(\交点为:(\ }else{ System.out.println(\两条线段不相交!\ } } } class LinearEquation{ private double a,b,c,d,e,f; public LinearEquation(double x1,double y1, double x2,double y2,double x3,double y3,double x4,double y4){ this.a = y1 - y2; this.b = x2 - x2; this.e = y1 * x2 - x1 * y2; this.c = y3 - y4; this.d = x4

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com