Java宝藏实验资源库(3)类

一、实验目的

  1. 理解面向对象程序的基本概念。
  2. 掌握类的继承的实现机制。
  3. 熟悉类中成员的访问控制方法。
  4. 熟悉ArrayList类的使用。

二、实验内容 过程及结果

*9.5Programming Exerc
ise the GregorianCal endar class) Java API has the GregorianCalendar class in the java. uti7 package, which you can use to obtain the year, month, and day of a date. The no-arg constructor constructs an instance for the current date, and the methods get(GregorianCalendar.YEAR), get(GregorianCalendar.MONTH),and get(GregorianCalendar. DAY_OF_MONTH) return the year, month, and day.Write a program to perform two tasks:
■ Display the current year, month, and day.
The GregorianCalendar class has the setTimeInMillis(long), which can be used to set a specified elapsed time since January 1, 1970. Set the value to 1234567898765L and display the year, month, and day.

*9.5编程Exerc Java API在Java中有GregorianCalendar类。Uti7包,您可以使用它来获取日期的年、月和日。无参数构造函数为当前日期构造一个实例,并使用get(GregorianCalendar. year)、get(GregorianCalendar. month)和get(GregorianCalendar)方法。DAY_OF_MONTH)返回年、月、日。编写一个程序来执行两个任务: ■显示当前的年、月、日。 GregorianCalendar类具有setTimeInMillis(long),可用于设置指定的经过时间。

运行代码如下 :

java 复制代码
package chapter09;
import java.util.GregorianCalendar;
class Code_05 {
    public static void main(String[] args){
        GregorianCalendar calendar = new GregorianCalendar();
        System.out.println(calendar.get(calendar.YEAR) + ":" + calendar.get(calendar.MONTH) + ":" + calendar.get(calendar.DAY_OF_MONTH));

        calendar.setTimeInMillis(1234567898765L);
        System.out.println(calendar.get(calendar.YEAR) + ":" + calendar.get(calendar.MONTH) + ":" + calendar.get(calendar.DAY_OF_MONTH));
    }
}

运行结果

*9.10 (Algebra: quadratic equations) Design a class named QuadraticEquation fora quadratic equation ax2 + bx + x = 0. The class contains:

■ Private data fields a, b, and c that represent three coefficients.

■ A constructor for the arguments for a, b, and c.

■ Three getter methods for a, b, and c

*9.10(代数:二次方程式)为二次方程式ax2+bx+c=0设计一个名 QuadraticEquation的类。这个类包括:

■ 代表三个系数的私有成员变量a、b和c。

■ 一个参数为a、b和c的构造方法。

■ a、b、c 的三个get 方法。

运行代码如下 :

java 复制代码
Main.java
import java.util.Scanner;
class Unite9Test10
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入a,b,c:");
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();
        QuadraticEquation q =new QuadraticEquation(a,b,c);
        if(q.getRoot1()==0 && q.getRoot2()==0)
        {
            System.out.println("The equation has no roots");
        }else if(q.getRoot1()==q.getRoot2())
        {
            System.out.println(q.getRoot1());
        }else
        {
            System.out.println("root1 = "+q.getRoot1()+","+"root2 = "+q.getRoot2());
        }
    }
}
 QuadraticEquation.java
class QuadraticEquation
{
    private double a;
    private double b;
    private double c;
    public QuadraticEquation(double a,double b,double c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
    public double getA() {
        return a;
    }
    public void setA(double a) {
        this.a = a;
    }
    public double getB() {
        return b;
    }
    public void setB(double b) {
        this.b = b;
    }
    public double getC() {
        return c;
    }
    public void setC(double c) {
        this.c = c;
    }
    public double getDiscriminant()
    {
        return b*b - 4*a*c;
    }
    public double getRoot1()
    {
        double root1 = (-b +Math.sqrt(this.getDiscriminant()))/(2*a);
        if(getDiscriminant() >= 0)
        {
            return root1;
        }else
        {
            return 0;
        }
    }
    public double getRoot2()
    {
        double root2 = (-b - Math.sqrt(this.getDiscriminant()))/(2*a);
        if(getDiscriminant() >= 0)
        {
            return root2;
        }else
        {
            return 0;
        }
    }
}

运行结果

三、实验 结论

通过本次实验实践了二次方程式知识和操作,得到了编程与数学,逻辑思维息息相关的感悟,要想写优秀的代码,头脑必须清醒,思维必须井井有条,敲写代码时必须心无旁骛。

结语

细节决定成败

态度决定高度

注重细节,端正态度

!!!

相关推荐
千里镜宵烛2 分钟前
深入 Lua 环境机制:全局变量的 “容器” 与 “隔离术”
开发语言·junit·lua
命运之光3 分钟前
【快速解决】idea运行javafx错误: 缺少 JavaFX 运行时组件, 需要使用该组件来运行此应用程序
java·ide·intellij-idea
电鱼智能的电小鱼4 小时前
基于电鱼 AI 工控机的智慧工地视频智能分析方案——边缘端AI检测,实现无人值守下的实时安全预警
网络·人工智能·嵌入式硬件·算法·安全·音视频
年年测试4 小时前
AI驱动的测试:用Dify工作流实现智能缺陷分析与分类
人工智能·分类·数据挖掘
孫治AllenSun5 小时前
【算法】图相关算法和递归
windows·python·算法
学到头秃的suhian5 小时前
Maven
java·maven
QX_hao5 小时前
【Go】--反射(reflect)的使用
开发语言·后端·golang
格图素书5 小时前
数学建模算法案例精讲500篇-【数学建模】DBSCAN聚类算法
算法·数据挖掘·聚类
小坏讲微服务5 小时前
Docker-compose 搭建Maven私服部署
java·spring boot·后端·docker·微服务·容器·maven
chxii5 小时前
Maven 详解(下)
java·maven