赞
踩
评分说明
该次课程考试,有六个编程题,重点考察同学们基于面向对象的编程方法与应用。注意,评分是按知识点是否作对给定成绩的。
六个题的评分说明如下:
分为3个知识点:
1、5分——类的属性(可以只有半径,也可以含有圆心)、类的构造方法(包括默认、带参数的),以及每个属性的set/get方法。
2、5分——计算周长、面积的方法。
3、5分——测试类
package t1;
public class Yuan {
private double x,y;
private double r;
public Yuan(double x, double y, double r) {
super();
this.x = x;
this.y = y;
this.r = r;
}
public Yuan() {
super();
// TODO Auto-generated constructor stub
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public double mj(){
return 3.14*r*r;
}
public double zc(){
return 2*r*3.14;
}
public String pp(){
return "圆心:("+x+","+y+") 半径:"+r;
}
}
package t1;
public class Test {
public static void main(String[] args) {
Yuan y1=new Yuan(10,20,30);
System.out.println(y1.pp()+" 面积="+y1.mj()+" 周长="+y1.zc());
}
}
1、10分——方程类
(1)必须含有3个属性
(2)必须具有计算三种情况的求根方法(等实根、不等实根、复根),该方法的返回为字符串(其它类型无法完成返回值的)
(3)构造方法必须有(包括默认、带参数的)
2、5分——测试类
必须能够对三种情况都输出正确、合适的值。
package t2;
public class FC {
private double a,b,c;
public FC(double a, double b, double c) {
super();
this.a = a;
this.b = b;
this.c = c;
}
public FC() {
super();
// TODO Auto-generated constructor stub
}
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public double getC() {
return c;
}
public void setC(double c) {
this.c = c;
}
public String jie(){
String jg="";
double d=b*b-4*a*c;
if(d==0){
jg="x1=x2="+(-b/(2*a));
}
if(d>0){
jg="x1="+(-b+Math.sqrt(d))/(2*a);
jg=jg+" x2="+(-b-Math.sqrt(d))/(2*a);
}
if(d<0){
double sb=-b/(2*a);
double xb=Math.sqrt(-d)/(2*a);
jg="x1="+sb+"+"+xb+"i";
jg=jg+" x2="+sb+"-"+xb+"i";
}
return jg;
}
}
package t2;
public class Test {
public static void main(String[] args) {
FC f1=new FC(2,4,2);
FC f2=new FC(2,5,2);
FC f3=new FC(2,4,4);
System.out.println(f1.jie());
System.out.println(f2.jie());
System.out.println(f3.jie());
}
}
1、5分——点类
(1)必须含有2个属性
(2)必须具有获取圆信息的方法(返回值一般为字符串)
(3)构造方法必须有(包括默认、带参数的)
(4)必须含有利用两点求距离的方法
2、5分——三角形类
(1)基于3点构成三角形的思想,设计三角形类。
(2)基于继承,还需要添加两个点属性
(3)构造方法(可以适当改造!)
(4)利用计算两点之间的距离方法,求三角形的三边长。
(5)求面积、周长的方法
3、5分——测试类
正确利用上面的两个类,创建对象并完成相关的功能。
package t4;
public class Point {
private double x,y;
public Point(double x, double y) {
super();
this.x = x;
this.y = y;
}
public Point() {
super();
// TODO Auto-generated constructor stub
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public String pp(){
return "("+x+","+y+")";
}
public double d(Point p){
double s=Math.sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
return s;
}
}
package t4;
public class Sss extends Point {
private Point p2;
private Point p3;
public Sss(double x, double y, Point p2, Point p3) {
super(x, y);
this.p2 = p2;
this.p3 = p3;
}
public Sss(Point p1, Point p2, Point p3) {
super(p1.getX(),p1.getY());
this.p2 = p2;
this.p3 = p3;
}
public double zc(){
double a=super.d(p2);
double b=super.d(p3);
double c=p2.d(p3);
return a+b+c;
}
public double mj(){
double a=super.d(p2);
double b=super.d(p3);
double c=p2.d(p3);
double d=(a+b+c)/2;
return Math.sqrt(d*(d-a)*(d-b)*(d-c));
}
}
package t4;
public class Test {
public static void main(String[] args) {
Point p1=new Point(0,0);
Point p2=new Point(1,0);
Point p3=new Point(0,1);
Sss a=new Sss(p1,p2,p3);
System.out.println(a.zc()+":"+a.mj());
}
}
提示1:判断是否是闰年的条件:年被400整除或者被4整除同时不被100整除。
提示2:该题目共5个问题,每个问题4分,共20分。
提示3:从1月到12月依次对应的天数为:31,28(或29),31,30,31,30,31,31,30,31,30,31
package t5;
public class Date {
private int year; //年
private int month; //月
private int day; //日
private int weekday; //星期几
public Date(int year, int month, int day, int weekday) {
super();
this.year = year;
this.month = month;
this.day = day;
this.weekday = weekday;
}
public Date() {
this.year = 2020;
this.month = 12;
this.day = 29;
this.weekday = 2;
}
public String getDate(){ //获取当前的日期信息
return year+"年"+month+"月"+day+"日 星期"+ weekday;
}
public int pdRunNian(){//判定是否闰年,并返回一个整型值
int y=0;
if(year%400==0|| year%4==0&& year%100!=0){
y=1;
}
return y;
}
public Date nextDate(){//获取并返回明天(下一天)的新日期
int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(pdRunNian()==1){
m[2]=29;
}
int a=year;
int b=month;
int c=day;
int d=weekday;
c=c+1;
if(c>m[b]){
c=1;
b=b+1;
if(b>12){
a=a+1;
b=1;
c=1;
}
}
d=d+1;
if(d>7){d=1;}
Date n=new Date(a,b,c,d);
return n;
}
}
}
package t5;
public class Test {
public static void main(String[] args) {
Date a=new Date();
System.out.println(a.getDate());
Date b=a.nextDate();
System.out.println(b.getDate());
}
}
该题目可以设计一个类实现(也可以设计多个类)。
关键是一个复制过程:采用单字符(或者字符串)实现复制,在复制前要判定是否字符并转换,同时要统计字符类型的个数。
若只实现了复制,且正确,做多给8分。
若没有统计字符个数或者统计错误,至少扣5分,严重错误扣10分。
package t6;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Copy {
public static void main(String[] args) throws IOException {
int x=0;
int y=0;
int z=0;
File f1=new File("d:/xy/a.txt");
File f2=new File("d:/xy/b.txt");
FileWriter fw=new FileWriter(f2);
FileReader fr=new FileReader(f1);
int a=fr.read();
while(a!=-1){
if(Character.isLowerCase((char)(a))){
Character.toUpperCase((char)(a));
}
fw.write(a);
if(Character.isLetter((char)(a))){
x=x+1;
}else if(Character.isDigit((char)(a))){
y=y+1;
}else{
z=z+1;
}
a=fr.read();
}
String ab="字母个数="+x+":数字个数="+y+":其他字符="+z;
fw.write("\n\r");
fw.write(ab);
}
}
1、6分——类ABCX
关键只有一个方法的重写以及构造方法
属性是什么?是关键!!
只有正确定义类给成绩。
2、6分——类ABCY
关键只有一个方法的重写以及构造方法
属性是什么?是关键!!
只有正确定义类给成绩。
3、8分——测试类
利用接口(多态性)以及实现类创建对象,并输出相关的值。
如何创建对象?如何利用对象?只有正确创建并使用对象才给分数。**
package t7;
public class ABCX implements IABC {
private double a[];
public ABCX(double[] a) {
super();
this.a = a;
}
@Override
public double max() {
double m=a[0];
for(int i=1;i<a.length;++i){
if(m<a[i]){
m=a[i];
}
}
return m;
}
}
package t7;
import java.util.List;
public class ABCY implements IABC {
private List<Double> a;
public ABCY(List a) {
super();
this.a = a;
}
@Override
public double max() {
double m=(a.get(0)).doubleValue();
for(int i=1;i<a.size();++i){
if(m<(a.get(i)).doubleValue()){
m=(a.get(i)).doubleValue();
}
}
return m;
}
}
package t7;
public interface IABC {
public double max();//该方法返回一个集合中的最大值
}
package t7;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
double a[]={1,2,3,10,4,5,6,7};
List b=new ArrayList();
for(int i=0;i<a.length;++i){
b.add(a[i]);
}
IABC c=new ABCX(a);
IABC d=new ABCY(b);
System.out.println(c.max());
System.out.println(d.max());
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。