Java面向对象程序设计实验报告
序号:实验三
学号 | 姓名 | 班级 | ||||
座号 | 指导教师 | 提交时间 | ||||
一、实验目的和要求 二、实验环境及要求 1、 Eclipse 2、 Window XP 三、实验项目 1.Pre-lab Exercises(阅读) 理解String, Random, 和 Math的类的使用 2.Exercises 1)根据a,b,c,d,e的提示填空 a声明变量town,初始化成"Anytown, USA" String town = new String ("Anytown USA"); b调用length方法求变量college的长度,将结果保存在变量stringLength里 stringLength = college.length(); c将college转成大写存放在change1中 change1 = ("PODUNK COLLEGE"); d将change1中的大写O替换成*存放在change2中 change2 = ("P*DUNK C*LLEGE"); e将college和town连接(使用concat方法)存放在change3中 change3 = college + town; 2)以下程序给直角三角形两条直角边的长度(通过键盘输入),求斜边长度(需要用到Math类) 实验结果如图1所示: 图1 3)随机数的产生与使用,请填空 lucky1 = Int(30) + 50; lucky2 = Int(11) + 90; lucky3 = Float() * 30; lucky3 = lucky3 + 11; 3.Working with Strings(选做) 运行程序,按如下要求修改: 1)声明一个String变量middle3,用substring方法取phrase中间的三个字母,加一条输出语句输出middle3. 2)将switchedPhrase中的空格替换成*,最终仍保存在switchedPhrase中 3)声明两个变量city和state,加语句让用户输入city和state,输出结果。 4.Rolling Dice 写一个程序实现打骰子,对两个骰子随机生成1-6之间的数字,分别输出两个骰子单独的结果和总的结果。你必须要使用到Ramdom类,可以参考例题3.2。 实验结果如同2所示: 图2 5.Computing Distance 文件Distance.java用来计算两点之间的距离,用公式(x1 – x2)2+ (y1 – y2)2来计算。你需要添加赋值语句计算距离然后输出。用以下数据测试你的程序:(3, 17)和 (8, 10) 距离是 8.6023 实验结果截图如图3所示: 图3 6.Formatting Output 文件Deli.java用来计算在熟食店买东西的花费 1)理解程序 2)加一条import语句用来引入DecimalFormat 和 NumberFormat 类 3)根据注释加一条语句声明NumberFormat的对象money 4)根据注释加一条语句声明DecimalFormat的对象fmt 5)添加语句输出以下格式(用money输出单价和总价,用fmt输出重量)。(注意:我们所在的地区是中国,所以以下所有的$会被¥取代。) ***** CS Deli ***** import语句 Unit Price: $4.25 per pound Weight: 2.56 pounds TOTAL: $10.89 实验结果截图如图4所示: 图4 7.Playing With Cards(选做) 定义枚举类型Rank,取值为ace, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king,主要内容包括: 声明Rank类型变量highCard, faceCard, card1 和 card2 1)将highCard赋为ace,faceCard赋为jack,queen或者king(任取一个),card1 和 card2赋为其余的卡(任取一个) 2)输出一行,使用highCard和faceCard对象,格式如下: A blackjack hand: ace and ..... 3)声明两个整型变量card1Val 和 card2Val,将他们分别赋值为card1 和 card2的序数值。 4)调用card1 和 card2对象,输出如下数据 A two card hand: (print card1 and card2) Hand value: (print the sum of the face values of the two cards) 8.Experimenting with the Integer Class 写一个程序IntWrapper,使用常量和Integer类,实现以下的要求(参考P140和P819): 1)提示和读入一个整型,输出二进制(toBinaryString)、八进制和十六进制 2)输出Java整型的最大值和最小值。 3)提示用户输入两个十进制整数,用Scanner 类的next方法读入,将读入后的strings类型转成int,最后输出两数之和sum 9.Nested Panels 运行NestedPanels.java,完成如下内容: 1)编译和运行程序,调整窗口大小并观察效果 2)修改程序,加一个子面板(与另两个子面板等高,宽度为两倍),你可以自己选择label 和color,将新加入的子面板加到主面板中。 3)编译和运行程序 4)添加语句将主面板初始大小设置为320 * 260 添加一个新面板(背景颜为蓝,大小为320 * 20),添加一个显示"My Panels"的label到该面板中,然后把新加入的面板添加到主面板中。 | ||||||
教师评语: 签字: 日期: | 成绩: | |||||
实验报告代码清单
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 the 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 = Double();
System.out.print ("side2 = ");
side2 = Double();
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 = ");
num1 = Int(6) + 1;
System.out.println(num1);
System.out.print("num2 = ");
num2 = Int(6) + 1;
System.out.println(num2);
System.out.println("The total of two numbers = " + (num1 + num2));
}
}
6.
import java.util.Scanner;
NumberFormat;
DecimalFormat;
public 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 weightOunces; // weight in ounces
double weight; // weight in pounds
double totalPrice; // total price for the item
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论