int x
int y
public Point() {
}
public Point(int x, int y) {
this.x = x
this.y = y
}
public Point(int x) {
this.x = x
this.y = x
}
public double distance() {//求当前点到原点的距离
return Math.sqrt((x * x + y * y))
}
public double distance(int x1, int y1) {//求当前点到(x1,y1)的距离
return Math.sqrt((x-x1)*(x-x1) + (y-y1) * (y-y1))
}
public double distance(Point other){
int x2 = other.x
int y2 = other.y
return Math.sqrt((x-x2)*(x-x2) + (y-y2) * (y-y2))
}
}
注释就不写了,将就着看吧awt/swing的题目就算了,懒得写,很少用
1、声明一个类Person,成员变量有姓名、出生年月、性别。有成员方法以显示姓名、年龄和性别。
import java.text.SimpleDateFormat
import java.util.Date
class Person {
public Person(String name, Date birthday, boolean isMale) {
super()
this.name = name
this.birthday = birthday
this.isMale = isMale
}
private String name
private Date birthday
private boolean isMale
public void showName() {
System.out.println("姓名:" + name)
}
public void showBirthday() {
System.out.println("出生年月日:"
+ new SimpleDateFormat("yyyy年MM月dd日").format(birthday))
}
public void showSex() {
System.out.print("性别:")
if (isMale){
System.out.println("男")
} else {
System.out.println("女")
}
}
}
2、声明一个接口,将一个人的出生年份转换成年龄
public interface DateUtil {
public int DateToAge()
}
3、在类Shapearea中声明三个同名方法求圆、矩形和三角形的面积(三个方法有不同的参数)。
public class Shapearea {
static final double PI=3.1415926
public static double getArea(double r){
return r*r*PI
}
public static double getArea(double x,double y){
return x*y
}
public static double getArea(double a,double b,double c){
double l=a+b+c
return Math.sqrt(l*(l-c)*(l-b)*(l-a))
}
}
4、已知某一角度的值,求它的正弦值。
Math.sin(a)
5、由键盘输入两个字符串“10”和“20”,将他们转换成整数,然后计算并输出这两个数的和。
Scanner sc=new Scanner(System.in)
String str1=sc.next()
String str2=sc.next()
Integer int1=new Integer(str1)
Integer int2=new Integer(str2)
System.out.println("结果是:"+int1+int2)
6绘图
7、绘制一圆角矩形、实心普通矩形,分别使用不同的颜色。
8、绘制一多边形(至少4个顶点)和一空心弧线(长度和角度自定)。颜色自定。
9、绘制一折线(至少六个顶点)和一正方形。颜色自定。
10、创建一个String类的对象Str1和Str2,判断Str2是否str1的子串。如果是,输出str1中在子串str2后的字符串。
Scanner sc=new Scanner(System.in)
String str1=sc.next()
String str2=sc.next()
if (str1.contains(str2)) {
System.out.println(str1.split(str2, 2)[1])
}
11、声明一个矩形类Rectangle,其中有多个构造方法。用不同的构造方法创建对象,并输出矩形的周长和面积。
12、完成一小应用程序,实现用CardLayout布局摆放三个按钮(每张卡片上一个按钮,点击按钮换卡片。)
3、C;4、A;6、A;7、A;8、D;12、A;13、B;14、B;17、A;18、D;19、D;20、A;4题:A很明显是错误的,java程序在运行前需要编译成字节码文件,才能运行。
14题:在Java语言中,标识符是以字母、下划线或美元符开头,由字母、数字、下划线或美元符组成的字符串。标识符区分大小写,长度没有限制。除以上所列几项之外,标识符中不能含有其他符号,也不允许插入空格。
17题:向main方法传入的是三个参数接收后args[]={"aaa","bb","c"}
int k1=args.length//得到数组元素个数,为3
int k2=args[1].length()//得到数组中下标为1的元素字符数(即第二个元素)
18题:String s1="AbcD"String s2=s1.toLowerCase()作用是把字符串全部转为小写,所以选D
19题:函数重载定义 1:保持相同的功能,并且有相同的函数名
2、重载方式为:返回值类型不同,形参个数不同同,形参类型不同。
在满足一的前提下,二中三个条件任意一个,或其中多个的任意组合都是重载
20题:BB继承了AA并且重写了Show()方法。父类AA实例化了a,所以a.Show()调用的是父类中的Show方法,显示:我喜欢Java!子类BB实例化了b,所以b.Show()调用子类BB中的Show方法,显示:我喜欢C++!