Java第五周

本文介绍了使用Java封装三角形、梯形和圆形等几何图形类的方法,包括初始化、修改属性、计算周长和面积等功能,并展示了如何进行矩阵操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生 
* All rights reserved.
* 文件名称:   Fifth                           
* 作    者:   姜雅明                              
* 完成日期:   2012 年  09  月  21  日
* 版 本 号:   2.0       

* 对任务及求解方法的描述部分
* 输入描述: 
* 问题描述: 
* 程序输出: 
* 程序头部的注释结束
*/

/* 1.封装一类三角形对象Triangle,该类对象具有三条边的属性,
 * 具有初始化三角形的功能、修改边长的功能、判断三条边能否构成三角形的功能、求周长的功能、求面积的功能。
 */

public class Triangle {

	private int line1;
	private int line2;
	private int line3;
	
	Triangle(int line1, int line2, int line3)
	{
		this.line1 = line1;
		this.line2 = line2;
		this.line3 = line3;
	}
	public int set_line1(int line1)
	{
		this.line1 = line1;
		System.out.println("the Triangle's first side has changed to " + this.line1);
		return this.line1;
	}
	public int set_line2(int line2)
	{
		this.line2 = line2;
		System.out.println("the Triangle's second side has changed to " + this.line2);
		return this.line2;
	}
	public int set_line3(int line3)
	{
		this.line3 = line3;
		System.out.println("the Triangle's tirth side has changed to " + this.line3);
		return this.line3;
	}
	public boolean isTriangle()
	{
		if(line1 + line2 > line3 && line2 + line3 > line1 && line1 + line3 > line2)
		{
			return true;
			//System.out.println("Congretulations!!!");
		}
		else
		{
			return false;
			//System.out.println("can not make a Triangle!!!");	
		}
	}
	public void girth()
	{
		if(isTriangle() == true)
		{
			int sum = line1 + line2 + line3;
			System.out.println("the gitrh of the Triangle is "+sum);
		}
		else
		{
			System.out.println("can not make a Triangle!!!");	
		}
	}
	public void area()
	{
		if(isTriangle() == true)
		{
			double x = 0, y = 0;
			x = (line1 + line2 + line3) / 2;
			y = Math.sqrt(x * (x - line1) * (x - line2) * (x - line3));
			System.out.println("the area of the Triangle is " + y);
		}
		else
		{
			System.out.println("can not make a Triangle!!!");	
		}
	}
	public void display()
	{
		System.out.println("the three sides of the Triangle is: " + line1 + " " + line2 + " " + line3);
	}
}


 

/* 2.封装一类梯形对象Ladder,该类对象具有上底、下底和高的属性,
 * 具有初始化梯形的功能、修改上底、下底和高的功能、求周长的功能、求面积的功能。
 */
public class Ladder {
	
	private int top;
	private int down;
	private int height;
	private int left_side;
	private int right_side;
	
	Ladder(int top, int down, int height, int left_side, int right_side)
	{
		this.top = top;
		this.down = down;
		this.height = height;
		this.left_side = left_side;
		this.right_side = right_side;
	}
	public int set_top(int top)
	{
		this.top = top;
		return this.top;
	}
	public int set_down(int down)
	{
		this.down = down;
		return this.down;
	}
	public int set_height(int height)
	{
		this.height = height;
		return this.height;
	}
	public int set_left_side(int left_side)
	{
		this.left_side = left_side;
		return this.left_side;
	}
	public int set_right_side(int right_side)
	{
		this.right_side = right_side;
		return this.right_side;
	}
	public void girth()
	{
		int sum = 0;
		sum = top + down + left_side + right_side;
		System.out.println("the girth of the Ladder is " + sum);
		//return sum;
	}
	public void area()
	{
		double sum = 0;
		sum = (top + down) * height /2;
		System.out.println("the area of the Ladder is " + sum);
	}
	public void display()
	{
		System.out.println("the top side is : " + top);
		System.out.println("the down side is : " + down);
		System.out.println("the height is : " + height);
		System.out.println("the left side is : " + left_side);
		System.out.println("the right side is : " + right_side);
	}
}


不知道怎么算两边,只好增加两条斜边的属性···

 

/* 3.封装一类圆形对象Circle,该类对象具有半径的属性,
 * 具有初始化圆的功能、修改半径的功能、求周长的功能、求面积的功能。
 */

public class Circle {

	private int r;
	
	Circle(int r)
	{
		this.r = r;
	}
	public int set_r(int r)
	{
		this.r = r;
		System.out.println("the radius of the Circle has changed to : " + this.r);
		return this.r;
	}
	public void gitth()
	{
		double g = 0;
		g = 2 * Math.PI * r;
		System.out.println("the girth of the Circle is : " + g);
	}
	public void area()
	{
		double a = 0;
		a = Math.PI * r * r;
		System.out.println("the area of the Circle is : " + a);
	}
}


测试:

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Triangle t = new Triangle(3, 4, 5);
		t.display();
		t.isTriangle();
		
		t.set_line1(6);
		t.isTriangle();
		
		t.girth();
		t.area();
		
		System.out.println();
		
		Ladder l = new Ladder(2, 3, 4, 6, 6);
		l.display();
		l.girth();
		l.area();
		
		System.out.println();
		
		Circle c = new Circle(2);
		c.set_r(3);
		c.gitth();
		c.area();
	}

}


 

/* 5.封装一类矩阵对象,该类对象具有初始化矩阵的功能、修改矩阵元素的功能。
 */

public class Matrix 
{
	private int row;  //矩阵的行数
	private int column;  //矩阵的列数
	private int [][] a;  
	
	Matrix(int row, int column)
	{
		this.row = row;
		this.column = column;
		
		a = new int [row][column];  //为矩阵开辟空间
		int i = 0, j = 0; 
		for(i = 0; i < row; i++)
		{
			for(j = 0; j < column; j++)
			{
				a[i][j] = 1;        //矩阵元素赋初值
			}
		}
	}
	
	public void display()  //输出矩阵
	{
		int i = 0, j = 0;
		for(i = 0; i < this.row; i++)  //开始用的row,但是出错。在之前加入了a = new [row][column],这样,赋初值就无用了。
		{
			for(j = 0; j < this.column; j++)
			{
				System.out.print(a[i][j] + " ");
			}
			System.out.println();
		}
	}
	
	public void change(int row, int column, int x) //修改矩阵元素
	{
		a[row][column] = x;
	}
	
	public void set_row(int row)  //设置矩阵行数
	{
		this.row = row;
		a = new int [row][column];  //重新开辟空间
                for(int i = 0; i < row; i++)
		{
			for(int j = 0; j < column; j++)
			{
				a[i][j] = 1;        //矩阵元素赋值
			}
		}
	}
	
	public void set_column(int column) //设置矩阵列数
	{
		this.column = column;
		a = new int [row][column];
                for(int i = 0; i < row; i++)
		{
			for(int j = 0; j < column; j++)
			{
				a[i][j] = 1;        //矩阵元素赋值
			}
		}
	}
	
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		Matrix m = new Matrix(2, 2);
		m.set_row(3);
		m.set_column(3);
		m.change(0, 0, 3);
		m.display();
	}

}


好吧,又是让人头疼的矩阵····

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值