编写矩形类Rectangle.类包括以下信息: 1)两个名为width和height的double型数据域(private),表示矩形的宽和高。width和height的默认值为1. 2)创建默认矩形的无参构造方法。 3)一个创建width和height为指定值日的矩形的构造方法。 4)一个名为getArea()方法返回这个矩形的面积 5)一个名为getPerimeter()的方法返回周长 在mian 方法里实现输出矩形的面积和周长。 输入要求 多组数据。宽,高,空格区分。 输出要求 宽,高,面积和周长,然后换行。空格分开,保留二位小数。 输入
1 2 2.0 3.5 12.3 4.5
输出
1.00 2.00 2.00 6.00 2.00 3.50 7.00 11.00 12.30 4.50 55.35 33.60
代码
import java.util.Scanner; public class zxzx { public static void main(String[] args) { Scanner input = new Scanner(System.in); while (input.hasNext()) { double width=input.nextDouble(); double high=input.nextDouble(); System.out.printf("%.2f ",width); System.out.printf("%.2f ",high); Rectangle r1 = new Rectangle(width,high); System.out.printf("%.2f ",r1.getArea()); System.out.printf("%.2f\n",r1.getPerimeter()); } } public static class Rectangle { private double high; private double width; public Rectangle(double high,double width) { this.high = high; this.width = width; } public Rectangle() { this.high = 1.0; this.width = 1.0; } public double getArea() { return high*width; } public double getPerimeter() { return (high+width)*2; } } }填空题: 建立一个类RegularPolygon表示正n边形。给类有以下要求: 1)私有数据域定义多边形的边数量 n (int型),缺省为 3 ; 2)私有数据域定义多边形的边长 side(double型) ,缺省为 1 ; 3)正多边形中心点坐标 x,y(double型,私有数据域) ,缺省为0 ; 4)三种构造函数 public RegularPolygon() public RegularPolygon(int numberOfSide,double side )
public RegularPolygon(int numberOfSide,double side,double x,double y )5)所有数据域的访问器和修改器 6)返回多边形周长的方法public double getPerimeter() 7)返回多边形面积的方法public double getArea() 面积公式 面积= nS2/(4tan(pi/n)) //s为边长
主函数,分别输出正多边形的周长和面积
输入要求 多组输入。 边数 边长 中心点x坐标 中心点x坐标 输出要求 边数 边长 周长(保留2位小数)面积(保留2位小数) 边数 边长 周长(保留2位小数)面积(保留2位小数) 边数 边长 周长(保留2位小数)面积(保留2位小数) 输入
3 4 0.0 0.0
输出
3 4.00 12.00 6.93 3 4.00 12.00 6.93 3 4.00 12.00 6.93
代码
import java.util.Scanner; public class RegularPolygon { private int n; private double side; private double x; private double y; public RegularPolygon(){ n = 3; side = 1.0; x = 0; y = 0; } public RegularPolygon(int newN, double newSide){ n = newN; side = newSide; x = 0; y = 0; } public RegularPolygon(int newN, double newSide, double newCenterX, double newCenterY){ n = newN; side = newSide; x = newCenterX; y = newCenterY; } public void setN(int newN){ n = newN; } public int getN(){ return n; } public void setSide(double newSide){ side = newSide; } public double getSide(){ return side; } public void setX(double newX){ x = newX; } public double getCenerX(){ return x; } public void setY(double newY){ y = newY; } public double getCenerY(){ return y; } public double getPerimeter(){ return n*side; } public double getArea(){ double area; area = (n * Math.pow(side, 2)) / (4 * Math.tan((Math.PI / n))); return area; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); while (input.hasNext()) { int n = input.nextInt(); double side = input.nextDouble(); double x = input.nextDouble(); double y = input.nextDouble(); // 三种方式构造正多边形,并输出其周长和面积 RegularPolygon rp1 = new RegularPolygon(); rp1.setN(n); rp1.setSide(side); rp1.setX(x); rp1.setY(y); RegularPolygon rp2 = new RegularPolygon(n, side); rp2.setX(x); rp2.setY(y); RegularPolygon rp3 = new RegularPolygon(n, side, x, y); System.out.printf("%d %.2f %.2f %.2f\n", rp1.getN(), rp1.getSide(), rp1.getPerimeter(), rp1.getArea()); System.out.printf("%d %.2f %.2f %.2f\n", rp2.getN(), rp2.getSide(), rp2.getPerimeter(), rp2.getArea()); System.out.printf("%d %.2f %.2f %.2f\n", rp3.getN(), rp3.getSide(), rp3.getPerimeter(), rp3.getArea()); } input.close(); } }使用日期类变现程序,设置流逝时间,然后用toString()显示上述日期。 输入要求 多组输入流逝时间。 输出要求 日期 输入
1000 10000 100000000 10000000000 1320000000000
输出
Thu Jan 01 08:00:01 CST 1970 Thu Jan 01 08:00:10 CST 1970 Fri Jan 02 11:46:40 CST 1970 Mon Apr 27 01:46:40 CST 1970 Mon Oct 31 02:40:00 CST 2011
代码
import java.util.Date; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); while (input.hasNext()) { long n=input.nextLong(); Date d=new Date(n); System.out.println(d); } } }参考课本P306(第11版 P313),设计一个名为StopWatch的类。
在主函数中验证你输入一个整数需要花费多久时间。 输入要求 任意一个整数 输出要求 输出花费的时间 输入
无
输出
无
tips:
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
StopWatch stopWatch = new StopWatch();
input.nextInt() ; //由于不方便评测,建议提交时注释本行
stopWatch.stop();
System.out.println(stopWatch.getElapsedTime());
input.close() ;
}
代码
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); StopWatch stopWatch = new StopWatch(); stopWatch.stop(); System.out.println(stopWatch.getElapsedTime()); input.close() ; } public static class StopWatch { private long startTime; private long endTime; StopWatch(){ startTime = System.currentTimeMillis(); } public void start(){ startTime = System.currentTimeMillis(); } public void stop(){ endTime = System.currentTimeMillis(); } public long getElapsedTime(){ return endTime - startTime; } } }写一个方阵类Matrix,完成以下任务: 1)具有一个带2维数组的构造方法public Matrix(double[][] m ) 2)具有一个获取方阵中心的方法public double[][] getCenter() 3)具有获得方阵所有数据之和的方法public double getSum() 注意: nn方阵的中心,如果n位奇数,中心是一个值,偶数时,中心是4个值,也就是一个22的小矩阵 在主方法里,验证构造函数,获取方阵的中心,和所有数据之和。 输入要求 多组数据。 n 分行输入方阵数据 输出要求 方阵元素之和(小数点后面保留两位) 方阵中心(每个数据保留小数点后面保留两位) 输入
-1 3 3 4 5 2 2 2 1 1 1
1 4 4 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4
输出
ERROR! 21.00 2.00
4.00 4.00
40.00 2.00 2.00 3.00 3.00
代码
import java.util.Scanner; public class main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int len = sc.nextInt(); if (len <= 0) System.out.println("ERROR!"); else { double[][] M = new double[len][len]; for (int i = 0; i < len; i++) for (int j = 0; j < len; j++) M[i][j] = sc.nextInt(); Matrix matrix = new Matrix(M); double sum = matrix.getSum(); System.out.printf("%.2f\n", sum); double[][] ans = matrix.getCenter(); for (int i = 0; i < ans.length; i++){ for (int j = 0; j < ans[0].length; j++) { System.out.printf("%.2f ", ans[i][j]); } System.out.println(); } } } } } class Matrix { double[][] m; public Matrix(double[][] m){ this.m=m; } public double[][] getCenter(){ if(m.length<=2) return m; else{ if(m.length%2==1){ double[][] ans=new double[1][1]; ans[0][0]=m[(m.length)/2][(m[0].length)/2]; return ans; } else{ double [][] ans=new double[2][2]; ans[0][0]=m[m.length/2-1][m[0].length/2-1]; ans[0][1]=m[m.length/2-1][m[0].length/2]; ans[1][0]=m[m.length/2][m[0].length/2-1]; ans[1][1]=m[m.length/2][m[0].length/2];return ans; } } } public double getSum(){ double sum=0; for(int i=0;i<m.length;i++){ for(int j=0;j<m[i].length;j++) sum+=m[i][j]; } return sum; } }