Java实验4.docx

上传人:b****4 文档编号:4188751 上传时间:2023-05-06 格式:DOCX 页数:9 大小:68.27KB
下载 相关 举报
Java实验4.docx_第1页
第1页 / 共9页
Java实验4.docx_第2页
第2页 / 共9页
Java实验4.docx_第3页
第3页 / 共9页
Java实验4.docx_第4页
第4页 / 共9页
Java实验4.docx_第5页
第5页 / 共9页
Java实验4.docx_第6页
第6页 / 共9页
Java实验4.docx_第7页
第7页 / 共9页
Java实验4.docx_第8页
第8页 / 共9页
Java实验4.docx_第9页
第9页 / 共9页
亲,该文档总共9页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

Java实验4.docx

《Java实验4.docx》由会员分享,可在线阅读,更多相关《Java实验4.docx(9页珍藏版)》请在冰点文库上搜索。

Java实验4.docx

Java实验4

Java实验4

Java面向对象程序设计实验报告

序号:

实验三

学号

姓名

班级

座号

指导教师

提交时间

一、实验目的和要求

二、实验环境及要求

1、Eclipse

2、WindowXP

三、实验项目

PrelabExercises(选做)

ABankAccountClass

1.根据以下要求完善Account.java,注意在未完成ManageAccounts.java前程序无法运行。

⏹完善toString方法,返回一个字符串包含name,accountnumber,balance.

publicStringtoString()

{

return"name:

"+this.name+"accountname:

"+this.acctNum+"balance:

"+this.balance;

}

⏹完善chargeFee方法,从账户中扣除10元的服务费

publicvoidchargeFee()

{

this.balance-=10;

}

⏹修改chargeFee方法,原方法返回值类型为void,修改后让它返回新的balance

⏹完善changeName方法,用string类型作为参数名来修改账户名

publicvoidchangeName(StringnewName)

{

this.name=newName;

}

2.ManageAccounts.java中调用Account.java类,根据注释完善程序

⏹为Joe创建对象account2,初始账户余额为500

acct2=newAccount(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();

acct1.chargeFee();

⏹将Joe的姓名修改为Joseph

acct2.changeName("Joseph");

⏹调用toString()方法输出Joe和Sally账户信息

System.out.println(acct1.toString());

System.out.println(acct2.toString());

3.修改ManageAccounts.java使它在调用chargeFees方法后输出直接输出balance(余额),你可以通过println方法输出变量balance的值。

TrackingGrades

老师想创建一个Student类来追踪学生的成绩:

⏹学生由三部分数据组成:

姓名、第一门课程的成绩、第二门课程的成绩。

⏹有一个构造方法,将姓名作为构造方法的参数

Test2:

91

BandBoosterClass(音乐助推器,音乐帮助糖果的销售)(选做)

写一个BandBooster类更新糖果销售

1.写一个BandBooster类,它的对象包含两个数据成员:

name(aString)和boxesSold(aninteger),方法如下:

⏹有一个单参数的构造方法,参数是BandBooster的名称,构造方法将boxesSold设置为0

⏹getName方法放回name值(无参数)

⏹updateSales方法有一个整型参数表示额外的销售量,将额外销售加总到boxesSold中

⏹toString方法返回一个字符串包含name和boxesSold,如:

Joe:

16boxes

2.写一个程序调用BandBooster对象追踪2段音乐在3周时间内的销售状况,完成以下几点内容:

⏹读取这两段音乐的名称,为它们各建一个对象

⏹给用户提示并读取每段音乐在三周的销售量,你的提示必须包括音乐的名称,如:

EnterthenumberofboxessoldbyJoethisweek:

,对输入的周销售量调用updateSales方法更新boxesSold

⏹输出name和boxesSold(你会自动调用toString方法)

RepresentingNames

1.编写一个Name类存储一个人的first,middle,和lastnames,提供以下方法:

⏹publicName(Stringfirst,Stringmiddle,Stringlast)构造方法,根据参数设置名字

⏹publicStringgetFirst()返回firstnames

⏹publicStringgetMiddle()返回middlename

⏹publicStringgetLast()返回lastname

⏹publicStringfirstMiddleLast()返回一个人的全名,如"MaryJaneSmith"

⏹publicStringlastFirstMiddle()返回一个人的全名,先出现lastname,如"Smith,MaryJane"

⏹publicbooleanequals(NameotherName)如果当前name和otherName一致返回true,要求不区分大小写(提示:

String对象可以调用equalsIgnoreCase方法,使得两个String比较不区分大小写,如:

string1.equalsIgnoreCase(string2))

⏹publicStringinitials()返回人名得缩写(first,middle,和lastnames的首字母,共3个字母),必须全是大写字母(提示:

不使用charAt方法,使用substring方法得到字符串的子串,然后调用toUpperCase方法转成大写,详见P119)

⏹publicintlength()返回人名中的字母数,不含空格

2.编写一个TestNames.java,提示用户读入两个名字(各自需要first,middle,和lastnames),为两个名字各自创建一个Name类对象,调用Name类的如下方法:

a.对每个名字,输出

⏹first-middle-last

⏹last-first-middle

⏹initials

⏹length

b.判断两个名字是否相同

DrawingSquares(选做)

VotingwithButtons

文件VoteCounter.java和VoteCounterPanel.java是书中例题4.10和4.11的PushCounter.javaandPushCounterPanel.java的修改版。

修改了一些变量名,如voteforJoe。

1.编译运行程序

2.修改程序,使得可以给两个候选人投票Joe和Sam

a.为Sam添加变量:

avotecounter(计票器),abutton(按钮),andalabel(单行文本)

b.添加一个新的内部类SamButtonListener,监听Sam的button,实现actionPerformed方法。

c.在面板中添加Sam的button和label

3.编译运行程序

CalculatingBodyMassIndex

BodyMassIndex(BMI)指数是衡量体重的一个重要指标,如果BMI指数高于25被认为是超重,BMI的计算公司如下:

(703*身高(英寸))/(体重(磅))2

文件BMI.java和BMIPanel.java只包含程序框架,使用方法类似例题4.12和4.13,请根据注释完成程序。

 

教师评语:

签字:

日期:

成绩:

实验报告代码清单

2.

2)

importjava.util.Scanner;

publicclassRightTriangle{

publicstaticvoidmain(String[]args)

{

doubleside1,side2,num;//lengthsofthesidesofarighttriangle

doublehypotenuse;//lengthofthehypotenuse

Scannerscan=newScanner(System.in);

//Getthelengthsofthesidesasinput

System.out.println("Pleaseenterthelengthsofthetwosidesof"+

"arighttriangle(separatebyablankspace):

");

System.out.print("side1=");

side1=scan.nextDouble();

System.out.print("side2=");

side2=scan.nextDouble();

num=side1*side1+side2*side2;

hypotenuse=Math.sqrt(num);

System.out.println("Lengthofthehypotenuse:

"+hypotenuse);}

}

4.

importjava.util.Random;

publicclassDice

{

publicstaticvoidmain(String[]args)

{

Randomgenerator=newRandom();

intnum1,num2;

System.out.print("num1=");

num1=generator.nextInt(6)+1;

System.out.println(num1);

System.out.print("num2=");

num2=generator.nextInt(6)+1;

System.out.println(num2);

System.out.println("Thetotaloftwonumbers="+(num1+num2));

}

}

6.

importjava.util.Scanner;

importjava.text.NumberFormat;

importjava.text.DecimalFormat;

publicclassDeli

{

//---------------------------------------------------

//mainreadsinthepriceperpoundofadeliitem

//andnumberofouncesofadeliitemthencomputes

//thetotalpriceandprintsa"label"fortheitem

//--------------------------------------------------

publicstaticvoidmain(String[]args)

{

finaldoubleOUNCES_PER_POUND=16.0;

doublepricePerPound;//priceperpound

doubleweightOunces;//weightinounces

doubleweight;//weightinpounds

doubletotalPrice;//totalpricefortheitem

Scannerscan=newScanner(System.in);

NumberFormatmoney=NumberFormat.getCurrencyInstance();

DecimalFormatfmt=newDecimalFormat("0.###");

//DeclaremoneyasaNumberFormatobjectandusethe

//getCurrencyInstancemethodtoassignitavalue

//DeclarefmtasaDecimalFormatobjectandinstantiate

//ittoformatnumberswithatleastonedigittotheleftofthe

//decimalandthefractionalpartroundedtotwodigits.

//prompttheuserandreadineachinput

System.out.println("WelcometotheCSDeli!

!

\n");

System.out.print("Enterthepriceperpoundofyouritem:

");

pricePerPound=scan.nextDouble();

System.out.print("Entertheweight(ounces):

");

weightOunces=scan.nextDouble();

//Convertouncestopoundsandcomputethetotalprice

weight=weightOunces/OUNCES_PER_POUND;

totalPrice=pricePerPound*weight;

//Printthelabelusingtheformattingobjects

//fmtfortheweightinpoundsandmoneyfortheprices

System.out.println("*****CSDeli*****");

System.out.println();

System.out.println("UnitPrice:

¥"+pricePerPound+"perpound");

System.out.println("Weight:

"+weight+"pounds");

System.out.println("Total:

¥"+totalPrice);

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2