大家好,我是【1+1=王】, 热爱java的计算机(人工智能)渣硕研究生在读。
如果你也对java、人工智能等技术感兴趣,欢迎关注,抱团交流进大厂!!!
Good better best, never let it rest, until good is better, and better best.
近期会把自己本科阶段的一些课程设计、实验报告等分享出来,供大家参考,希望对大家有帮助。
博客更新至专栏【课程设计实验报告】:https://blog.csdn.net/weixin_43598687/category_11640051.html
1、掌握如何使用类来封装对象的属性和功能。
2、掌握使用package和import语句。
class Lader{
private double up;
private double down;
private double height;
private double area;
public Lader(double up, double down, double height) {
super();
this.up = up;
this.down = down;
this.height = height;
this.area = (up+down)*height/2;
System.out.println("面积:" + this.area);
}
}
class Circle{
public double pi = 3.14;
private double r;
private double c;
private double s;
public Circle(double r) {
super();
this.r = r;
this.c = 2*pi*r;
this.s = pi*r*r;
System.out.println("周长:"+c + "面积:" + s);
}
}
public class one {
public static void main(String[] args) {
double up = 1;
double down =2;
double height =1;
double r =1;
Lader lader = new Lader(up, down, height);
Circle circle = new Circle(r);
}
import java.util.Scanner;
class Complex{
private double x;
private double y;
public Complex() {
super();
this.x =0;
this.y = 0;
}
public Complex(double x, double y) {
super();
this.x = x;
this.y = y;
}
public void tostring() {
if(this.y>0) {
System.out.println(this.x + "+" + this.y + "i");
}else {
System.out.println(this.x + "-" + this.y + "i");
}
}
public Complex addComp(Complex C1,Complex C2) {
Complex comp = new Complex();
comp.x = C1.x + C2.x;
comp.y = C1.y + C2.y;
return comp;
}
public Complex subComp(Complex C1,Complex C2) {
Complex comp = new Complex();
comp.x = C1.x - C2.x;
comp.y = C1.y - C2.y;
return comp;
}
public Complex multComp(Complex C1,Complex C2) {
Complex comp = new Complex();
comp.x = C1.x * C2.x -C1.y*C2.y;
comp.y = C1.x * C2.y -C1.y*C2.x;
return comp;
}
public boolean equalComp(Complex C1,Complex C2) {
if((C1.x == C2.x)&&(C1.y == C2.y)) {
return true;
}else {
return false;
}
}
}
public class two {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double x1,x2,y1,y2;
x1 = sc.nextDouble();
y1 = sc.nextDouble();
x2 = sc.nextDouble();
y2 = sc.nextDouble();
Complex C1 = new Complex(x1,y1);
Complex C2 = new Complex(x2,y2);
Complex comp = new Complex();
comp = comp.addComp(C1, C2);
System.out.print("相加:") ;
comp.tostring();
System.out.print("相减:") ;
comp = comp.subComp(C1, C2);
comp.tostring();
System.out.print("相乘:") ;
comp = comp.multComp(C1, C2);
comp.tostring();
System.out.println("是否相等:" + comp.equalComp(C1, C2));
}
}
public class SquareEquation {
private int A;
private int B;
private int C;
public SquareEquation() {
super();
}
public SquareEquation(int a, int b, int c) {
super();
A = a;
B = b;
C = c;
}
public void jisuan() {
double dt = 0,dt2 = 0 ,gen1 = 0,gen2 =0 ;
dt2 = this.B*this.B - 4*this.A*this.C;
dt = Math.sqrt(dt2);
if(dt<0) {
System.out.println("方程无解");
}else if (dt==0) {
gen1 = (-this.B)/(2*this.A);
System.out.println("方程的根为:" + gen1);
}else if (dt>0) {
gen1 = (-this.B+dt)/(2*this.A);
gen2 = (-this.B-dt)/(2*this.A);
System.out.println("方程的根为:" + gen1 + "或" + gen2);
}
}
}
public class SunRise {
public static void main(String[] args) {
int a,b,c;
Scanner sc = new Scanner(System.in);
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
SquareEquation square = new SquareEquation(a, b, c);
square.jisuan();
}
}
class MyPoint{
private double x;
private double y;
public MyPoint() {
super();
this.x = 0;
this.y = 0;
}
public MyPoint(double x, double y) {
super();
this.x = x;
this.y = y;
}
public double getD(MyPoint myPoint) {
double d = 0;
d = Math.sqrt((this.x-myPoint.x)*(this.x-myPoint.x)+(this.y-myPoint.y)*(this.y-myPoint.y));
return d;
}
}
public class four {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double x1,y1,x2,y2;
x1 = sc.nextDouble();
y1 = sc.nextDouble();
x2 = sc.nextDouble();
y2 = sc.nextDouble();
MyPoint my1 = new MyPoint(x1, y1);
MyPoint my2 = new MyPoint(x2, y2);
double d = my1.getD(my2);
System.out.println("两点间的距离为:" + d);
}
}
对象是用来描述客观事物的一个实体,类定义了对象将会拥有的特征(属性)和行为(方法)
类是对象的类型。对象是类的实例。
上面几种类的实现方法都比较类似,先是创建类的对象,在对类的对象赋值并调用一些方法。
下面是本次实验学习及用到的一些知识:
创建对象:
类名 对象名 = new 类名();
引用类的属性:
对象名.属性
引用类的方法:
对象名.方法名
定义方法:
Public 返回值类型 方法名(){
}
调用方法:
对象名.方法名();
博客更新至专栏【课程设计实验报告】:https://blog.csdn.net/weixin_43598687/category_11640051.html
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_43598687/article/details/124487936
内容来源于网络,如有侵权,请联系作者删除!