<h2>3.1<span style="font-family:楷体_GB2312;">、实验目的</span></h2><p>l 进一步掌握类的定义和对象的创建</p><p>l 掌握访问控制符的关系(<span style="font-family:Times New Roman;">public</span><span style="font-family:宋体;">、</span><span style="font-family:Times New Roman;">priviate</span><span style="font-family:宋体;">、</span><span style="font-family:Times New Roman;">protect</span><span style="font-family:宋体;">)</span></p><p> </p><h2>3.2<span style="font-family:楷体_GB2312;">、实验准备</span></h2><p>(<span style="font-family:Times New Roman;">1</span><span style="font-family:宋体;">) </span><span style="font-family:Times New Roman;">JDK</span><span style="font-family:宋体;">安装</span></p><p>(<span style="font-family:Times New Roman;">2</span><span style="font-family:宋体;">) </span><span style="font-family:Times New Roman;">Eclipse</span><span style="font-family:宋体;">的安装</span></p><p>(<span style="font-family:Times New Roman;">3</span><span style="font-family:宋体;">) 书本第四章节的复习</span></p><p> </p><h2>3.3<span style="font-family:楷体_GB2312;">、实验内容与要求</span></h2><p>一、编写一个<span style="font-family:Times New Roman;">Java</span><span style="font-family:宋体;">应用程序,该程序中有</span><span style="font-family:Times New Roman;">3</span><span style="font-family:宋体;">个类:</span><span style="font-family:Times New Roman;">Trangle</span><span style="font-family:宋体;">、</span><span style="font-family:Times New Roman;">Ladder</span><span style="font-family:宋体;">和</span><span style="font-family:Times New Roman;">Circle</span><span style="font-family:宋体;">,分别用来刻画“三角形”、“梯形”和“圆形”。具体要求如下:</span><span style="font-family:Times New Roman;"> </span></p><p>a) Trangle<span style="font-family:宋体;">类具有类型为</span><span style="font-family:Times New Roman;">double</span><span style="font-family:宋体;">的三个边,以及周长、面积属性,</span><span style="font-family:Times New Roman;">Trangle</span><span style="font-family:宋体;">类具有返回周长、面积以及修改三个边的功能。另外,</span><span style="font-family:Times New Roman;">Trangle</span><span style="font-family:宋体;">类还具有一个</span><span style="font-family:Times New Roman;">boolean</span><span style="font-family:宋体;">型的属性,该属性用来判断三个属能否构成一个三角形。</span><span style="font-family:Times New Roman;"> </span></p><p>b) Ladder<span style="font-family:宋体;">类具有类型</span><span style="font-family:Times New Roman;">double</span><span style="font-family:宋体;">的上底、下底、高、面积属性,具有返回面积的功能。</span><span style="font-family:Times New Roman;"> </span></p><p>c) Circle<span style="font-family:宋体;">类具有类型为</span><span style="font-family:Times New Roman;">double</span><span style="font-family:宋体;">的半径、周长和面积属性,具有返回周长、面积的功能</span></p><p> </p><p>二、编写一个<span style="font-family:Times New Roman;">StringUtils</span><span style="font-family:宋体;">类,实现十进制到八进制的转换并输出。需要一个测试类加以测试。</span></p><p> </p>
import java.util.Scanner;
public class Mymain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
double l1,l2,l3;
while(true)
{
System.out.println("请输入三角形的三边长:");
l1=sc.nextDouble();
l2=sc.nextDouble();
l3=sc.nextDouble();
if(l1+l2>l3&&l1+l3>l2&&l2+l3>l1)
{
break;
}
}
Trangle sjx=new Trangle(l1,l2,l3);
System.out.println("三角形的周长:"+sjx.getPerimeter());
System.out.println("三角形的面积:"+sjx.getArea());
System.out.println("请输入梯形的上底,下底,高:");
l1=sc.nextDouble();
l2=sc.nextDouble();
l3=sc.nextDouble();
Ladder tx=new Ladder(l1,l2,l3);
System.out.println("梯形的面积:"+tx.getArea());
System.out.println("请输入园的半径:");
l1=sc.nextDouble();
Circle yuan=new Circle(l1);
System.out.println("园的周长:"+yuan.getPerimeter());
System.out.println("园的面积:"+yuan.getArea());
System.out.println("请输入一个10进制数:");
int val=sc.nextInt();
System.out.println("8进制表示:"+new StringUtils().change(val));
}
}
class Trangle
{
double l1,l2,l3;
Trangle(double l1,double l2,double l3)
{
this.l1=l1;
this.l2=l2;
this.l3=l3;
}
double getPerimeter()
{
return l1+l2+l3;
}
double getArea()
{
double p=(l1+l2+l3)/2;
return Math.sqrt(p*(p-l1)*(p-l2)*(p-l3));
}
}
class Ladder
{
double sd,xd,h;
Ladder(double sd,double xd,double h)
{
this.sd=sd;
this.xd=xd;
this.h=h;
}
double getArea()
{
return (sd+xd)*h/2;
}
}
class Circle
{
double r;
Circle(double r)
{
this.r=r;
}
double getPerimeter()
{
return 2*r*Math.PI;
}
double getArea()
{
return Math.PI*r*r;
}
}
class StringUtils
{
String change(int val)
{
String tem="";
while(val>0)
{
tem+=val%8;
val/=8;
}
if(tem.length()==0)
tem="0";
String re="";
for(int i=tem.length()-1;i>=0;i--)
{
re+=tem.charAt(i);
}
return re;
}
}