java语言程序设计基础篇第八章1,5,9,12习题答案

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shuishanshu30/article/details/80526461

8.1

package Ch08;



public class Ch0801 {


public static void main(String[] args) {
Rectangle oneRectangle = new Rectangle(4 , 40);
System.out.println("The oneRectangle's width is: "+oneRectangle.width);
System.out.println("The oneRectangle's height is: "+oneRectangle.height);
System.out.println("The oneRectangle's area is: "+oneRectangle.getArea());
System.out.println("The oneRectangle's perimeter is: "+oneRectangle.getPerimeter());
System.out.println("");
Rectangle twoRectangle = new Rectangle(3.5 , 35.9);
System.out.println("The twoRectangle's width is: "+twoRectangle.width);
System.out.println("The twoRectangle's height is: "+twoRectangle.height);
System.out.println("The twoRectangle's area is: "+twoRectangle.getArea());
System.out.println("The twoRectangle's perimeter is: "+twoRectangle.getPerimeter());


}

}


class Rectangle{
double width=1,height=1;
public Rectangle() {
}
public Rectangle(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return (width + height) * 2;
}
}

8.5

package Ch08;


import java.util.GregorianCalendar;
public class Ch0805 {


public static void main(String[] args) {
GregorianCalendar time = new GregorianCalendar();
System.out.println("The time now");
System.out.println("Year is " + time.get(GregorianCalendar.YEAR));
   System.out.println("Month is " + (time.get(GregorianCalendar.MONTH)+1));
   System.out.println("Date is " + time.get(GregorianCalendar.DATE));
   time.setTimeInMillis(1234567898765L);
   System.out.println("The time after definnition");
   System.out.println("Year is " + time.get(GregorianCalendar.YEAR));
   System.out.println("Month is " + time.get(GregorianCalendar.MONTH));
   System.out.println("Date is " + time.get(GregorianCalendar.DATE));
}


}

8.9

package Ch08;


public class Ch0809 {


public static void main(String[] args) {
RegularPolygon pol1 = new RegularPolygon();
   RegularPolygon pol2 = new RegularPolygon(6, 4);
   RegularPolygon pol3 = new RegularPolygon(10, 4, 5.6, 7.8);
   System.out.println("Polygon1's perimeter is : " + pol1.getPerimeter());
   System.out.println("Polygon1's area is : " + pol1.getArea());
   System.out.println("Polygon2's perimeter is : " + pol2.getPerimeter());
   System.out.println("Polygon2's area is : " + pol2.getArea());
   System.out.println("Polygon3's perimeter is : " + pol3.getPerimeter());
   System.out.println("Polygon3's area is : " + pol3.getArea());
}
}
class RegularPolygon{
private int n = 3;
private double side = 1;
private double x=0;
private double y=0;

public RegularPolygon() {
}
public RegularPolygon(int num,double newSide) {
n=num;
side=newSide;
}
public RegularPolygon(int num, double newSide, double newX, double newY) {
   n = num;
   side = newSide;
   x = newX;
   y = newY;
 }
 public int getN() {
   return n;
 }
 public void setN(int number) {
   n = number;
 }


 public double getSide() {
   return side;
 }
 public void setSide(double newSide) {
   side = newSide;
 }


 public double getX() {
   return x;
 }
 public void setX(double newX) {
   x = newX;
 }
 
 public double getY() {
   return y;
 }
 public void setY(double newY) {
   y = newY;
 }
 
 public double getPerimeter() {
   return n * side;
 }


 public double getArea() {
   return n * side * side / Math.tan(Math.PI / n) / 4;
 } 
}

8.12

package Ch08;


import java.util.Scanner;
public class Ch0812 {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
   
   System.out.print("Enter the endpoints of the first line segment: ");
   double x1 = input.nextDouble();
   double y1 = input.nextDouble();
   double x2 = input.nextDouble();
   double y2 = input.nextDouble();
   
   System.out.print("Enter the endpoints of the second line segment: ");
   double x3 = input.nextDouble();
   double y3 = input.nextDouble();
   double x4 = input.nextDouble();
   double y4 = input.nextDouble();
   
   
   double a=y1-y2;
double b=x2-x1;
double e=-y1*(x1-x2)+(y1-y2)*x1;
double c=y3-y4;
double d=x4-x3;
double f=-y3*(x3-x4)+(y3-y4)*x3;

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

if (line.isSolvable()) {
     System.out.println("The intersecting point is: (" +
     line.getX() + ", " + line.getY() + ")");
   }
   else {
     System.out.println("The two lines is non-intersect");
   }
}


}
class LinearEquation {
 private double a;
 private double b;
 private double c;
 private double d;
 private double e;
 private double f;


 public LinearEquation(double newA, double newB, double newC,
     double newD, double newE, double newF) {
   a = newA;
   b = newB;
   c = newC; 
   d = newD;
   e = newE;
   f = newF; 
 }
 
 double getA() {
   return a;
 }


 double getB() {
   return b;
 }


 double getC() {
   return c;
 }


 double getD() {
   return d;
 }


 double getE() {
   return e;
 }


 double getF() {
   return f;
 }


 boolean isSolvable() {
   return a*d-b*c!=0;
 }


 double getX() {
   double x=(e*d - b*f) / (a*d - b*c);
   return x; 
 }


 double getY() {
   double y = (a*f - e*c) / (a*d - b*c);
   return y;
 }
}

展开阅读全文

没有更多推荐了,返回首页