java程序设计(第二版)课后习题答案

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

//习题2.2
import java.util.*;
class MyDate{
private int year;
private int month;
private int day;
public MyDate(int y,int m,int d){//构造函数,构造方法
year=y;
month=m;
day=d;
}//end public MyDate(int y,int m,int d)
public int getYear(){//返回年
return year;
}//end getYear()
public int getMonth(){//返回月
return month;
}//end getMonth()
public int getDay(){//返回日
return day;
}//end getDay()
}//end class MyDate
class Employee{
private String name;
private double salary;
private MyDate hireDay;
public Employee(String n,double s,MyDate d){
name=n;
salary=s;
hireDay=d;
}//end public Employee(String n,double s,MyDate d)
public void print(){
System.out.println("名字:"+name+"\n工资:"+salary+"\n雇佣年份:"+hireYear()+"\n");
}//end print()
public void raiseSalary(double byPercent){
salary*=1+byPercent/100;
}//end
public int hireYear(){
return hireDay.getYear();
}
}//end class Employee
public class MyTestClass {
public static void main(String[] args) {
Employee[]staff=new Employee[3];
staff[0]=new Employee("Harry Hacker",35000,new MyDate(1989,10,1));
- 2 - staff[1]=new Employee("Carl Carcker",75000,new MyDate(1987,12,15)); staff[2]=new Employee("Tony T ester",38000,new MyDate(1990,3,12)); int integerValue;
System.out.println("The information of employee are:");
for(integerValue=0;integerValue<=2;integerValue++){
staff[integerValue].raiseSalary(5);
}//end for()
for(integerValue=0;integerValue<=2;integerValue++){
staff[integerValue].print();
}//end for()
}//end main() }//end class MyTestClass
//习题2.4
import java.util.*;
public class DataType {
public static void main(String[] args) {
boolean flag;
char yesChar;
byte finByte;
int intValue;
long longValue;
short shortValue;
float floatValue;
double doubleValue;
flag=true;
yesChar='y';
finByte=30;
intValue=-7000;
longValue=200l;
shortValue=20000;
floatValue=9.997E-5f;
doubleValue=floatValue*floatValue;
System.out.println("the values are:");
System.out.println("布尔类型变量flag="+flag);
System.out.println("字符型变量yesChar="+yesChar);
System.out.println("字节型变量finByte="+finByte);
System.out.println("整型变量intValue="+intValue);
System.out.println("长整型变量longValue="+longValue);
System.out.println("短整型变量shortValue="+shortValue);
System.out.println("浮点型变量floatValue="+floatValue);
System.out.println("双精度浮点型变量doubleValue="+doubleValue);
}//end main()
}
//习题2.9
import java.util.*;
class PubTest1{
private int ivar1;
private float fvar1,fvar2;
public PubTest1(){
fvar2=0.0f;
}
public float sum_f_I(){
fvar2=fvar1+ivar1;
return fvar2;
}
public void print(){
System.out.println("fvar2="+fvar2);
}
public void setIvar1(int ivalue){
ivar1=ivalue;
}
public void setFvar1(float ivalue){
fvar1=ivalue;
}
}
public class PubMainTest {
public static void main(String[] args) {
PubTest1 pubt1=new PubTest1();
pubt1.setIvar1(10);
pubt1.setFvar1(100.02f);
pubt1.sum_f_I();
pubt1.print();
}
}
//习题2.10
import java.util.*;
class Date {
private int year;
private int month;
private int day;
public Date(int day, int month, int year) { //构造函数,构造方法this.year = year;
this.month = month;
this.day = day;
} //end public MyDate(int y,int m,int d)
- 4 -
public int getYear() { //返回年
return year;
} //end getYear()
public int getMonth() { //返回月
return month;
} //end getMonth()
public int getDay() { //返回日
return day;
} //end getDay()
} //end class Date
public class Teacher {
String name;//教师名字
boolean sex;//性别,true 表示男性
Date birth;//出生日期
String salaryID;//工资号
String depart;//教师所在系所
String posit;//教师职称
String getName() {
return name;
}
void setName(String name) {
= name;
}
boolean getSex() {
return sex;
}
void setSex(boolean sex) {
this.sex = sex;
}
Date getBirth() {
return birth;
}
void setBirth(Date birth) {
this.birth = birth;
}
String getSalaryID() {
return salaryID;
}
void setSalaryID(String salaryID) {
this.salaryID = salaryID;
}
String getDepart() {
return depart;
}
void setDepart(String depart) {
this.depart = depart;
}
String getPosit() {
return posit;
}
void setPosit(String posit) {
this.posit = posit;
}
public Teacher(){
System.out.println("父类无参数的构造方法!!!!!!!");
}//如果这里不加上这个无参数的构造方法将会出错!!!!public Teacher(String name,boolean sex,Date birth,
String salaryid,String depart,String posit){ =name;
this.sex=sex;
this.birth=birth;
this.salaryID=salaryid;
this.depart=depart;
this.posit=posit;
}//end Teacher()
public void print(){
System.out.print("the teacher'name:");
System.out.println(this.getName());
System.out.print("the teacher'sex:");
if(this.getSex()==false){
System.out.println("女");
}
else{
System.out.println("男");
}
System.out.print("the teacher'birth:");
System.out.println(this.getBirth().getYear()+"-"+
this.getBirth().getMonth()+"-"+
this.getBirth().getDay()); System.out.print("the teacher'salaryid:");
System.out.println(this.getSalaryID());
System.out.print("the teacher'posit:");
System.out.println(this.getPosit());
System.out.print("the teacher'depart:");
System.out.println(this.getDepart());
}//end print()
public static void main(String[] args) {
Date dt1=new Date(11,23,1989);
Date dt2=new Date(2,6,1975);
- 6 -
Date dt3=new Date(11,8,1964);
Date dt4=new Date(10,4,1975);
Date dt5=new Date(8,9,1969);
//创建各系教师实例,用来测试
Teacher t1=new Teacher("王莹",false,dt1,"123","经济学","prefessor");
ResearchTeacher rt=new ResearchTeacher("杨zi 青",true,dt2,"421","软件工程", "associate prefessor","software"); LabTeacher lat=new LabTeacher("王夏瑾",false,dt3,"163","外语",
"pinstrucor","speech lab");
LibTeacher lit=new LibTeacher("马二孩",true,dt4,"521","大学物理",
"prefessor","physicalLib");
AdminTeacher at=new AdminTeacher("王xi",false,dt5,"663","环境",
"prefessor","dean");
/////////分别调用各自的输出方法,输出相应信息//////////////////////////// System.out.println("-------------------------------");
t1.print();//普通教师信息
System.out.println("-------------------------------");
rt.print();//研究系列教师信息
System.out.println("-------------------------------");
lat.print();//普通教师信息
System.out.println("-------------------------------");
lit.print();//实验系列教师信息
System.out.println("-------------------------------");
at.print();//行政系列教师信息
System.out.println("-------------------------------");
}//end main()
}//end public class Teacher
class ResearchT eacher extends Teacher{
private String resField;
public ResearchT eacher(String name, boolean sex, Date birth, String salaryid, String depart, String posit, String resField) {
= name;
this.sex = sex;
this.birth = birth;
this.salaryID = salaryid;
this.depart = depart;
this.posit = posit;
this.resField = resField;
} //end public ResearchTeacher(){}
String getResField(){
return resField;
}
void setResField(String resField){
this.resField=resField;
}
public void print() {
System.out.print("research teacher info is:");
System.out.print("the teacher'name:");
System.out.println(this.getName());
System.out.print("the teacher'sex:");
if (this.getSex() == false) {
System.out.println("女");
}
else {
System.out.println("男");
}
System.out.print("the teacher'birth:");
System.out.println(this.getBirth().getYear() + "-" +
this.getBirth().getMonth() + "-" +
this.getBirth().getDay());
System.out.print("the teacher'salaryid:");
System.out.println(this.getSalaryID());
System.out.print("the teacher'posit:");
System.out.println(this.getPosit());
System.out.print("the teacher'depart:");
System.out.println(this.getDepart());
System.out.print("the teacher'resField:");
System.out.println(this.getResField());
} //end print()
}//end class ResearchTeacher
class LabTeacher extends T eacher{
private String labName;
public LabTeacher(String name, boolean sex, Date birth,
String salaryid, String depart,
String posit, String labName) { = name;
this.sex = sex;
this.birth = birth;
this.salaryID = salaryid;
this.depart = depart;
this.posit = posit;
bName = labName;
} //end public ResearchTeacher(){}
String getLabName(){
return labName;
}
void setLabName(String labName){
- 8 -
bName=labName;
}
public void print() {
System.out.print("lab teacher info is:");
System.out.print("the teacher'name:");
System.out.println(this.getName());
System.out.print("the teacher'sex:");
if (this.getSex() == false) {
System.out.println("女");
}
else {
System.out.println("男");
}
System.out.print("the teacher'birth:");
System.out.println(this.getBirth().getYear() + "-" +
this.getBirth().getMonth() + "-" +
this.getBirth().getDay());
System.out.print("the teacher'salaryid:");
System.out.println(this.getSalaryID());
System.out.print("the teacher'posit:");
System.out.println(this.getPosit());
System.out.print("the teacher'depart:");
System.out.println(this.getDepart());
System.out.print("the teacher'labName:");
System.out.println(bName);
} //end print()
}//end class LabTeacher
class LibTeacher extends Teacher{
private String libName;
public LibTeacher(String name,boolean sex,Date birth,
String salaryid,String depart,
String posit,String libName){
= name;
this.sex = sex;
this.birth = birth;
this.salaryID = salaryid;
this.depart = depart;
this.posit = posit;
this.libName=libName;
}//end public ResearchT eacher(){}
String getLibName(){
return libName;
}
void setLibName(String libName){
this.libName=libName;
}
public void print() {
System.out.print("lib teacher info is:");
System.out.print("the teacher'name:");
System.out.println(this.getName());
System.out.print("the teacher'sex:");
if (this.getSex() == false) {
System.out.println("女");
}
else {
System.out.println("男");
}
System.out.print("the teacher'birth:");
System.out.println(this.getBirth().getYear() + "-" +
this.getBirth().getMonth() + "-" +
this.getBirth().getDay());
System.out.print("the teacher'salaryid:");
System.out.println(this.getSalaryID());
System.out.print("the teacher'posit:");
System.out.println(this.getPosit());
System.out.print("the teacher'depart:");
System.out.println(this.getDepart());
System.out.print("the teacher'libName:");
System.out.println(this.libName);
} //end print()
}//end class LibTeacher
class AdminTeacher extends Teacher{
private String managePos;
public AdminTeacher(String name,boolean sex,Date birth,
String salaryid,String depart,
String posit,String managePos){ = name;
this.sex = sex;
this.birth = birth;
this.salaryID = salaryid;
this.depart = depart;
this.posit = posit;
this.managePos=managePos;
- 10 -
}//end public ResearchT eacher(){}
String getManagePos(){
return managePos;
}
void setManagePos(String managePos){
this.managePos=managePos;
}
public void print() {
System.out.print("adminteacher info is:");
System.out.print("the teacher'name:");
System.out.println(this.getName());
System.out.print("the teacher'sex:");
if (this.getSex() == false) {
System.out.println("女");
}
else {
System.out.println("男");
}
System.out.print("the teacher'birth:");
System.out.println(this.getBirth().getYear() + "-" +
this.getBirth().getMonth() + "-" +
this.getBirth().getDay());
System.out.print("the teacher'salaryid:");
System.out.println(this.getSalaryID());
System.out.print("the teacher'posit:");
System.out.println(this.getPosit());
System.out.print("the teacher'depart:");
System.out.println(this.getDepart());
System.out.print("the teacher'managePos:");
System.out.println(this.managePos);
} //end print() }//end class AdminTeacher
习题2.11
public class Course {
private String courseID;
private String courseName;
private String courseType;
private int classHour;
private float credit;
public Course(String courseID, String courseName, String courseType, int classHour, float credit) {
this.courseID=courseID;
this.courseName=courseName; this.courseType=courseType; this.classHour=classHour; this.credit=credit;
}//end public Course(){}
String getID() {
return courseID;
}
void setID(String id) {
this.courseID = id;
}
String getName() {
return courseName;
}
void setName(String name) { this.courseName = name;
}
String getType() {
return courseType;
}
void setType(String type) { this.courseType = type;
}
int getClassHour() {
return classHour;
}
void setClassHour(int hour) { this.classHour = hour;
}
float getCredit() {
return classHour;
}
void setCredit(float credit) { this.credit= credit;
}
- 12 -
public void print(){
System.out.println("the basic info of this course as followed:"); System.out.println("courseID="+this.getID());
System.out.println("courseName="+this.getName());
System.out.println("courseType="+this.getType());
System.out.println("classHour="+this.getClassHour());
System.out.println("credit="+this.getCredit());
}
public static void main(String[] args) {
Course cs=new Course("d12","java 程序设计(第二版)","cs",64,3.0f); System.out.println("----------------------------------");
cs.print();
System.out.println("修改课程学分为4.0f");
cs.setCredit(4);
cs.print();
} }
//习题2.12
public class MyGraphic {
String lineColor;
String fillColor;
MyGraphic(String lc,String fc){
this.lineColor=lc;
this.fillColor=fc;
}
void print(){
System.out.println("line color is "+this.lineColor+
"\t fill color is "+this.fillColor);
}
public static void main(String[] args) {
float rd=(float)4.5;
MyCircle mc=new MyCircle(rd,"black","white");
MyRectangle mr=new MyRectangle(4,6,"red","blue");
System.out.println("Circle info ");
mc.print();
System.out.println("circumference is " + mc.calCircum());
System.out.println("square is " + mc.calSquare());
System.out.println("rectangle info: ");
mr.print();
System.out.println("circumference is " + mr.calCircum());
System.out.println("square is " + mr.calSquare()); }//end main(){}
}//end public class MyGraphic
class MyRectangle extends MyGraphic{
float rLong;
float rWidth;
MyRectangle (float rl,float rw,String lc,String fc){
super(lc,fc);
this.rLong=rl;
this.rWidth=rw;
}//end MyRectangle (){}
float calCircum(){
return ((float)((this.rLong+this.rWidth)*2));
}
float calSquare(){
return ((float)(this.rLong*this.rWidth));
}
}//end class MyRectangle
class MyCircle extends MyGraphic{
float radius;
MyCircle (float rd,String lc,String fc){
super(lc,fc);
this.radius=rd;
}//end MyRectangle (){}
float calCircum(){
return (float)((this.radius*3.12*2));
}
float calSquare(){
return ((float)(this.radius*this.radius*3.14));
}
}//end class MyCircle
//习题2.13
public class Vehicle {
String brand;
String color;
int price;
int number;
- 14 -
public Vehicle(String b, String c) {
this.brand = b;
this.color = c;
}
public Vehicle(String b, String c, int p, int n) {
this(b, c);
this.price = p;
this.number = n;
}
void print() {
System.out.println("\n-------------------------");
System.out.println("the vehicle info as followed :");
System.out.println("brand=" + this.brand + "\t");
System.out.println("color=" + this.color + "\t");
System.out.println("price=" + this.price + "\t");
System.out.println("number=" + this.number + "\t");
} //end void print()
public static void main(String[] args) {
Vehicle c1=new Vehicle("vehicle1","white");
Vehicle c2=new Vehicle("vehicle2","white",300,1);
Car cr=new Car("car1","red",300,4,400);
Truck tk2=new Truck("truck1","black",300,400);
c1.print();
c2.print();
cr.print();
tk2.print();
} //end main()
} //end public class Vehicle
class Car extends Vehicle{
int speed;
Car(String b, String c, int p, int n,int s){
super(b,c,p,n);
this.speed=s;
}
void print(){
super.print();
System.out.print("speed="+this.speed);
}
}//end class Car
class Truck extends Vehicle{
int weight;
Truck(String b, String c, int s,int w){
super(b,c);
this.speed=s;
this.weight=w;
}
void print(){
super.print();
System.out.print("speed="+this.speed);
System.out.print("weight="+this.weight); }
}//end class Truck
//习题3.3
public class Test {
public static void main(String[] args) {
int b1=1;
int b2=1;
System.out.println("b1=" + b1);
System.out.println("b2=" + b2);
b1<<=31;
b2<<=31;
System.out.println("b1=" + b1);
System.out.println("b2=" + b2);
b1 >>= 31;
System.out.println("b1=" + b1);
b1 >>= 1;
System.out.println("b1=" + b1);
b2 >>>= 31;
System.out.println("b2=" + b2);
b2 >>>= 1;
System.out.println("b2=" + b2);
}
}
//习题3.4
public class Factorial {
private int result,initVal;
public static int Factorial(int n){
if(n==0){
- 16 -
}
return n*Factorial(n-1);
}
public void print(){
System.out.println(initVal+"!="+result);
}
public void setInitVal(int n){
initVal=n;
}
public static void main(String[] args) {
Factorial ff=new Factorial();
for(int i=0;i<=4;i++){
ff.setInitVal(2*(i+1));
ff.result=Factorial(ff.initVal);
ff.print();
}//end for()
}//end main()
}//end public class Factorial
public class Factorial2 {
private int result,initVal;
public void print(){
System.out.println(initVal+"!="+result);
}
public void setInitVal(int n){
initVal=n;
}
public static void main(String[] args) {
Factorial2 ff=new Factorial2();
for(int i=0;i<=4;i++){
ff.setInitVal(2*(i+1));
ff.result=1;
for(int j=2;j<=ff.initVal;j++){
ff.result*=j;
}
ff.print();
}//end for()
}//end main() }
//习题3.5
public class MathRandomTest {
public static void main(String[] args) {
int count=0,MAXof100,MINof100;
int num,i;
MAXof100=(int)(100*Math.random());
MINof100=(int)(100*Math.random());
System.out.print(MAXof100+" ");
System.out.print(MINof100+" ");
if(MAXof100>50)count++;
if(MINof100>50)count++;
if( MAXof100<MINof100){
num=MINof100;
MINof100=MAXof100;
MAXof100=num;
}//end if()
for(i=0;i<98;i++){
num=(int)(100*Math.random());
System.out.print(num+((i+2)%10==9?"\n":" "));
if(num>MAXof100){
MAXof100=num;
}
else if(num<MINof100){
MINof100=num;
}
if(num>50){
count++;
}
}//end for()
System.out.println("the max of 100 random integers is "+MAXof100);
System.out.println("the min of 100 random integers is "+MINof100);
System.out.println("the number of random more than50 is "+count); }//end main()
}//end public class MathRandomTest
//习题3.7
public class PrintAst {
public void printAstar() {
System.out.print("*");
}
public void printSpace() {
System.out.print(" ");
}
- 18 -
public static void main(String[] args) {
PrintAst pa = new PrintAst();
int initNum = 13;
for (int i = 1; i <= initNum / 2 + 1; i++) {
for (int n = 1; n <= i; n++) {
pa.printSpace();
pa.printSpace();
}
for (int m = 1; m <= initNum - 2 * i + 2; m++) {
pa.printSpace();
pa.printAstar();
}
System.out.println();
} //end for
if (initNum % 2 == 0) {
for (int i = 1; i <= initNum / 2; i++) {
pa.printSpace();
pa.printSpace();
}
pa.printSpace();
pa.printAstar();
pa.printSpace();
pa.printAstar();
System.out.println();
}
for (int i = initNum / 2 + 2; i <= initNum; i++) {
for (int n = 1; n <= initNum - i + 1; n++) {
pa.printSpace();
pa.printSpace();
}
for (int m = 1; m <= 2 * i - initNum; m++) {
pa.printSpace();
pa.printAstar();
}
System.out.println();
} //end for
System.out.println();
} //end main()
} //end public class PrintAst
//习题3.8
public class PrintTriag {
public void printAstar() {
System.out.print("*");
}
public static void main(String[] args) {
int initLine = 10;
int initNum = 10;
PrintTriag pt = new PrintTriag();
for (int i = 0; i < initLine; i++) {
for (int j = 0; j < initNum - i; j++) {
pt.printAstar();
}
System.out.println();
}
}//end main()
}//end public class PrintTriag
习题3.9
import java.util.*;
public class MultipleT able {
public void printFormula(int i,int j,int res){ System.out.print(i+"*"+j+"="+res+" "); }
public static void main(String[] args) {
MultipleT able mt=new MultipleT able();
int initNum=9;
int res=0;
for(int i=1;i<=initNum;i++){
for(int j=1;j<=i;j++){
res=i*j;
mt.printFormula(i,j,res);
}
System.out.println();
}//end for
}//end main()
- 20 - }//end public class MultipleT able
习题3,10
import java.io.*;
public class HuiWen {
boolean isHuiWen(char str[], int n) {
int net = 0;
int i, j;
for (i = 0, j = n - 1; i < n / 2; i++, j--) {
if (str[i] == str[j]) {
net++;
} //end if
} //end for
if (net == (int) (n / 2)) {
return true;
} //end if
else {
return false;
}
} //end boolean isHuiWen(char str[], int n)
public static void main(String[] args) {
HuiWen hw1 = new HuiWen();
String pm = "";
try {
InputStreamReader reader = new InputStreamReader(System.in); BufferedReader input = new BufferedReader(reader);
System.out.print("give your test string:\n");
pm = input.readLine();
System.out.println(pm);
} //end try
catch (IOException e) {
System.out.print(e);
} //end catch
boolean bw = hw1.isHuiWen(pm.toCharArray(), pm.length()); if (bw == true) {
System.out.println("是回文");
}
else {
System.out.println("不是回文");
}
} //end main()
} //end public class HuiWen
import java.io.*;
public class HuiWen2 {
String reverse(String w1){
String w2;
char[]str1=w1.toCharArray();
int len=w1.length();
char[]str2=new char[len];
for(int i=0;i<len;i++){
str2[i]=str1[len-1-i];
}
w2=new String(str2);
return w2;
}
public static void main(String[] args) {
HuiWen2 hw1 = new HuiWen2();
String pm = "";
try {
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
System.out.print("give your test string:\n");
pm = input.readLine();
} //end try
catch (IOException e) {
System.out.print(e);
} //end catch
String w2=hw1.reverse(pm);
if(pareTo(pm)==0){
System.out.println("是回文");
}
else {
System.out.println("不是回文");
}
}
}
//习题3.11
import java.io.*;
public class PrimeNumber {
- 22 -
private int pm;
public void setPm(int pm){
this.pm=pm;
}
public boolean isPrime(){
boolean bl=true;
int i=2;
for(i=2;i<=Math.sqrt(pm);){
if(pm%i==0){
bl=false;
break;
}
else{
i++;
}
}//end for
return bl;
}//end public boolean isPrime()
public static void main(String[] args) {
PrimeNumber prim=new PrimeNumber();
int testNum=0;
try{
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
System.out.print("give your test number:\n");
testNum=Integer.parseInt(input.readLine());
}//end try
catch(IOException e){
System.out.println(e);
}//end catch
prim.setPm(testNum);
boolean bl=prim.isPrime();
if(bl==true){
System.out.println(testNum+"是质数");
}
else {
System.out.println(testNum+"不是质数");
}
}//end main }//end public class PrimeNumber
习题3.12
import java.io.*;
public class Tempconverter {
double celsius(double y){
return ((y-32)/9*5);
}
public static void main(String[] args) {
Tempconverter tc=new Tempconverter ();
double tmp=0;
try{
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
System.out.print("give your fahrenheit number:\n");
tmp=Double.parseDouble(input.readLine());
}//end try
catch(NumberFormatException e){
System.out.println(e);
}//end catch
catch(IOException e){
System.out.println(e);
}//end catch
System.out.println("the celsius of temperature is "+tc.celsius(tmp));
}//end main()
}//end public class Tempconverter
习题3.13
import java.io.*;
public class Trigsquare {
double x, y, z;
Trigsquare(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
boolean isTriangle() {
boolean bl = false;
if (this.x > 0 && this.y > 0 && this.z > 0) {
if ( (this.x + this.y) > this.z && (this.x + this.z) > this.y && (this.z + this.y) > this.x) {
bl = true;
} //ebd if
else {
bl = false;
} //end else
} //end if(this.x>0&&this.y>0&&this.z>0)
return bl;
} //end boolean isTriangle()
double getArea() {
double s = (this.x + this.y + this.z) / 2.0;
return (Math.sqrt(s * (s - this.x) * (s - this.y) * (s - this.z)));
} //end double getArea()
public static void main(String[] args) {
double s[] = new double[3];
try {
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
System.out.print("输入三角形的三边的长度:\n");
for (int i = 0; i <= 2; i++) { //输入三个数
s[i] = Double.parseDouble(input.readLine());
} //end for
} //end try
catch (NumberFormatException e) {
System.out.println("format error!!!");
} //end catch
- 24 - catch (IOException e) {
System.out.print(e);
} //end catch
Trigsquare ts = new Trigsquare(s[0], s[1], s[2]);
if (ts.isTriangle() == true) {
System.out.println("三角形的面积是" + ts.getArea());
} //end if
else {
System.out.println("输入的三边不能组成三角形!!");
} //end else
} //end main()
} //end public class Trigsquare
//习题3.14
import java.util.*;
import java.io.*;
import java.util.Date;
import java.text.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar.*;。

相关文档
最新文档