第四周总结&第二次实验报告

实验二 Java简单类与对象

实验目的

  • 掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
  • 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
  • 理解static修饰付对类、类成员变量及类方法的影响。

    实验内容

  1. 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:

(1) 使用构造函数完成各属性的初始赋值

(2) 使用get…()和set…()的形式完成属性的访问及修改

(3) 提供计算面积的getArea()方法和计算周长的getLength()方法

实验过程

class Rectangle {
    public double getWidth() {
        return width;
    }
    public void setWidth(double width) {
        this.width = width;
    }
    public double getHeight() {
        return height;
    }
    public void setHeight(double height) {
        this.height = height;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public Rectangle(double width,double height,String color) {
        this.setColor(color);
        this.setHeight(height);
        this.setWidth(width);
    }
    private double width;
    private double height;
    private  String color;
    public double getArea() {
        return width * height;
    }
    public double getLength() {
        return (width + height) * 2;
    }

}

public class Example {
    public static void main(String args[]) {
        Rectangle angle = null;
        angle = new Rectangle(4,5,"red");
        System.out.println("矩形的颜色为:"+angle.getColor());
        System.out.println("矩阵的周长为:"+angle.getLength());
        System.out.println("矩阵的面积为:"+angle.getArea());
    }

}

实验结果

1580249-20190918105859554-1481863516.png

  1. 银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。

    实验过程】

import java.util.*;
public class Account {
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    /*
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    */
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public double getBalance() {
        return balance;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }
    public static String getRandomNumbersAndString(int length) {
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        return getRandomString(length, base);
    }
    private static String getRandomString(int length, String base) {
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
    /*private static String getUniqueRandomString(int length, String base) {
        // TODO Auto-generated method stub
        return null;
    }*/
    //private Date date = new Date();
    
    private String id = getRandomNumbersAndString(11);
    private String name;
    private String password = "123456";
    private double balance;
    public  Account(String name,double balance) {
        this.setName(name);
        this.setBalance(balance);
    }
    public Account() {
        
    }
    public double Deposit(double n) {
        this.balance = balance + n;
        return balance;
    }
    public double Draw(double m) {
        this.balance = balance - m;
        return balance;
    }
    public String Changepassword(String password) {
        this.password = password;
        return password;
    }
}

import java.util.*;
public class Example2 {
    public static void main(String args[]) {
        Account yonghu = null;
        yonghu = new Account("及你太美",0);
        Date date = new Date();
        Scanner in= new Scanner(System.in);
        Scanner sr = new Scanner(System.in);
        String str;
        System.out.println("输入相应的英文进行操作。。。。。。。。");
        while(true) {
        str = in.nextLine();
        if(str == "exit") {
            break;
        }
        switch(str) {
        case "Deposit":{
            double n = in.nextDouble();
            System.out.println("用户的余额还有:"+yonghu.Deposit(n));
            System.out.println("输入exit结束程序。。。。。。");
            break;
        }
        case "Draw":{
            double n = in.nextDouble();
            System.out.println("用户的余额剩余:"+yonghu.Draw(n));
            System.out.println("输入exit结束程序。。。。。。");
            break;
        }
        case "Changepassword":{
            String str2;
            str2 = in.nextLine();
            System.out.println("用户的密码改为:"+yonghu.Changepassword(str2));
            System.out.println("输入exit结束程序。。。。。。");
            break;
        }
        case "date":{
            System.out.println("用户的开户日期为:"+date);
            System.out.println("输入exit结束程序。。。。。。");
            break;
        }
        case "id":{
            System.out.println("用户标识为:"+yonghu.getId());
            System.out.println("输入exit结束程序。。。。。。");
            break;
        }
        case "name":{
            System.out.println("用户的姓名为:"+yonghu.getName());
            System.out.println("输入exit结束程序。。。。。。");
            break;
        }
        case "balance":{
            System.out.println("用户的当前余额为:"+yonghu.getBalance());
            System.out.println("输入exit结束程序。。。。。。");
            break;
        }
        case "exit":{
            System.exit(0);
            System.out.println("程序关闭正常");
        }
        default:{
            System.out.println("输入错误请重新输入:");
            System.out.println("输入exit结束程序。。。。。。");
            break;
        }
        }
        }
        /*System.out.println(yonghu.getId());
        System.out.println(date);*/
        in.close();
        sr.close();
    }

}

实验结果

1580249-20190918110537590-413970581.png

总结:

这次的实验总体来说还算可以,第一题没什么问题,第二题有一点问题,开始定义了一些在Account类中的方法,然后想在另一个类中使用,开始不能用,提示说要定义为static属性,然后想了一下定义为static那些set跟get就没用了,后面一想,可以用对象调用,把这个问题解决了,后面又出了一个在switch中yonghu这个对象要为空,出现了这么一个错误,后来把对象的实例化放到前面去就可以了,没有这个错误了,这个题目还有一些没有实现,比如是否开户,开户了每一个人的账户内容保存在那,不能这个程序只能保存一个的吧,还有产生的随机账户,如果后面有重复账户怎么办,本来还想把它优化一下的,想用对象数组来保存每一个对象,但是这个题目在周二我就做好了,所以也不想改了,时间我是不知道怎么保存。

第四周总结:

本周主要讲了String类,还讲了对象数组,以及Java数组的定义。

String类

我们一直使用String定义字符串,一种是直接赋值,还有一种是调用String的构造方法,两种赋值方法效果类似。但有一些区别,直接赋值是如果开辟了一个堆空间来保存这个字符串,那么后面又出现这个字符串的赋值,就不会再开辟空间,而构造方法则是开辟了两个空间,甚至更多
String对象内容的比较,如果一个直接赋值,和一个用构造方法赋值的一样的字符串,进行直接比较,会出现false
1580249-20190919141207261-310316075.png
1580249-20190919141332495-1250597770.png

所以要用eauals()方法比较这个方法只比较内容,但是会区分大小写。
字符串一旦声明就不可改变
1580249-20190919141805080-677610241.png

还有一些String类的一些常用操作方法
1580249-20190919141950025-1994796791.png

对象数组

所谓对象数组,就是包含了一组相关的对象数组,数组一定要先开辟空间,因为其是引用数据类型,所以数组每一个对象都是null值,则再引用对象数组时都要对其进行实例化操作
1580249-20190919142813122-1687925500.png

1580249-20190919142817820-1892143957.png
1580249-20190919142852906-1807138370.png

1580249-20190919142858260-1502211018.png

转载于:https://www.cnblogs.com/xiao--liang/p/11540504.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本次手写数字识别实验使用了深度学习框架TensorFlow,实现了对手写数字图片的识别。以下是实验报告。 ## 实验目的 通过训练神经网络模型,实现对手写数字图片的自动识别,提高人工智能的应用水平。 ## 实验过程 ### 数据集准备 使用MNIST数据集,该数据集包含60000张训练图像和10000张测试图像,每张图片大小为28*28像素,共10个类别(数字0~9)。首先需要将数据集下载到本地,并进行预处理。 ```python import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # 导入数据集 mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # 分别获取训练集、验证集和测试集 train_data = mnist.train.images train_label = mnist.train.labels valid_data = mnist.validation.images valid_label = mnist.validation.labels test_data = mnist.test.images test_label = mnist.test.labels ``` ### 模型构建 使用TensorFlow搭建卷积神经网络模型,包括两个卷积层、两个池化层和一个全连接层。 ```python # 创建输入占位符 x = tf.placeholder(tf.float32, [None, 784]) y_true = tf.placeholder(tf.float32, [None, 10]) # 将输入数据转换为图像格式 x_image = tf.reshape(x, [-1, 28, 28, 1]) # 第一层卷积层 conv1 = tf.layers.conv2d(inputs=x_image, filters=32, kernel_size=[5, 5], padding="same", activation=tf.nn.relu) # 第一层池化层 pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2) # 第二层卷积层 conv2 = tf.layers.conv2d(inputs=pool1, filters=64, kernel_size=[5, 5], padding="same", activation=tf.nn.relu) # 第二层池化层 pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2) # 将卷积层输出展开为一维向量 flatten = tf.reshape(pool2, [-1, 7*7*64]) # 全连接层 fc = tf.layers.dense(inputs=flatten, units=1024, activation=tf.nn.relu) # 输出层 y_pred = tf.layers.dense(inputs=fc, units=10) ``` ### 模型训练 使用交叉熵作为损失函数,Adam优化器进行模型训练。 ```python # 定义损失函数和优化器 cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_pred, labels=y_true)) train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) # 计算准确率 correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y_true, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # 创建Session并运行模型 sess = tf.Session() sess.run(tf.global_variables_initializer()) for i in range(10000): batch_data, batch_label = mnist.train.next_batch(50) sess.run(train_step, feed_dict={x: batch_data, y_true: batch_label}) if i % 100 == 0: train_accuracy = sess.run(accuracy, feed_dict={x: train_data, y_true: train_label}) print("step %d, training accuracy %g" % (i, train_accuracy)) ``` ### 模型评估 使用测试集对模型进行评估,计算准确率。 ```python test_accuracy = sess.run(accuracy, feed_dict={x: test_data, y_true: test_label}) print("test accuracy %g" % test_accuracy) ``` ## 实验结果 经过10000次迭代的训练,最终模型在测试集上的准确率为98.3%。 ## 实验结论 本次实验使用TensorFlow搭建了卷积神经网络模型,实现了对手写数字图片的识别。通过实验结果可以看出,该模型在MNIST数据集上表现较好,可以应用于实际场景中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值