实习一 复数类的相关计算
题目描述
题解
//Test测试过程 略
//ComplexNumber类
public class ComplexNumber {
double real;
double imag;
public ComplexNumber(double r,double i) {
this.real = r;
this.imag = i;
}
public ComplexNumber add(ComplexNumber c) {
double r = this.real +c.real;
double i = this.imag + c.imag;
return new ComplexNumber(r,i);
}
public ComplexNumber minus(ComplexNumber c) {
double r = this.real -c.real;
double i = this.imag -c.imag;
return new ComplexNumber(r,i);
}
public ComplexNumber multiply(ComplexNumber c) {
double r = this.real*c.real-this.imag*c.imag;
double i = this.real*c.imag+this.imag*c.real;
return new ComplexNumber(r,i);
}
public ComplexNumber divide(ComplexNumber c) {
double r = (this.real*c.real+this.imag*c.imag)/(c.imag*c.imag+c.real*c.real);
double i = (this.imag*c.real-this.real*c.imag)/(c.imag*c.imag+c.real*c.real);
return new ComplexNumber(r,i);
}
public double module() {
return Math.sqrt(this.real*this.real+this.imag*this.imag);
}
public String toString() {
if(this.imag>0)
return "该复数是:"+this.real+"+"+this.imag+"i\n";
else if(this.imag==0)
return "该复数是:"+this.real+"\n";
else
return "该复数是:"+this.real+this.imag+"i\n";
}
}
实习二 面向抽象编程--求解锥体体积
题目描述
题解
//Point类 实现构造和求解两点距离 未呈现
//抽象Shape类 --内设面积和周长虚方法
public abstract class AbstractShape {
public abstract double Area();
public abstract double Perimeter();
}
//不同的形状类继承抽象Shape 并重新改写抽象类的方法
//①Circle类
public class Circle extends AbstractShape{
double r;
public Circle(double r) {
this.r = r;
}
@Override
public double Area() {
// TODO Auto-generated method stub
return Math.PI*this.r*this.r;
}
@Override
public double Perimeter() {
// TODO Auto-generated method stub
return 2*Math.PI*this.r;
}
}
//②矩形类
public class Rectangle extends AbstractShape{
double a,b;
public Rectangle(double a,double b) {
this.a = a;
this.b = b;
}
@Override
public double Area() {
// TODO Auto-generated method stub
return a*b;
}
@Override
public double Perimeter() {
// TODO Auto-generated method stub
return (this.a+this.b)*2;
}
}
//③三角形
public class Triangle extends AbstractShape{
//double a,b,c;
Point a,b,c;
public Triangle(int ax,int ay,int bx,int by,int cx,int cy) {
a = new Point(ax,ay);
b = new Point(bx,by);
c = new Point(cx,cy);
}
@Override
public double Area() {
// TODO Auto-generated method stub
double ac = a.Distance(c);
double bc = b.Distance(c);
double ab = a.Distance(b);
double t = (ac+bc+ab)/2.0;
return Math.sqrt(t*(t-ac)*(t-ab)*(t-bc));
}
@Override
public double Perimeter() {
// TODO Auto-generated method stub
double ac = a.Distance(c);
double bc = b.Distance(c);
double ab = a.Distance(b);
double t = (ac+bc+ab);
return t;
}
}
//锥体类 --内设成员变量->抽象对象
public class Vertebral {
double h; //锥体的高度
AbstractShape shape;
public Vertebral(AbstractShape shape,double h) {
// TODO Auto-generated constructor stub
this.shape = shape;
this.h = h;
}
public double Volume() {
return this.h*shape.Area()/3.0;
}
public String toString() {
return "该锥体的体积是:"+this.Volume();
}
}
//Test测试类
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
System.out.println("输入圆的半径及锥体的高度:");
double r = scanner.nextDouble();
double h1 = scanner.nextDouble();
Circle circle = new Circle(r);
Vertebral Vc = new Vertebral(circle,h1);
System.out.println(Vc);
System.out.println("输入三角形的三点坐标及锥体的高度:");
int ax = scanner.nextInt();
int ay = scanner.nextInt();
int bx = scanner.nextInt();
int by = scanner.nextInt();
int cx = scanner.nextInt();
int cy = scanner.nextInt();
double h2 = scanner.nextDouble();
Triangle t = new Triangle(ax,ay,bx,by,cx,cy);
Vertebral Vt = new Vertebral(t,h2);
System.out.println(Vt);
System.out.println("输入矩形的两边及锥体的高度:");
double a1 = scanner.nextDouble();
double b1 = scanner.nextDouble();
double h3 = scanner.nextDouble();
Rectangle re = new Rectangle(a1,b1);
Vertebral Vr = new Vertebral(re,h3);
System.out.println(Vr);
}
实习三 线性表设计【详情见数据结构aaaa】
题目描述
题解
//Test略
//Node
public class Node {
int value;
private Node next;
public Node(int value) {
this.value = value;
next = null;
}
public void setValue(int value) {
this.value = value;
}
public void setNext(Node next) {
this.next = next;
}
public int getValue() {
return this.value;
}
public Node getNext() {
return this.next;
}
}
//List类
public class List {
Node head;
Node last;
public List() {
head = last = null;
}
//将指定节点添加到此列表的结尾
public boolean insert(Node node) {
return insert(length(), node);
}
//根据 value 生成 Node 对象,添加至指定位置
public boolean insert(int index, int value) {
return insert(index, new Node(value));
}
public boolean insert(int index, Node node) {
if (index < 0 || index > length()) {
return false;
}
if (index == 0) {
if (head == null) {
head = node;
return true;
}
node.setNext(head);
head = node;
} else if (index == length()) {
if (head == null) {
head = node;
return true;
}
Node last = head;
while (last.getNext() != null) {
last = last.getNext();
}
last.setNext(node);
} else {
int position = 0;
Node now = head; // 标记当前节点
Node prior = null; // 记录前置节点
while (now != null) {
if (position == index) {
node.setNext(now);
prior.setNext(node);
return true;
}
prior = now;
now = now.getNext();
position++;
}
}
return true;
}
public boolean insertToHead(Node node) {
return insert(0, node);
}
//返回此列表中指定位置处的元素
private Node get(int index) {
if (index < 0 || index > length() - 1) {
throw new IndexOutOfBoundsException("IndexOutOfBoundsException");
}
int position = 0;
Node now = head;
Node node = new Node(head.getValue());
while (now != null) {
if (position == index) {
node = now;
}
now = now.getNext();
position++;
}
return node;
}
public void setData(int index, int value) {
if (index < 0 || index > length() - 1) {
throw new IndexOutOfBoundsException("IndexOutOfBoundsException");
}
int position = 0;
Node now = head;
while (now != null) {
if (position == index) {
now.setValue(value);
}
now = now.getNext();
position++;
}
}
//返回此列表的元素个数
private int length() {
int length = 0;
Node now = head;
while (now != null) {
now = now.getNext();
length++;
}
return length;
}
public boolean isEmpty() {
if (head == null && last == null) {
return true;
} else {
return false;
}
}
public Node getHead() {
return head;
}
public Node getLast() {
return last;
}
//遍历输出每个节点的序号和 value
public void printList() {
Node now = head;
int position = 0;
while (now != null) {
System.out.println(position + ": " + now.getValue());
now = now.getNext();
position++;
}
}
public Node remove(int index) {
Node deleteNode = new Node(head.getValue());
if (index < 0 || index > length() - 1) {
throw new IndexOutOfBoundsException("IndexOutOfBoundsException");
}
if (index == 0) {
deleteNode = head;
head = head.getNext();
return deleteNode;
}
int position = 0;
Node now = head;
Node prior = null;
while (now != null) {
if (position == index) {
deleteNode = now;
prior.setNext(now.getNext());
now.setNext(null);
}
prior = now;
now = now.getNext();
position++;
}
return deleteNode;
}
}