Java语言程序设计与数据结构(基础篇)课后练习题 第九章(1)

文章展示了Java编程中的基础操作,包括构造RegularPolygon和QuadraticEquation/LinearEquation类,进行几何形状的周长和面积计算,以及线性方程的求解。还涉及输入验证和找到二维数组中最大值的位置。
摘要由CSDN通过智能技术生成

public Fan(){

speed=SLOW;

on=false;

radius=5;

color=“blue”;

}

public String toString(){

String r=“”;

if(this.on)

r=r+speed+" “+color+” "+radius;

else

r=r+"fan is off “+color+” "+radius;

return r;

}

}

9.9

================================================================

package demo;

public class dijiuzhang {

public static void main(String[] args) {

// TODO Auto-generated method stub

RegularPolygon p = new RegularPolygon();

RegularPolygon p2 = new RegularPolygon(6,4);

RegularPolygon p3 = new RegularPolygon(10,4,5.6,7.8);

System.out.println(p.getPerimeter()+" "+p.getArea());

System.out.println(p2.getPerimeter()+" "+p2.getArea());

System.out.println(p3.getPerimeter()+" "+p3.getArea());

}

}

class RegularPolygon{

private int n=3;

private double side=1;

private double x=0;

private double y=0;

public RegularPolygon(){

n=3;

side=1;

}

public RegularPolygon(int num,double len){

n=num;

side=len;

}

public RegularPolygon(int hum,double len,double x,double y){

n=hum;

side=len;

x=x;

y=y;

}

public void setN(int n){

n=n;

}

public int getN(){

return n;

}

public void setSide(double s){

side=s;

}

public double getSide(){

return side;

}

public void setX(double x){

x=x;

}

public double getX(){

return x;

}

public void setY(double y){

y=y;

}

public double getY(){

return y;

}

public double getPerimeter(){

return n*side;

}

public double getArea(){

return nsideside/(4*Math.tan(Math.PI/n));

}

}

9.10

=================================================================

package demo;

import java.util.Scanner;

public class dijiuzhang {

public static void main(String[] args) {

// TODO Auto-generated method stub

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();

QuadraticEquation r = new QuadraticEquation(a,b,c);

if(r.getDiscriminant()<0)

System.out.println(“The equation has no roots.”);

else if(r.getDiscriminant()==0)

System.out.println("One root "+r.getRoot1());

else

System.out.println("Two roots “+r.getRoot1()+” "+r.getRoot2());

}

}

class QuadraticEquation{

private double a;

private double b;

private double c;

public QuadraticEquation(double a1,double b1,double c1){

a=a1;

b=b1;

c=c1;

}

public double getA(){

return a;

}

public double getB(){

return b;

}

public double getC(){

return c;

}

public double getDiscriminant(){

return bb-4a*c;

}

public double getRoot1(){

return (-1b+Math.sqrt(bb-4ac))/(2*a);

}

public double getRoot2(){

return (-1b-Math.sqrt(bb-4ac))/(2*a);

}

}

9.11

=================================================================

package demo;

import java.util.Scanner;

public class dijiuzhang {

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.print("Enter a b c d e f: ");

Scanner input = new Scanner(System.in);

double a = input.nextDouble();

double b = input.nextDouble();

double c = input.nextDouble();

double d = input.nextDouble();

double e = input.nextDouble();

double f = input.nextDouble();

LinearEquation r = new LinearEquation(a,b,c,d,e,f);

if(r.isSolvable())

System.out.println(r.getX()+" "+r.getY());

else

System.out.println(“The equation has no solutions.”);

}

}

class LinearEquation{

private double a,b,c,d,e,f;

public LinearEquation(double a1,double b1,double c1,double d1,double e1,double f1){

a=a1;

b=b1;

c=c1;

d=d1;

e=e1;

f=f1;

}

public double getA(){

return a;

}

public double getB(){

return b;

}

public double getC(){

return c;

}

public double getD(){

return d;

}

public double getE(){

return e;

}

public double getF(){

return f;

}

public boolean isSolvable(){

return ad-bc!=0;

}

public double getX(){

return (ed-bf)/(ad-bc);

}

public double getY(){

return (af-ec)/(ad-bc);

}

}

9.12

=================================================================

package demo;

import java.util.Scanner;

public class dijiuzhang {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input = new Scanner(System.in);

System.out.print("Enter the four points x1 y1 x2 y2 x3 y3 x4 y4: ");

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();

LinearEquation j = new LinearEquation(y1-y2,x2-x1,y3-y4,x4-x3,(y1-y2)*x1-(x1-x2)*y1,(y3-y4)*x3-(x3-x4)*y3);

if(j.isSolvable())

System.out.println(j.getX()+" "+j.getY());

else

System.out.println(“No joining.”);

}

}

class LinearEquation{

private double a,b,c,d,e,f;

public LinearEquation(double a1,double b1,double c1,double d1,double e1,double f1){

a=a1;

b=b1;

c=c1;

d=d1;

e=e1;

f=f1;

}

public double getA(){

return a;

}

public double getB(){

return b;

}

public double getC(){

return c;

}

public double getD(){

return d;

}

public double getE(){

return e;

}

public double getF(){

return f;

}

public boolean isSolvable(){

return ad-bc!=0;

}

public double getX(){

return (ed-bf)/(ad-bc);

}

public double getY(){

return (af-ec)/(ad-bc);

}

}

9.13

=================================================================

package demo;

import java.util.Scanner;

public class dijiuzhang {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input = new Scanner(System.in);

System.out.print("Enter the number of rows and columns in the array: ");

int rows = input.nextInt();

int columns = input.nextInt();

double[][] n = new double[rows][columns];

System.out.print("Enter the array: ");

for(int i=0;i<rows;i++){

for(int j=0;j<columns;j++)

n[i][j]=input.nextDouble();

}

Location location = locateLargest(n);

System.out.println(“The location of the largest element is " + location.maxValue+” at (" + location.row+“, " + location.column+”)");

}

public static Location locateLargest(double[][] a){

Location max = new Location();

max.maxValue=a[0][0];

max.column=0;

max.row=0;

int rowCount = a.length;

int columnCount = a[0].length;

for(int i=0;i<rowCount;i++){

for(int j=0;j<columnCount;j++){

if(a[i][j]>max.maxValue){

max.row=i;

max.maxValue=a[i][j];

max.column=j;

}

}

}

return max;

}

}

class Location{

public int row;

public int column;

public double maxValue;

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
img

常用的JavaScript设计模式

  • 单体模式

  • 工厂模式

  • 例模式

函数

较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频**

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
[外链图片转存中…(img-CMAAbyt4-1710604438697)]

常用的JavaScript设计模式

  • 单体模式

  • 工厂模式

  • 例模式

函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值