【java】第9章 对象和类 测试题(作业部分)

这个博客涵盖了多个Java编程概念,包括创建矩形类计算面积和周长,使用Random生成随机数,通过GregorianCalendar获取当前日期,实现正多边形类计算周长和面积,以及解决线性方程组找到交点。示例代码展示了如何在Java中实现这些功能。
摘要由CSDN通过智能技术生成

文章目录

9.1

在这里插入图片描述
在这里插入图片描述

package wwr;
//9.1
class Rectangle {
	//宽&高
	double width;	
	double height;
	//无参构造
	Rectangle() {
		width = 1;
		height = 1;
	}
	//构造方法
	Rectangle(double newWidth, double newHeight) {
		width = newWidth;
		height = newHeight;
	}
	//面积
	double getArea() {
		return width * height;
	}
	//周长
	double getPerimeter() {
		return 2 * (width + height);
	}
}
public class Demo {
	
    public static void main(String[] args) {
        Rectangle rectangle1 = new Rectangle(4, 40);
        System.out.println("The width of rectangle1 is " + rectangle1.width);
        System.out.println("The height of rectangle1 is " + rectangle1.height);
        System.out.println("The area of rectangle1 is " + rectangle1.getArea());
        System.out.println("The perimeterof rectangle1 is " + rectangle1.getPerimeter());
        Rectangle rectangle2 = new Rectangle(3.5, 35.9);
        System.out.println("The width of rectangle2 is " + rectangle2.width);
        System.out.println("The height of rectangle2 is " + rectangle2.height);
        System.out.println("The area of rectangle2 is " + rectangle2.getArea());
        System.out.println("The perimeterof rectangle2 is " + rectangle2.getPerimeter());

    }
}

9.4

在这里插入图片描述

package wwr;
//9.4
import java.util.Random;
public class Demo {
	
    public static void main(String[] args) {
        
    	Random random = new java.util.Random(1000);
    	for(int i = 0; i < 50; i++) {
    		System.out.print(random.nextInt(100) + " ");
    	}
    }
}

9.5

在这里插入图片描述

package wwr;
//9.5
import java.util.GregorianCalendar;
public class Demo {
	
    public static void main(String[] args) {
        
    	GregorianCalendar calendar = new GregorianCalendar();
    	System.out.print(calendar.get(GregorianCalendar.YEAR) + 
    			//月份是从0开的,比如1月输出0,所以都得加1,好坑QAQ
    			" " + (calendar.get(GregorianCalendar.MONTH) + 1) +
    			" " + calendar.get(GregorianCalendar.DAY_OF_MONTH));
    	System.out.println();
    	calendar.setTimeInMillis(1234567898765L);
    	System.out.print(calendar.get(GregorianCalendar.YEAR) + 
    			" " + (calendar.get(GregorianCalendar.MONTH) + 1) + 
    			" " + calendar.get(GregorianCalendar.DAY_OF_MONTH));
    }
}

9.9

在这里插入图片描述
在这里插入图片描述

package wwr;
//9.9
 class RegularPolygon {
	 
	private int n;			//边数
	private double side; 	//边长
	private double x;		//中点x坐标	
	private double y;		//中点y坐标
	//带有默认值的无参构造
	RegularPolygon() {
		n = 3;
		side = 1;
		x = 0;
		y = 0;
	}
	//指定边数和边长,中心在(0, 0)
	RegularPolygon(int newN, double newSide) {
		n = newN;
		side = newSide;
		x = 0;
		y = 0;
	}
	//指定边数和边长,中心在(x, y)
	RegularPolygon(int newN, double newSide, double newX, double newY) {
		n = newN;
		side = newSide;
		x = newX;
		y = newY;
	}
	//访问器
	int getN() {
		return n;
	}
	double getSide() {
		return side;
	}
	double getX() {
		return x;
	}
	double getY() {
		return y;
	}
	//修改器
	void setN(int newN) {
		n = newN;
	}
	void setSide(double newSide) {
		side = newSide;
	}
	void setX(double newX) {
		x = newX;
	}
	void setY(double newY) {
		y = newY;
	}
	//周长
	double getPerimeter() {
		return side * n;
	}
	//面积
	double getArea() {
		return (n * side * side) / (4 * Math.tan(Math.PI / n));
	}
}

public class Demo {
	
    public static void main(String[] args) {
        
    	RegularPolygon rp1 = new RegularPolygon();
    	RegularPolygon rp2 = new RegularPolygon(6, 4);
    	RegularPolygon rp3 = new RegularPolygon(10, 4, 5.6, 7.8);
    	System.out.println("rp1's perimeter is " + 
    			rp1.getPerimeter() + ", rp1's area is " + rp1.getArea());
    	System.out.println("rp2's perimeter is " + 
    	    	rp2.getPerimeter() + ", rp2's area is " + rp2.getArea());
    	System.out.println("rp3's perimeter is " + 
    	    	rp3.getPerimeter() + ", rp3's area is " + rp3.getArea());
    }
}

9.12

在这里插入图片描述

package wwr;
//9.12
import java.util.Scanner;
class LinearEquation {
	
	private double a;
	private double b;
	private double c;
	private double d;
	private double e;
	private double f;
	
	LinearEquation(double a, double b, double c, double d, double e, double f) {
		this.a = a;
		this.b = b;
		this.c = c;
		this.d = d;
		this.e = e;
		this.f = f;
	}
	
	double getA() {
		return a;
	}
	double getB() {
		return b;
	}
	double getC() {
		return c;
	}
	double getD() {
		return d;
	}
	double getE() {
		return e;
	}
	double getF() {
		return f;
	}
	double getX() {
		return (e * d - b * f) / (a * d - b * c) ;
	}
	double getY() {
		return (a * f - e * c) / (a * d - b * c);
	}
	
}
public class Demo {
	
    public static void main(String[] args) {
        
    	Scanner input = new Scanner(System.in);
    	System.out.print("Enter the two endpoints of the first line segment (x1, y1, x2, y2): ");
    	double x1 = input.nextInt();
    	double y1 = input.nextInt();
    	double x2 = input.nextInt();
    	double y2 = input.nextInt();
    	double a = (y1 - y2) / (x2 - x1);
    	double b = 1;
    	double e = y1 - ((x1 * (y2 - y1)) / (x2 - x1));
    	System.out.print("Enter the two endpoints of the second line segment (x3, y3, x4, y4): ");
    	double x3 = input.nextInt();
    	double y3 = input.nextInt();
    	double x4 = input.nextInt();
    	double y4 = input.nextInt();
    	double c = (y3 - y4) / (x4 - x3);
    	double d = 1;
    	double f = y3 - ((x3 * (y4 - y3)) / (x4 - x3));
    	if(a * d - b * c == 0)
    		System.out.println("The equation has no solution");
    	else {
    		LinearEquation le = new LinearEquation(a, b, c, d, e, f);
//			LinearEquation le = new LinearEquation(9.0, 4.0, 3.0, -5.0, -6.0, -21.0);
			double tepX = le.getX();
			double tepY = le.getY();
			System.out.print("x is " + tepX + " and y is " + tepY);
    	}
    }
}

9.13

在这里插入图片描述

package wwr;
//9.13
import java.util.Scanner;

class Location {
	
	public int row;
	public int column;
	public double maxValue;
	public static Location locateLargest(double[][] a) {
		Location lo = new Location();
		lo.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] > lo.maxValue) {
					lo.maxValue = a[i][j];
					lo.row = i;
					lo.column = j;
				}
		return lo;
	}
}
public class Demo {
	
    public static void main(String[] args) {
        
    	Scanner input = new Scanner(System.in);
    	System.out.print("Enter the number of rows and columns in the array: ");
    	int Row = input.nextInt();
    	int Column = input.nextInt();
    	System.out.println("Enter the array: ");
    	double array[][] = new double[Row + 2][Column + 2];
    	for(int i = 0; i < Row; i++)
    		for(int j = 0; j < Column; j++)
    			array[i][j] = input.nextDouble();
    	Location LO = new Location();
    	LO = LO.locateLargest(array);
    	System.out.print("The location of the largest element is " + LO.maxValue 
    			+ " at (" + LO.row + ", " + LO.column + ")");
    }
    
}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_碗碗儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值