ImageVerifierCode 换一换
格式:DOCX , 页数:8 ,大小:18.70KB ,
资源ID:4667201      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/4667201.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Java实验4.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Java实验4.docx

1、Java实验4Java实验4Java面向对象程序设计实验报告序号:实验三学号姓名班级座号指导教师提交时间一、实验目的和要求二、实验环境及要求1、 Eclipse2、 Window XP三、实验项目Prelab Exercises(选做)A Bank Account Class1.根据以下要求完善Account.java,注意在未完成ManageAccounts.java前程序无法运行。完善toString方法,返回一个字符串包含name, account number, balance.public String toString() return name: + this.name + ac

2、countname: + this.acctNum + balance: + this.balance;完善chargeFee方法,从账户中扣除10元的服务费public void chargeFee() this.balance -= 10;修改chargeFee方法,原方法返回值类型为void,修改后让它返回新的balance完善changeName方法,用string类型作为参数名来修改账户名public void changeName(String newName) this.name = newName;2.ManageAccounts.java中调用Account.java类,根据

3、注释完善程序为Joe创建对象account2,初始账户余额为500acct2 = new Account (500,Joe,2222);为Joe存100元acct2.deposit(100);调用getBanlance()方法输出Joe的余额System.out.println(acct2.getBalance();从Sally账户中取50元acct1.withdraw(50);调用getBanlance()方法输出Sally的余额System.out.println(acct1.getBalance();对Joe和Sally账户调用chargeFee()方法acct2.chargeFee()

4、; acct1.chargeFee();将Joe的姓名修改为Josephacct2.changeName(Joseph);调用toString()方法输出Joe和Sally账户信息System.out.println(acct1.toString(); System.out.println(acct2.toString();3.修改ManageAccounts.java使它在调用chargeFees方法后输出直接输出balance(余额),你可以通过println方法输出变量balance的值。Tracking Grades老师想创建一个Student类来追踪学生的成绩:学生由三部分数据组成:

5、姓名、第一门课程的成绩、第二门课程的成绩。有一个构造方法,将姓名作为构造方法的参数三个方法:getName方法,返回学生的姓名;inputGrades方法,提示和读入学生的成绩;getAverage方法,计算和返回学生的平均成绩。1.Student.java包含一个不完整的学生类定义,根据以下要求完善程序a.声明类变量(姓名、第一门课程的成绩test1、第二门课程的成绩test2)b.创建Scanner类的对象读取成绩c.添加缺失的方法头(getAverage方法和printName方法)d.添加缺失的方法内容2.文件Grades.java声明两个学生对象,调用inputGrades方法读取学

6、生的成绩,调用getAverage方法求平均成绩,参考The average for Joe is 87.格式输出学生姓名和成绩,可以通过getName方法取得学生姓名。3.在Grades.java中添加语句使得可以直接输出Student对象,如:System.out.println(Student 1: + student1);可以通过添加toString方法实现以上功能,返回学生姓名和成绩,如:Name: Joe Test1: 85 Test2: 91Band Booster Class(音乐助推器,音乐帮助糖果的销售)(选做)写一个Band Booster类更新糖果销售1.写一个Band

7、Booster类,它的对象包含两个数据成员:name (a String)和boxesSold (an integer),方法如下:有一个单参数的构造方法,参数是BandBooster的名称,构造方法将boxesSold设置为0getName方法放回name值(无参数)updateSales方法有一个整型参数表示额外的销售量,将额外销售加总到boxesSold中toString方法返回一个字符串包含name和boxesSold,如:Joe: 16 boxes2.写一个程序调用BandBooster对象追踪2段音乐在3周时间内的销售状况,完成以下几点内容:读取这两段音乐的名称,为它们各建一个对象

8、给用户提示并读取每段音乐在三周的销售量,你的提示必须包括音乐的名称,如:Enter the number of boxes sold by Joe this week:,对输入的周销售量调用updateSales方法更新boxesSold输出name和boxesSold(你会自动调用toString方法)Representing Names1.编写一个Name类存储一个人的first, middle, 和 last names,提供以下方法:public Name(String first, String middle, String last) 构造方法,根据参数设置名字public Str

9、ing getFirst() 返回first namespublic String getMiddle() 返回middle namepublic String getLast() 返回 last namepublic String firstMiddleLast() 返回一个人的全名,如Mary Jane Smithpublic String lastFirstMiddle() 返回一个人的全名,先出现last name,如Smith, Mary Janepublic boolean equals(Name otherName) 如果当前name和otherName一致返回true,要求不区

10、分大小写(提示:String对象可以调用equalsIgnoreCase方法,使得两个String比较不区分大小写,如:string1. equalsIgnoreCase(string2))public String initials() 返回人名得缩写(first, middle, 和 last names的首字母,共3个字母),必须全是大写字母(提示:不使用charAt方法,使用substring方法得到字符串的子串,然后调用toUpperCase方法转成大写,详见P119)public int length() 返回人名中的字母数,不含空格2.编写一个TestNames.java,提示用

11、户读入两个名字(各自需要first, middle, 和 last names),为两个名字各自创建一个Name类对象,调用Name类的如下方法:a.对每个名字,输出first-middle-lastlast-first-middleinitialslengthb.判断两个名字是否相同Drawing Squares(选做)Voting with Buttons文件VoteCounter.java 和 VoteCounterPanel.java是书中例题4.10 和 4.11的PushCounter.java and PushCounterPanel.java的修改版。修改了一些变量名,如vot

12、e for Joe。1.编译运行程序2.修改程序,使得可以给两个候选人投票Joe 和 Sama.为Sam添加变量:a vote counter(计票器), a button(按钮), and a label(单行文本)b.添加一个新的内部类SamButtonListener,监听Sam的button,实现actionPerformed方法。c.在面板中添加Sam的button和label3.编译运行程序Calculating Body Mass IndexBody Mass Index(BMI)指数是衡量体重的一个重要指标,如果BMI指数高于25被认为是超重,BMI的计算公司如下:(703 *

13、 身高(英寸)) / (体重(磅))2文件BMI.java 和 BMIPanel.java只包含程序框架,使用方法类似例题4.12 和 4.13,请根据注释完成程序。教师评语:签字:日期:成绩:实验报告代码清单2.2)import java.util.Scanner;public class RightTriangle public static void main (String args) double side1, side2,num; / lengths of the sides of a right triangle double hypotenuse; / length of th

14、e hypotenuse Scanner scan = new Scanner(System.in); / Get the lengths of the sides as input System.out.println (Please enter the lengths of the two sides of + a right triangle (separate by a blank space): ); System.out.print (side1 = ); side1 = scan.nextDouble(); System.out.print (side2 = ); side2 =

15、 scan.nextDouble(); num = side1*side1 + side2*side2; hypotenuse = Math.sqrt(num); System.out.println (Length of the hypotenuse: + hypotenuse);4.import java.util.Random;public class Dice public static void main (String args) Random generator = new Random(); int num1,num2; System.out.print(num1 = ); n

16、um1 = generator.nextInt(6) + 1; System.out.println(num1); System.out.print(num2 = ); num2 = generator.nextInt(6) + 1; System.out.println(num2); System.out.println(The total of two numbers = + (num1 + num2); 6.import java.util.Scanner;import java.text.NumberFormat;import java.text.DecimalFormat;publi

17、c class Deli/ -/ main reads in the price per pound of a deli item/ and number of ounces of a deli item then computes/ the total price and prints a label for the item/ - public static void main (String args) final double OUNCES_PER_POUND = 16.0; double pricePerPound; / price per pound double weightOu

18、nces; / weight in ounces double weight; / weight in pounds double totalPrice; / total price for the item Scanner scan = new Scanner(System.in); NumberFormat money = NumberFormat.getCurrencyInstance(); DecimalFormat fmt = new DecimalFormat(0.#);/ Declare money as a NumberFormat object and use the/ ge

19、tCurrencyInstance method to assign it a value/ Declare fmt as a DecimalFormat object and instantiate/ it to format numbers with at least one digit to the left of the/ decimal and the fractional part rounded to two digits./ prompt the user and read in each input System.out.println (Welcome to the CS

20、Deli!n ); System.out.print (Enter the price per pound of your item: ); pricePerPound = scan.nextDouble(); System.out.print (Enter the weight (ounces): ); weightOunces = scan.nextDouble();/ Convert ounces to pounds and compute the total price weight = weightOunces / OUNCES_PER_POUND; totalPrice = pri

21、cePerPound * weight;/ Print the label using the formatting objects/ fmt for the weight in pounds and money for the prices System.out.println(* CS Deli *); System.out.println(); System.out.println(Unit Price: ¥+pricePerPound + per pound); System.out.println(Weight: +weight + pounds); System.out.println(Total: ¥ + totalPrice);

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1