定义一个“点”(Point)类用来表示三维空间中的点(有三个坐标)。要求如下:
(1)可以生成具有特定坐标的点对象。
(2)提供可以设置三个坐标的方法。
(3)提供可以计算该“点”距原点距离平方的方法。
(4)编写主类程序验证。
class Point {
 private int x,y,z;
Point(){this(0,0,0);}
 Point(int ox,int oy,int oz){
 setXYZ(ox,oy,oz);}
 public void setX(int x){this.x=x;}
 public void setY(int y){this.y=y;}
 public void setZ(int z){this.z=z;}
 public void setXYZ(int x,int y,int z){
 setX(x);setY(y);setZ(z);}
 public int getDistance(){
  return x*x+y*y+z*z;
 }}
 public class Test{
 public static void main(String[] args) {
  Point p = new Point();
  p.setXYZ(4,5,6);
  int d =&Distance();
  System.out.println("坐标为(4,5,6)的点到原点的距离是:"+d);
 }
}
按要求编写一个Java应用程序程序:
1)定义一个接口CanFly,描述会飞的方法public void fly();
2)分别定义类飞机和鸟,实现CanFly接口。
3)定义一个测试类,测试飞机和鸟,在main方法中创建飞机对象和鸟对象,
再定义一个makeFly()方法,其中让会飞的事物飞。并在main方法中调用该方法,
让飞机和鸟起飞。
interface CanFly{
     
public void fly();}
    class Plane implements CanFly{
    public void fly(){
    System.out.println("Plane fly");
        }
    }
    class Bird implements CanFly{
    public void fly(){
    System.out.println("Bird fly");
        }
    }
    public class Test{
    public static void main(String[] args){
          Plane p = new Plane();
          Bird b = new Bird();
          new Test().makeFly(p,b);
        }
    void makeFly(Plane p,Bird b){
        p.fly();
        b.fly();
      } 
    }
【老师】 跨平台设计语言-何老师158****7403 2019/6/28 9:21:52
interface CanSwim{
void swim();
}
interface CanFly{
void fly();
}
abstract class ActionCharacter{
abstract void fight(String emp);
void speak(String s)
{System.out.println(s);}
}
class Hero extends ActionCharacter implements CanSwim,CanFly {
String name;
Hero(String name){
this.name=name;}
@Override
public void fly() {
System.out.println(name+"   fly");
}
@Override
public void swim() {
System.out.println(name+"   swim");
}
@Override
void fight(String emp) {
System.out.println(name+"   fight"+"  "+emp);
}
}
public class Adventure{
public static void main(String args[]){
Hero hb=new Hero("张友");
hb.swim();
hb.fight("Ben");
hb.fly();
CanFly cf=hb;
cf.fly();
CanSwim cs=hb;
cs.swim();
ActionCharacter ac=hb;
ac.speak("你好!");
ac.fight("JiKe");
}
}
按要求编写Java程序:
(1)编写一个接口:InterfaceA,只含有一个方法int method(int n);
(2)编写一个类:ClassA来实现接口InterfaceA,实现int method(int n)接口方
法时,要求计算1到n的和;
(3)编写另一个类:ClassB来实现接口InterfaceA,实现int method(int n)接口
方法时,要求计算n的阶乘(n!);
(4)编写测试类E,在测试类E的main方法中使用接口回调的形式来测试实现接口的类。
接口的类。
interface InterfaceA {
int method(int n);
}
class ClassA implements InterfaceA{
@Override
public int method(int n) {
int result = 0;
for(int i = 1; i<=n; i++){
result+=i;}
return result;
}
}
class ClassB implements InterfaceA{
@Override
public int method(int n) {
int result = 1;
for(int i = 1; i<=n; i++)
{ result*=i;}
return result;
}
}
public class E{
public static void main(String[] args) {
ClassA a = new ClassA();
System.out.println("1+2+3+...+6="+a.method(6));
ClassB b = new ClassB();
System.out.println("7!="+b.method(7));
}
}
第五题:
在包a中新建一个类A,在类A中有一个int add(int m)方法,用来求1+2+…+m
的和。在包b中新建一个类B,在类B中有一个int cheng(int n)方法,用来求n!
的结果。在包c中新建一个主类C,调用A、B中的方法输出1+2+…+30的和,
以及10!的计算结果。
package a;
public class A{
    int sum=0;
    public int add(int m){
        for(int i=1;i<=m;i++)
            sum+=i;
    return sum;}
}
package b;
public class B {
    int sum=1;
    public int cheng(int n){
        for(int i=1;i<=n;i++)
            sum*=i;
    return sum;}
}
package c;
java程序设计考试题import b.B;
import a.A;
public class C {
    public static void main(String[] args) {
        A a=new A();
        B b=new B();
        System.out.println("1+2+…+30="+a.add(30));
        System.out.println("10!="+b.cheng(10));
    }
第6题:在包a中编写一个类Father,具有属性:年龄(私有)、姓名(公有);
具有功能:工作(公有)、开车(公有)。
在包a中编写一个子类Son,具有属性:年龄(受保护的)、姓名;
具有功能:玩(私有)、学习(公有)。
最后在包b中编写主类Test,在主类的main方法中测试类Father与类Son。
package java.a;
public class Father {
 private int age;
     public String name;
     public void work(){
      System.out.println("Father’s work.");}
     public void driving(){
      System.out.println("Father’s driving.");
     }
     Father(String name){
             this.name=name;
     }
     public void setAge(int age){
     this.age = age;
     } 
     public int getAge(int age){
     return age;
     } 
}
class Son extends Father{
        protected int age;
        public void study(){
System.out.println("Son is studying.");}
        private void play(){
System.out.println("Son is play.");}
        Son(String name){super();}
}
package java.b        ;
class MainTest{