1.定义一个复数类complex,它的内部具有两个实例变量:realPart和imagPart,分别代表复数的实部和虚部,编程实现要求的数学运算:1)实现两个复数相加;2)实现两个复数相减;3)输出运算的结果。然后,调用上述方法实现两个复数18+2i、19-13i的相加、相减,并打印出结果。

class complex{
	int realPart,imagePart;
	
	complex(){
		realPart=0;
		imagPart=0;
	}
	complex(int realPart,int imagePart){  
		this.realPart = realPart;
		this,imagePart = imagePart;
	}

   //以下的三个方法都可以+static,否则会报错“java: 无法从静态上下文中引用非静态 变量 realPart”,顶多+public可省略

	void add(complex c1,complex c2 ) {
	 	realPart = c1.realPart + c2.realPart;
	 	imagePart = c1.imagePart + c2.imagePart;
	}
	void sub(complex c1,complex c2 ) {  
	 	realPart = c1.realPart - c2.realPart;
	 	imagePart = c1.imagePart - c2.imagePart;
	}
	void Print(){
		if(realPart == 0 && imagePart == 0){ 
			System.out.println("0");
		}else if(realPart == 0){
			System.out.println(imagePart+"i");
		}else if(imagePart == 0){
			System.out.println(realPart);
		}else if(imagePart < 0){
			System.out.println(realPart + "" + imagePart + "i");
		}else{
			System.out.println(realPart + "+" + imagePart + "i");
		}
	}
	public static void main(String[] args){
	   //一定要创造一个新对象来引用对象的方法
	    complex c0 = new complex();       
        complex c1 = new complex(18,2);
        complex c2 = new complex(19,-13);
        c0.add(c1,c2);
        c0.Print();
        c0.sub(c1,c2);
        c0.Print();
	}
}

2.首先定义一个计算二维坐标系中圆面积的类circleClass,要求类中有一个定义圆心座标,圆上一点座标的构造函数,以及一个通过圆上一点座标与圆心座标计算圆面积的方法area。然后,通过上述类生成两个圆对象circle1、circle2进行测试:一个圆心、圆上一点座标分别为(0,0)、(8.5,9),另一个圆心、圆上一点座标分别为(2,3.5)、(9,6),并分别显示各自面积。

//因为要用到相同结构的点坐标,写出一个点类,后期通过创造对象来定义圆心坐标和圆上坐标
class Point{
	double x,y;
	Point(double  x,double y){
		this.x = x;
		this.y = y;
	}
}
class circleClass{
	//public final static double PI这是在创建一个常量,一旦声明就不允许修改值
	final static double PI = 3.14;
	//类中的有参构造函数
	Point p1,p2; 				    //不能遗漏,会报错
	circleClass(Point p1,Point p2){
		this.p1 = p1;
		this.p2 = p2;
	}
	//类中的计算圆面积的无返回值的方法
	void area(){
		System.out.println("圆的面积=" + PI*(Math.pow(p2.x-p1.x,2)+Math.pow((p2.y-p1.y),2)));	  //Math.pow不会导包,S = Π*r方
	}
	public static void main(String[] args){
		Point p1 = new Point(0,0);
		Point p2 = new Point(8.5,9);
		circleClass c1 = new circleClass(p1,p2);
		c1.area();
		Point p3 = new Point(2,3.5);
		Point p4 = new Point(9,6);
		circleClass c2 = new circleClass(p3,p4);
		c2.area();
	}
}

3.首先定义一个计算长方形面积的类rectangleClass,要求类中有一个定义长方形左上角和右下角座标的构造函数,以及一个通过长方形右下角座标与左上角座标计算长方形面积,并实例化两个长方形进行测试。

class Point{
	double x,y;
	Point(double x,double y){
		this.x = x;
		this.y = y;
	}
}
class rectangleClass{
	Point p1,p2;
	rectangle(Point p1,Point p2){
		this.p1 = p1;
		this.p2 = p2;
	}
	void area(){
		System.out.println("长方形面积:" + Math.abs((p2.x-p1.x)*(p2.y-p1.y)));
	}
	public static void main(String[] args){
		Point p1 = new Point(0,0);
		Point p2 = new Point(8.5,9);
		rectangleClass r1 = new rectangleClass(p1,p2);
		r1.area();
		Point p3 = new Point(2,3.5);
		Point p4 = new Point(9,6);
		rectangleClass r2 = new rectangleClass(p3,p4);
		r2.area();
	}
}

4.将笛卡尔坐标系上的点定义为一个类Point,该类要求提供求得坐标系上两点间距离的功能、获取和设置坐标的功能、获取极坐标的功能,以及完成对已创建的Point类对象进行个数统计的功能。设计测试Point类的应用程序主类,测试并显示输出提供所有功能的结果。

class Piont 
{
      private int x,y;     //私有成员变量
      static int pCount=0;     //静态成员变量
      static final double pi=3.14159;  //全局常量
      //重载的构造方法
      public Piont( )  {  }
      public Piont(int x,int y) {this.x=x;this.y=y;}
      //其它成员方法
      public int getx( ){ return this.x;}
      public void setx(int x) { this.x=x; }
      public int gety( ){ return this.y; }
      public void sety(int y) { this.y=y; }
      public double angle( ){ return (180/pi)*Math.atan2(y,x); }//Math.atan2()接受两个参数x和y,方法如下:
angel=Math.atan2(y,x)
x 指定点的 x 坐标的数字。
y 指定点的 y 坐标的数字。
计算出来的结果angel是一个弧度值,也可以表示相对直角三角形对角的角,其中 x 是临边边长,而 y 是对边边长。

      public double radius ( ){ return Math.sqrt(x*x+y*y);  }//sqrt() 方法可返回一个数的平方根。
      public double ppdistance ( Piont p) //方法的参数是对象
      { double distance;
        distance= Math.sqrt((double)((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)));
        return distance;
      }
      //静态成员方法
     static void setpCount( ) { pCount+=1;}
     static int getpCount( ) { return pCount;}
}
public class MDPoint3
{ public static void main (String args[])
  { double distance1,distance2;
    Piont p1=new Piont( );
    p1.setpCount( );
    System.out.println("当前点的总数为:"+p1.getpCount)( ));
    Piont p2=new Piont( 2,2);
    p2.setpCount( );
    System.out.println("当前点的总数为:"+p2.getpCount)( ));
    Piont p3=new Piont( 3,5);
    p3.setpCount( );
    System.out.println("当前点的总数为:"+p3.getpCount)( ));
    distance1=p1.ppdistance ( p2);
    distance2=p3.ppdistance ( p1);
    System.out.println("p1,p2两点间的距离为:"+distance1);
    System.out.println("p3,p1两点间的距离为:"+distance2);
    System.out.println("p3点的极坐标:angle="+p3.angle()+",radius="+p3.radius());
   }
}
程序运行结果:
当前点的总数为:1
当前点的总数为:2
当前点的总数为:3
p1,p2两点间的距离为:2.8284271247461903
p3,p1两点间的距离为:5.830951894845301
p3点的极坐标:angle=59.03629333375019,radius=5.830951894845301

5.设计一个表示图书的Book类,它包含图书的书名、作者、月销售量等属性,另有两个构造方法(一个不带参数,另一个带参数),成员方法setBook( ) 和printBook()分别用于设置和输出书名、作者、月销售量等数据。并设计相应的测试Book类的应用程序主类,测试并显示输出提供所有功能的结果。

class Book{
    //属性
	String name;
    String author;
    int monthSale;
	//构造方法
	Book(){}
	Book(String name,String author,int monthSale){
		 this.name = name;
		 this.author = author;
		 this. monthSale = monthSale;
	}
	void setBook(String name,String author,int monthSale){	   //void无返回值方法
		 this.name = name;
		 this.author = author;
		 this. monthSale = monthSale;
	}
	void printBook(){
		System.out.println("书名" + name);
		System.out.println("作者" + author);
		System.out.println("月销量" + monthSale);
	}
	public static void main(String[] args){
		Book b1 = new Book("西游记","吴承恩",1000);
		b1.printBook();
		Book b2 = new Book();
		b2.setBook("红楼梦","曹雪芹",2000);
		b2.printBook();
	}
}

6.请创建一个银行帐户类,要求如下:(1)类包括帐户名、帐户号、存款额等属性;(2)可实现余额查询,存款和取款的操作。(3)创建该类的对象,验证以上两项。

class Bank{
    //三个属性
	String userName;
	String account;
	double balance;
	//无参构造方法
	Bank(){
		userName = "银行测试号";
		account = "1000000000";
		balance = 1000000;
	}
	//有参构造方法
	Bank(String userName,String account,double balance){
		this.userName = userName;
		this.account = account;
		this.balance = balance;
	}
	//查询账户信息方法
	void query(){
		System.out.println("用户名" + userName + "\n" 
							+ "账号" + account + "\n"
							+ "余额" + balance);
	}
	//存钱方法
	void deposit(double numb){
		balance += numb;	
	}
	//取钱方法
	void withdraw(double numb){
		if(numb <= balance){
			balance -= numb;
		}else{
			System.out.println("账户余额不足,操作失败!");
		}
	}
	//测试方法,创建对象,调用方法
	public static void main(String[] args){
		Bank b1 = new Bank();
		b1.query();
		Bank b2 = new Bank("张三疯", "1000000002", 20000);
		b2.query();
		b2.deposit(500000);
		b2.query();
		b2.withdraw(100000); //如果未超出余额没有输出 
		//有个问题: 如果取出金额超出余额,查询显示的是初始值
		b2.query();
	}
}

7.设计一个学生类Student,其数据成员有name(姓名)、age(年龄)和degree(学位)。由Student类派生出本科生类Undergraduate和研究生类Graduate,本科生类Undergraduate增加成员specialty(专业),研究生类增加成员direction(研究方向)。每个类都有show()方法,用于输出数据成员信息。最后请输出下列信息:
在这里插入图片描述

class Student{
	String name;
	int age;
	String degree;
	Student(){}
	Student(String name,int age,String degree){
		this.name = name;
		this.age = age;
		this.degree = degree;
	}
	void show(){
		System.out.print("姓名:" + name + "\t"+ "年龄" + age + "\t" + "学位:" + degree);
	}
}
class Undergraduate extends Student{
	String specialty;
	//子类继承父类,只是继承其公有的属性和方法。私有的,构造继承不过来。子类只是调用父类的有参构造方法
	Undergraduate(String name,int age,String degree,String specialty){
		super(name,age,degree); //子类调用父类的有参构造方法,super关键字要写在第一行
		this.specialty = specialty;
	}
	void show(){
		super.show();
		System.out.println("\t专业:" + specialty);
	}
}
class Graduate extends Student{
	String direction;
	Graduate(String name,int age,String degree,String direction){
		super(name,age,degree);
		this. direction = direction;
	}
	void show(){
		super.show();
		System.out.println("\t研究方向:" + direction);
	}
}
class StudentTest {
	public static void main(String[] args) {
		Undergraduate u1 = new Undergraduate("张三",20,"本科","通信");
		Undergraduate u2 = new Undergraduate("李四",21,"本科","电子");
		Graduate u3 = new Graduate("王五",25,"硕士","通信");
		Graduate u4 = new Graduate("刘六",36,"博士","通信");
		u1.show();
		u2.show();
		u3.show();
		u4.show();
	}
}

8.编程实现有一个电话类Phone,它有号码的属性number,是一个12位的字符数组,它有四个功能,设置电话号码setNumber(),显示电话号码getNumber(),接电话answer(),拨打电话dial();移动电话mobilePhone和固定电话fixPhone是电话的两个子类, 但移动电话号码为11位, 并且移动电话和固定电话接听和拨打电话的方式不同.固定电话又有一个子类:无绳电话cordlessPhone,无绳电话号码为4位,它相对固定电话还多一个移动功能move().实现这几个类,并且测试它们的功能.
在这里插入图片描述

/**
 * 第8题:电话类Phone
 */
class phone
{
	char[] number=new char[12];
	void setNumber(char[] number) //将输入数据传入数组
	{
		this.number=number;
	}
	void getNumber()
	{
		System.out.print("本机号码:");
		for(int i=0;i<number.length;i++) 
			System.out.print(number[i]);
		System.out.println();
	}
	void dail()
	{
		System.out.println("正在拨打电话....");
	}
	void answer()
	{
		System.out.println("正在接听电话....");
	}
}
class mobilePhone extends phone
{
	char[] number=new char[11];
	void dail()
	{
		System.out.println("正在通过移动网络拨打电话....");
	}
	void answer()
	{
		System.out.println("正在通过移动网络接听电话....");
	}
}

class fixPhone extends phone
{
	void dail()
	{
		System.out.println("正在通过电信固网拨打电话....");
	}
	void answer()
	{
		System.out.println("正在通过电信固网接听电话....");
	}
}
class cordlessPhone extends fixPhone
{
	char[] number=new char[4];
	void move()
	{
		System.out.println("正在移动通话....");
	}
}
class phoneTest
{
	public static void main(String[] args)
	{
		fixPhone fp=new fixPhone();
		mobilePhone mp=new mobilePhone();
		cordlessPhone cp=new cordlessPhone();

		char []number = new char[]{'1','5','7','8','8','2','2','2','0','9','6','7'};
		fp.setNumber(number);
		fp.getNumber();
		fp.dail();
		fp.answer();

		mp.setNumber(new char[]{'1','5','7','8','8','2','2','2','0','9','6'});
		mp.getNumber();
		mp.dail();
		mp.answer();

		cp.setNumber(new char[]{'2','0','9','6'});
		cp.getNumber();
		cp.dail();
		cp.answer();
		cp.move();
	}
}

9.有工人、服务员、教师、科学家四种角色,其中服务员、工人只有月固定工资(元/月),教师除月固定工资外,还有课酬(元/节)。科学家除月固定工资外,还有科研激励奖(元/季度)。请通过继承设计出相应的类,将各种类型的员工的全年工资打印出来,并测试(张三、工人、4000元/月)、(李四、服务员、3500元/月)、(王五、教师、5000元/月、100元/节,200节/年)、(刘六、科学家、7000元/月、20000元/季度)。

class worker
{
	String name;
	String job;
	double salary;

	worker(String name,String job,double salary)
	{
		this.name=name;
		this.job=job;
		this.salary=salary;
	}

	void stat()
	{
		System.out.println("姓名:"+name+"\t 职业:"+job+"\t 月薪:"+salary+"\t 年收入:"+(salary*12.0));
	}
}

class teacher extends worker
{
	double allowance;
	teacher(String name,String job,double salary,double allowance)
	{
		super(name,job,salary);
		this.allowance=allowance;
	}
	void stat()
	{
		System.out.println("姓名:"+name+"\t 职业:"+job+"\t 月薪:"+salary +
				"\t 课时费:"+allowance 
				+"\t 年收入:"+(salary*12.0+allowance*200.0));
	}
}
class scientist extends worker
{
	double bonus;
	scientist(String name,String job,double salary,double bonus)
	{
		super(name,job,salary);
		this.bonus=bonus;
	}
	void stat()
	{
		System.out.println("姓名:"+name+"\t 职业:"+job+"\t 月薪:"+salary
		                 +"\t 激励奖:"+bonus
		                 +"\t 年收入:"+(salary*12.0+bonus*4.0));
	}

}

class workerTest
{
	public static void main(String[] args)
	{
		worker w=new worker("张三","工人",4000);
		worker s=new worker("李四","服务员",3500);
		teacher t=new teacher("王五","教师",5000,100);
		scientist k=new scientist("刘六","科学家",7000,20000);

		w.stat();
		s.stat();
		t.stat();
		k.stat();
	}
}

下面属于导包使用,了解即可,实际操作IDEA自动生成,这里手写代码所以必须加上

10.在biology包中的animal包中有human类,它具有name,height,weight的属性,还具有eat(),sleep()和work()的行为,在biology包中的plant包中有flower类,它具有name,color,smell的属性,还具有drink()和blossom()的行为.现在在一个school包中的garden包中一个张三的人,他是一个human类的对象,种植的rose是一个flower类对象,编程实现并测试各自的方法.

注意事项:public不能省略,测试类不要忘记导包,创建对象要在main方法中,human和flower类最好写构造函数;

package biology.animal;
public class human
{
  String name;
  double height,weight;
  
  public human(String name)
  {
	  this.name=name;
  }
  public void eat()
  {
	System.out.println(name+"is eating");
  }
  public void sleep()
  {
	System.out.println(name+"is sleeping");
  }
  public void work()
  {
	System.out.println(name+"is working");
  }
}

package biology.plant;
public class flower
{
   String name,color,smell;
   
   public flower(String name)
   {
	   this.name=name;
   }
   
   public void drink()
   {
		System.out.println(name+"is drinking");
   }
   
   public void blossom()
   {
		System.out.println(name+"is blossoming");
   }
}

package shool.garden;

import biology.animal.human;
import biology.plant.flower;
class packTest
{
   public static void main(String[] args)
   {
       human h=new human("张三");
	   flower f=new flower("rose");
	   
	   h.eat();
	   h.sleep();
	   h.work();
	   
	   f.drink();
	   f.blossom(); 
   }
}

11.在computer包中的mainbroad包中有一个VGACard的类,它有一个显示方法show(),显示”VGA checked success”,在server的包的mainbroad包中的showCard类是继承自VGACard,请测试showCard的show()功能。

package computer.mainboard;
public class VGACard
{
   public void show()
   {
	  System.out.println("VGA checked success!");
   }

}
package server.mainboard;
import computer.mainboard.VGACard;//导包
class ShowCard extends VGACard
{
	//主方法不要忘
   public static void main(String[] args)
   {
      ShowCard sc=new ShowCard();
	  sc.show();
   }
}

12.在com.graphic包中定义一个圆柱体类Cylinder,其半径r,高h都为私有属性,有构造方法和求体级方法volume()。在com.test包中定义一个测试类test,测试一个半径为5.34、高为2的圆柱体体积。半径PI为3.14。


package com.graphic;
//public 均不可省略
public class Cylinder
{
   private double r,h;
   private final static double PI=3.14;
   
   public Cylinder(double r,double h)
   {
      	this.r=r;
	    this.h=h;
   }
   public void volume()
   {
      System.out.println("圆柱体体积:"+(PI*r*r*h)); 
   }
}


package com.test;
import com.graphic.Cylinder;
class Cytest
{
   public static void main(String[] args)
   {
	   Cylinder cy=new Cylinder(5.34,2.0);
	   cy.volume();
   }
}

13.首先设计一个学生抽象类Student,其数据成员有name(姓名)、age(年龄)和degree(学位),以及一个抽象方法show()。然后由Student类派生出本科生类Undergraduate和研究生类Graduate,本科生类Undergraduate增加成员specialty(专业),研究生类增加成员direction(研究方向)。并且每个类都有show()方法,用于输出数据成员信息。请定义对象,并打印输出下列信息: .
张三:20,本科,计算机科学
李四:21,本科,物联网
王五:25,硕士,软件工程
刘六:36,博士,通信工程

//和第八题区别就是Student定义成抽象类,show抽象方法没有方法体
abstract class Student
{
	String name;
	int age;
	String degree;

	Student(String name,int age,String degree)
	{
		this.name=name;
		this.age=age;
		this.degree=degree;
	}
	abstract void show();
}

class Undergraduate extends Student
{
	String specialty;

	Undergraduate(String name,int age,String degree,String specialty)
	{
		super(name,age,degree);
		this.specialty=specialty;
	}
	void show()
	{
		System.out.println(name+":"+age+","+degree+","+specialty);
	}

}

class Graduate extends Student
{
	String direction;

	Graduate(String name,int age,String degree,String direction)
	{
		super(name,age,degree);
		this.direction=direction;
	}
	void show()
	{
		System.out.println(name+":"+age+","+degree+","+direction);
	}
}
class StudentTest{
	public static void main(String[] args)
	{
		Undergraduate ug1=new Undergraduate("张三",20,"本科","计算机科学");
		Undergraduate ug2=new Undergraduate("李四",21,"本科","物联网");
		Graduate g1=new Graduate("王五",25,"硕士","软件工程");
		Graduate g2=new Graduate("刘六",36,"博士","通信工程");

		ug1.show();
		ug2.show();
		g1.show();
		g2.show();
	}
}

14.设计一个抽象类Graphics,它具有一个String类型参数name和两个抽象方法parameter()、area(),name用来存储图形的名称,parameter()方法用于输出图形的名称和其它属性特征,area()方法用于输出图形的面积。请用该类派生的子类实现输出一个形状为长方形、长为3宽为2和它面积以及输出一个形状为圆形、颜色为红色、半径为4和它面积。


abstract class Graphics {
    String name;
    Graphics(String name) {
        this.name = name;
    }
    abstract void parameter();
    abstract void area();
}

class rectangle extends Graphics{
    int height,weight;
    rectangle(String name,int height,int weight) {
        super(name);
        this.height = height;
        this.weight = weight;
    }

   // @Override
    void parameter() {
        System.out.println(name+"\t长:"+height+",宽:"+weight);
    }

    //@Override
    void area() {
        System.out.println("面积:" + (height * weight));
    }
}

class circle extends Graphics{
    String color;
    double r;
    final static double PI = 3.14;//final static声明不允许修改值的常量
    circle(String name,String color,double r) {
        super(name);
        this.color = color;
        this.r = r;
    }
    void parameter(){
        System.out.println(name+"\t颜色:"+color+",半径:"+r);
    }
    void area(){
        System.out.println("面积:"+(PI*r*r));
    }

    public static void main(String[] args){
        rectangle r = new rectangle("长方形",3,2);
        r.parameter();
        r.area();

        circle c = new circle("圆形","红色",4);
        c.parameter();
        c.area();
    }
}

15.设计一个接口circleInterface,要求接口中有一个定义PI的常量以及一个计算圆面积的空方法circleArea()。然后设计一个类circleClass实现该接口,通过构造函数circleClass(double r)定义圆半径,并增加一个显示圆面积的方法。最后,通过上述类生成两个半径分别为3.5、5.0的圆对象circle1、circle2进行测试。

interface circleInterface{
    double PI = 3.14;
    void circleArea();  //空方法
}
class circleClass implements circleInterface{
    double r,s;
    circleClass(double r){
        this.r = r;
    }
    public void circleArea(){
        s = PI*r*r;          
    }
    void show(){
        System.out.println("半径:" + r + "的圆面积:" + s);
    }

    public static void main(String[] args){
        circleClass c1 = new circleClass(3.5);
        c1.circleArea();
        c1.show();
        circleClass c2 = new circleClass(5.0);
        c2.circleArea();
        c2.show();
    }
}

16.设计一个Shape接口和它的两个实现类Square和Circle,要求如下:
1)Shape接口中有一个抽象方法area(),方法接收一个double类型的参数,返回一个double类型的结果。
2)Square和Circle中实现了Shape接口的area()抽象方法,分别求正方形和圆形的面积并返回。
在测试类中创建Square和Circle对象,计算边长为2的正方形面积和半径为3的圆面积。

interface shape
{
    double area(double r);
}

class Circle implements shape
{
    double r;
    final static double PI=3.14;
    Circle(double r)
    {
        this.r=r;
    }

    public double area(double r)
    {
        return PI*r*r;
    }

    void show()
    {
        System.out.println("半径:"+r+"的圆面积:"+area(r));
    }
}
class square implements shape
{
    double weigth;

    square(double weigth)
    {
        this.weigth=weigth;
    }

    public double area(double r)
    {
        return r*r;
    }

    void show()
    {
        System.out.println("边长:"+weigth+"的正方形面积:"+area(weigth));
    }

    public static void main(String[] args)
    {
        square s=new square(2);
        s.show();

        Circle c=new Circle(3);
        c.show();
    }
}
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐