0801
编写矩形类Rectangle.类包括以下信息:
1)两个名为width和height的double型数据域(private),表示矩形的宽和高。width和height的默认值为1.
2)创建默认矩形的无参构造方法。
3)一个创建width和height为指定值日的矩形的构造方法。
4)一个名为getArea()方法返回这个矩形的面积
5)一个名为getPerimeter()的方法返回周长
在mian 方法里实现输出矩形的面积和周长。
输入要求:
多组数据。宽,高,空格区分。
输出要求:
宽,高,面积和周长,然后换行。空格分开,保留二位小数。
数据示例1:
输入:
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
思路提示:
主函数中创建一个Rectangle对象
Rectangle rectangle = new Rectangle(width,height);
import java.util.Scanner;
public class oj_0801 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
while (input.hasNext()){
double width=input.nextDouble();
double height=input.nextDouble();
Rectangle rectangle = new Rectangle(width,height);
width=rectangle.width;
height=rectangle.height;
double area=rectangle.getArea();
double perimeter=rectangle.getPerimeter();
System.out.printf("%.2f %.2f %.2f %.2f\n",width,height,area,perimeter);
}
}
static class Rectangle{
private double width=1;
private double height=1;
// private double
public Rectangle(double width, double height) {
this.width=width;this.height=height;
}
double getArea(){
return width*height;
}
double getPerimeter(){
return 2*(width+height);
}
}
}
0802
使用日期类变现程序,设置流逝时间,然后用toString()显示上述日期。
输入要求:
多组输入流逝时间。
输出要求:
日期
数据示例1:
输入:
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 oj_0802 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
while (input.hasNext()){
long t=input.nextLong();
Date d=new Date(t);
System.out.println(d);
}
}
}
0803
参考课本P306(第11版 P313),设计一个名为StopWatch的类。
在主函数中验证你输入一个整数需要花费多久时间。
输入要求:
任意一个整数
输出要求:
输出花费的时间
数据示例1:
输入:
无
输出:
无
思路提示:
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.Date;
import java.util.Scanner;
public class oj_0803 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// while (input.hasNext()) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
int n = input.nextInt();
// input.nextInt() ; //由于不方便评测,建议提交时注释本
stopWatch.stop();
System.out.println(stopWatch.getElapsedTime());
input.close();
// }
}
//类内的类 把它放到外面 建立一个stopwatch。
public static class StopWatch{
private long starTime;
private long stopTime;
public void start(){
starTime=System.currentTimeMillis();
//startime=new Date;
}
public void stop() {
stopTime=System.currentTimeMillis();
//endtime=new Date;
}
public long getElapsedTime(){
//return endtime.gettime- startime.gettime;
return stopTime-starTime;
}
}
}
0804
写一个方阵类Matrix,完成以下任务:
1)具有一个带2维数组的构造方法public Matrix(double[][] m )
2)具有一个获取方阵中心的方法public double[][] getCenter()
3)具有获得方阵所有数据之和的方法public double getSum()
注意:
nn方阵的中心,如果n位奇数,中心是一个值,偶数时,中心是4个值,也就是一个22的小矩阵
在主方法里,验证构造函数,获取方阵的中心,和所有数据之和。
输入要求:
多组数据。
n
分行输入方阵数据
输出要求:
方阵元素之和(小数点后面保留两位)
方阵中心(每个数据保留小数点后面保留两位)
数据示例1:
输入:
-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 oj_0804 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
while (input.hasNext()){
int n=input.nextInt();
if(n<=0) System.out.println("ERROR!");
else {
double[][] a=new double[n][n];
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
a[i][j]=input.nextDouble();
}
}
Matrix matrix=new Matrix(a);
double sum=matrix.getSum();
// a[1][1]=100;
System.out.printf("%.2f\n",sum);
double[][] cent=matrix.getCenter();
for (int i=0;i<cent.length;i++){
for (int j=0;j<cent.length;j++){
System.out.printf("%.2f ",cent[i][j]);
}
System.out.println();
}
}
}
}
static class Matrix {
double[][] m;
public Matrix( double[][] m ){
this.m = m;//不好,如果main里的m改变会跟着改变
//建议用复制
}
public double[][] getCenter () {
// double[][] cent=new double[][];
if(m.length<=2) return m;
else {
if(m.length%2==1){
double[][] cent=new double[1][1];
cent[0][0]=m[m.length/2][m[0].length/2];
return cent;
}
else {
double[][] cent=new double[2][2];
cent[0][0]=m[m.length/2-1][m[0].length/2-1];
cent[0][1]=m[m.length/2-1][m[0].length/2];
cent[1][0]=m[m.length/2][m[0].length/2-1];
cent[1][1]=m[m.length/2][m[0].length/2];
return cent;
}
}
}
public double getSum () {
double sum=0;
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m.length; j++) {
sum+=m[i][j];
}
}
return sum;
}
}
}
0805
编写向量类MyVector,完成以下任务:
1)使用一个一维数组构造一个向量类(元素类型为double)
2)实现向量的点积 MyVector dot(MyVector v2) ,x·y = (x0*y0,x1*y1,…,xn-1*yn-1)
3) 实现向量的加法 MyVector add(MyVector v2), x+y = (x0+y0,x1+y1,…,xn-1+yn-1)
4) 实现向量的String toString()方法;输出形式(x0,x1,…,xn-1),每个元素保留2位小数
5) 向量中一个私有变量double[] array,表示向量数据
6) 向量中一个私有变量int n,表示维度
写主方法里面,验证以上方法。
输入两个长度一致的数组,构造两向量,向量进行点积和求和运算,并输出结果
输入要求:
多组数据
长度n
数组1各元素
数组2各元素
输出要求:
点积后的向量结果//注意逗号后有个空格
向量相加后的结果//注意逗号后有个空格
数据示例1:
输入:
3
1 2 3
3 2 1
5
2.0 3.0 1 2 3
1 2 3 2.0 3.0
输出:
(3.00, 4.00, 3.00)
(4.00, 4.00, 4.00)
(2.00, 6.00, 3.00, 4.00, 9.00)
(3.00, 5.00, 4.00, 4.00, 6.00)
思路提示:
double[] v1,v2;
。。。。
MyVector myV1 = new MyVector(v1);
MyVector myV2 = new MyVector(v2);
MyVector myV3 = myV1.dot(myV2);
MyVector myV4 = myV1.add(myV2);
System.out.println(myV3.toString());
System.out.println(myV4.toString());
import java.text.DecimalFormat;
import java.util.Scanner;
public class oj_0805 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
while (input.hasNext()){
int n=input.nextInt();
double[] v1=new double[n];
double[] v2=new double[n];
for(int i=0;i<n;i++)
v1[i]=input.nextDouble();
for(int i=0;i<n;i++)
v2[i]=input.nextDouble();
MyVector myV1 = new MyVector(v1);
MyVector myV2 = new MyVector(v2);
MyVector myV3 = myV1.dot(myV2);
MyVector myV4 = myV1.add(myV2);
System.out.println(myV3.toString());
System.out.println(myV4.toString());
}
}
static class MyVector{
private double[] array;
private int n;
public MyVector(double[] v2) {
this.array = v2;
this.n = v2.length;
}
MyVector dot(MyVector v2){
double[] n=new double[v2.n];
for (int i=0;i<n.length;i++){
n[i]=this.array[i]*v2.array[i];
}
MyVector ne=new MyVector(n);
return ne;
}
MyVector add(MyVector v2){
double[] n=new double[v2.n];
for (int i=0;i<n.length;i++){
n[i]=this.array[i]+v2.array[i];
}
MyVector ne=new MyVector(n);
return ne;
}
public String toString(){
String b=new String();
b+="(";
DecimalFormat df=new DecimalFormat("0.00");
for (int i=0;i<this.n;i++){
if(i<this.n-1)
b+=df.format(this.array[i])+", ";
else if(i==this.n-1)
b+=df.format(this.array[i])+")";
}
return b;
}
}
}