Java实验报告_实验二
java实验报告_2

实验一1.编写一个程序,在屏幕上显示如下信息:**************************welcome<你的名字>**************************想一想:怎样让用户在运行程序的时候指定“你的名字”public class Experiment1_1{public static void main(String[]args){System.out.println("*************************");System.out.println("welcome宁");System.out.println("*************************");}}2.写一个Java程序打印出下列信息:☐姓名☐性别☐年龄☐学号☐系和专业☐兴趣爱好public class Experiment1_2{public static void main(String args[]){System.out.println("姓名");System.out.println("性别");System.out.println("年龄");System.out.println("学号");System.out.println("系和专业");System.out.println("兴趣爱好");}}3.编写一个程序,使用while循环计算1~1000之间能被3和7同时整除的整数之和public class Experiment1_3{public static void main(String[]args){int i=1;int sum=0;while(i<=1000){if(i%7==0&&i%3==0)sum+=i;i++;}System.out.print("sum="+sum);}}实验二1.有一函数,编写一个程序,从键盘输入一个x 值,程序输出y 的值import java.util.Scanner;public class Experiment2_1{public static void main(String[]args){Scanner reader=new Scanner(System.in);int x=reader.nextInt();if(x<0)System.out.println(-1+2*x);else if(x==0)System.out.println(-1);elseSystem.out.println(-1+3*x);}}2.编写一个程序,使用for 循环计算8+88+888+8888+…的前十项之和要求:在上述程序中设置断点和观察点进行单步调试public class Experiment2_2{public static void main(String[]args){int sum=0;int temp=8;for(int i=1;i<=10;i++){sum+=temp;temp=temp*10+8;}System.out.println("sum="+sum);}}3.利用for 循环打印9*9表1*1=11*2=22*2=41*3=32*3=63*3=91*4=42*4=83*4=124*4=161*5=52*5=103*5=154*5=205*5=251*6=62*6=123*6=184*6=245*6=306*6=36⎪⎩⎪⎨⎧>+-=-<+-=03101021x x x x x y1*7=72*7=143*7=214*7=285*7=356*7=427*7=491*8=82*8=163*8=244*8=325*8=406*8=487*8=568*8=641*9=92*9=183*9=274*9=365*9=456*9=547*9=638*9=729*9=81要求:对上述程序中的方法和语句加上注释public class Experiment2_3{public static void main(String[]args){for(int i=1;i<10;i++){for(int j=1;j<=i;j++)System.out.printf("%4d*%d=%d",i,j,j*i);System.out.print('\n');}}}4.从键盘输入一个百分制的成绩,输出相应的等级。
java实验报告_2

Ch4类和对象练习题之方法一简答题1、如果在一个返回值的方法中,不写return语句会发生什么错误?在返回值类型为void的方法中可以有return语句吗?下面的方法中的return语句是否会导致语法错误?public static void xMethod(double x,double y){System.out.println(x+y);return x+y;}(1)没有返回值(2)不可以(3)会2、什么是值传递?给出下面程序运行的结果,分别给出调用max之前、刚进入max方法、max方法刚要返回之前以及max方法返回之后堆栈的内容。
public class Test{public statci void main(String[] args){int max=0;max(1,2,max);System.out.println(max);}public static void max(int value1,int value2,int max){if(value1>value2)max=value1;elsemax=value2;}}(1)值传递是指在调用函数时将实际参数复制一份传递到函数中,这样在函数中如果对参数进行修改,将不会影响到实际参数。
(2)空,max=0,max=0,空3、描述传递基本类型参数和传递引用类型参数的区别,给出下面程序的输出。
public class Test{public static void main(String[] args){Count myCount=new Count();int times=0;for(int i=0;i<100;i++)increment(myCount,times);S ystem.out.println(“count is”+myCount.count);System.out.println(“times is”+times);}public static void increment(Count c,int times){c.count++;times++;}}public class Count{public int count;public Count(int c){count=c;}public Count(){count=1;}}(1)基本类型是值传递(2)引用类型是引用传递。
java实验报告 2

实验报告课程:Java 编程技术班级:网络1203班学号:姓名:实验4 面向对象编程一、实验目的通过编程和上机实验理解Java 语言是如何体现面向对象编程基本思想,了解类的封装方法,以及如何创建类和对象,了解成员变量和成员方法的特性,掌握OOP 方式进行程序设计的方法,了解类的继承性和多态性的作用。
二、实验内容1.创建一个名为Dog的类,它具有重载的bark()方法。
bark()方法应根据不同的基本数据类型的参数来进行重载,bark()方法可根据参数输出狗吠(barking)、咆哮(howling)等信息。
编写main()方法来调用不同的bark()方法。
源代码如下:public class Dog {void f(int m){System.out.println("barking!");}void f(double n){System.out.println("hoeling!");}public static void main(String[] args){Dog d=new Dog();d.f(2);d.f(2.2);}}运行界面如下:2.创建Rodent(啮齿动物)类,其子类有Mouse(老鼠)、Mole(鼹鼠)、Hamster(大颊鼠)。
在父类中,提供对所有的Rodent通用的方法。
在子类中,根据该子类特定的行为习性来覆盖这些方法。
例如老鼠属杂食(omnivorous),鼹鼠主食昆虫(insect),大颊鼠主食植物种子(plant seed)。
创建一个Rodent 数组,填充不同的数据类型,然后调用父类的方法,观察会发生什么情况。
源代码如下:public class Rodent {void eat(){System.out.println("zhushi");}public static void main(String[] args){Rodent r[]=new Rodent[4];Rodent rodent=new Rodent();Mouse mouse=new Mouse();Mole mole=new Mole();Hamster hamster=new Hamster();r[0]=rodent;r[1]=mouse;r[2]=mole;r[3]=hamster;r[0].eat();r[1].eat();r[2].eat();r[3].eat();}}class Mouse extends Rodent{void eat(){System.out.println("omniovrous!");}}class Mole extends Rodent{void eat(){System.out.println("insect!");}}class Hamster extends Rodent{void eat(){System.out.println("plant seed!");}}运行界面如下:3.3.修改上述第9题中的Rodent类,使其成为一个抽象类。
JAVA实验报告 (2)

附件2:实验报告封皮2015—2016学年第1学期Java应用程序设计课程实验报告学院:计算机科学技术专业:软件工程班级:14402姓名:邵磊学号:041440230任课教师:王薇实验日期:2015年11月02日实验题目Java简介及开发环境搭建实验内容1.熟悉Java开发环境2.Java程序的编辑和运行实验目的1.熟悉Java开发环境(1)熟悉JDK工具包的下载及安装过程。
(2)掌握JAVA_HOME、CLASSPATH及Path的配置内容。
(3)掌握Java程序运行原理及javac、java命令的使用。
2.Java程序的编辑和运行(1)熟悉用记事本编写Java程序的过程。
(2)了解Java程序的基本结构。
(3)掌握javac及java命令的使用。
(4)熟悉MyEclipse集成开发环境的使用。
实验步骤1.熟悉Java开发环境(1)登录Oracle官方网站Java首页/technetwork/java/index.html并下载最新版JDK工具包。
(2)将JDK工具包安装在D:\java\jdk1.6.0_18\文件夹中。
(3)完成JDK环境配置。
创建JAVA_HOME变量并设置其值为”D:\java\jdkl1.6.0_18”,创建CLASSPATH 变量并设置其值为”D:\java\jdkl1.6.0_18\lib”文件夹中的dt.jar、tools.jar及当前目录,在Path变量原有值的基础上增加”D:\java\jdk1.6.0_18\bin”。
(4)验证JDK是否配置正确。
2.Java程序的编辑和运行(1)创建D:\javaExecise文件夹。
(2)设置显示已知文件夹类型的扩展名。
(3)利用记事本完成Java程序的编写。
(4)利用命令编译运行Javs程序(5)在MyEclipse中编译并运行Java程序实验结果1.熟悉Java开发环境单击【开始】|【运行】命令打开【运行】对话框。
JAVA实验报告--6个实验

three=newComputerAver(four);
two=newDelScore(three);
one=newInputScore(two);
}
publicvoidgivePersonScore(){
one.inputScore();
}
}
publicclassComputerAver {
姓名
学号
实验组
实验时间
指导教师
吴红梅
成绩
实验项目名称
上机实践二 运算符、表达式和语句
实验环境
Eclipse
实验内容及结果
实验一托运行李
charge=(int)weight*trainCharge;
charge=(int)(weight+0.5)*carCharge;
charge=(int)(weight*1000+0.5)*planeCharge;
运行结果:tank1的炮弹数量:9
tank2的炮弹数量:8
实验三家族的姓氏
【代码1】FamilyPerson.surname="李";
【代码2】father.setName("向阳");
【代码3】father.setSurname("张");
运行结果:父亲:张向阳
大儿子:张抗日
二儿子:张抗战
指导教师意见
姓名
学号
实验组
实验时间
指导教师
吴红梅
成绩
实验项目名称
实验环境
Eclipse
实验内容及结果
实验一Tank类
【代码1】double speed;
java实验报告 (2)

4、程序及运行结果(或实验数据记录及分析) import java.util.*; import java.util.Calendar; class Birth {
private int year; private int month; private int day; public int getyear() {
三角形"); num=in.nextInt(); switch(num) { case 1:
System.out.printf("请输入等腰三角形的边长:"); m=in.nextInt(); r=in.nextInt(); d(r,m); break;
case 2:
System.out.printf("请输入直角三角形的边长:"); m=in.nextInt(); f(m); break;
掌握基本输入输出语句;7)掌握函数调用及参数传递
2. 熟悉并掌握结构化方法的自顶向下逐步求精思想;
2、 实验原理和内容 实验内容:
1. 输入 1、2、3、4 等标识,分别使用*号打印等腰三角形、直角三角形、长 方形、正方形等不同形状;
2. 必须使用 Scanner 类实现从键盘输入; 3. 打印不同形状使用不同的功能函数; 4. 提高 1:使用递归方式编写打印形状;
age is 12 years old. 提示:
Date 是系统定义的日期类,通过其 getYear()方法可以获得当前年份。
3、实验步骤 1、定义私有变量 year,month,day。 2、在 class 类中定义无参构造函数,对 year,month,day 进行初始化。 3、定义有参构造函数。 4、键盘输入出生日期。 5、主函数创建 Calendar 对象获取当前年份,创建 Birth 类。
JAVA实验报告_实验2__
Java Object-Oriented Programming Experimental ReportStudent ID Name ClassLocation Teacher Time1.Experimental purposes and requirements1.1 Print and println, String literals, String concatenation, Escape sequences1.2 Variables, Constants, Assignment, Integers and Floating point, Arithmetic Expressions, Operator Precedence, Input using the Scanner class1.3 HTML, Applets, Graphics, Colors2.Experimental equipment (environment) and requirements2.1 Eclipse2.2 Window XPb Exercises(Please see Lab Manual)3.1 Names and Places(Choose to do)3.1.13.1.23.1.33.2 Table of Student Grades(Choose to do)/\\\\\\\\\\\\\\\\\\\\\\\ (1)(2)(3)3.3 Two Meanings of Plus(1)(2)(3)解释语句一:第一个输出结果是85,没加括号从左到右运算前第一句为字符串则8和5为字符串,有85(4)解释语句二:第二个输出结果为13,在输出语句中默认优先进行带括号的运算,8和5为整型,变成数字之后在从左到右运算,左面第一个为字符串则运算完的13也为字符型(5)解释语句三:无括号,从左到右运算,其中第三句为字符串则从左到右全识别为字符型则853.4 Pre-lab Exercises(1)常量是静态变量,不会更改,全程序共用同一个常量名变量可以在不同类中同时存在不属于本类的变量名,值可以不同(2)第一句:定义变量x第二句:定义整型变量x值为3第三句:啥都不是(3)(4)A 73B -14C 0.3D 3.333E 1.57F 4.03G 0.248H 4.29I 0.31J 1K 1(5)修改语法错误(已修改完)import java.util.Scanner;public class errors{public static void main (String[] args){String Name; // Name of the userint number;int numSq;Scanner scan = new Scanner(System.in);System.out.print ("Enter your name, please: ");Name = scan.nextString();System.out.print ("What is your favorite number?“);number = scan.nextInt();numSq = number * number;System.out.println (Name +", the square of your number is "+numSquared);}}3.5 Area and Circumference of a Circle(1)(2)(3)3.6 Painting a Room(Choose to do) (1)(2)3.7 Ideal Weight(1)(2)3.8 Lab Grades(Choose to do)3.9 Base Conversion(Choose to do)3.10 Introduction to HTML(Choose to do) 3.11 Drawing Shapes(2)x,y修改为0(3)高和宽改为200 300(4)400 40 50 200(4)矩形打印(5)椭圆3.12 The Java Coordinate System(Choose to do)3.13 Drawing a Face(Choose to do)3.14 Creating a Pie Chart(Choose to do)3.15 Colors in Java(Choose to do)4.Experimental results and data processing5.Analysis and discussionScore: 6.Teacher ReviewsExperimental Report List1 Names and Places1) Source code list3.1package test_java_01;//*************************************************************** //Names.java////Prints a list of student names with their hometowns//and intended major//*************************************************************** public class Test_java_01_1{// ------------------------// main prints the list// ------------------------public static void main (String[] args){System.out.println ();System.out.println ("\tName\t\tHometown");System.out.println ("\t====\t\t========");System.out.println ("\tprince\t\tblack_dragon_river_province"); System.out.println ("\tcircle\t\tWashington");System.out.println ();}}package test_java_01;//*************************************************************** //Names.java////Prints a list of student names with their hometowns//and intended major//*************************************************************** public class Test_java_01_1{// ------------------------// main prints the list// ------------------------public static void main (String[] args){System.out.println ();System.out.println ("\tName\t\tHometown\t\tmajor\t");System.out.println ("\t====\t\t========\t\t==========="); System.out.println ("\tprince\t\tblack_drass\t\tcomputer");System.out.println ("\tcircle\t\tWashington\t\tghhhh");System.out.println ();}}3.2public class Test_java_01_1{// ------------------------// main prints the list// ------------------------public static void main (String[] args){System.out.println("\t//////////////////////\t"+"\t\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\ \t");System.out.println ("\t======\t\tStudent Points\t\t=====\t ");System.out.println ("\tName\t\tLab\t\tBonus\t\tTotal ");System.out.println ("\t----\t\t---\t\t-----\t\t----- ");System.out.println ("\tJoe\t\t43\t\t7\t\t50\t ");System.out.println ("\tWiam\t\t50\t\t8\t\t58 ");System.out.println ("\tMaue\t\t3\t\t1\t\t4 ");System.out.println ("\tMarSue\t\t9\t\t10\t\t9 ");System.out.println ("\tMSue\t\t39\t\t10\t\t49 ");}}3.3(3)package test_java_01;import java.util.Scanner;public class PlusTest {public static void main (String[] args){int vail1,vail2,vail3;double average;Scanner scan=new Scanner(System.in);vail1=scan.nextInt();vail2=scan.nextInt();vail3=scan.nextInt();average=((double)vail1+(double)vail2+(double)vail3)/3;System.out.println("Please enter three integers and " +"I will compute their average :"+average);}}3.43.5(1)package test_java_01;public class sdd_3_5 {public static void main(String[] args){final double PI = 3.14159;int radius = 10;double area = PI * radius * radius;double line = PI * 2 * radius;System.out.println("The area of a circle with radius " + radius + " is " + area);radius = 20;area = PI * radius * radius;area = PI * radius * radius;System.out.println("The area of a circle with radius " + radius + " is " + area);}}(2)package test_java_01;public class sdd_3_5 {public static void main(String[] args){final double PI = 3.14159;int radius = 10;double area = PI * radius * radius,area2=4*area,area3;double line = PI * 2 * radius,line2=2*line,line3;double a,a1;a=line2-line;a1=area2/area;System.out.println("The area of a circle with radius " + radius + " is " + area);System.out.println("The line of a circle with radius " + radius + " is " + line);System.out.println(a+" "+a1);radius = 20;line3 = PI * 2 * radius;area3 = PI * radius * radius;System.out.println("The line of a circle with radius " + radius + " is " + line3);System.out.println("The area of a circle with radius " + radius + " is " + area3);}}(3)package test_java_01;import java.util.Scanner;public class sdd_3_5 {public static void main(String[] args){final double PI = 3.14159;Scanner scan=new Scanner(System.in);System.out.println("put in two number:");int a=scan.nextInt(),b=scan.nextInt();int c=a*b;System.out.println("number :"+c);}}3.6(1)package test_java_01;import java.util.Scanner;public class Area_jisuan_2 {public static void main(String[] args) {final int COVERAGE = 350;Scanner scan = new Scanner(System.in);System.out.print("put in length width and height:");int length = scan.nextInt(),width = scan.nextInt(),height =scan.nextInt();double totalSqFt,paintNeeded;paintNeeded = height*width*2 + width*length*2 + length*height*2;totalSqFt = paintNeeded/COVERAGE;System.out.print("the paintNeeded:"+ paintNeeded+" the totalSqFt:" +totalSqFt);}}(2)package test_java_01;import java.util.Scanner;public class Area_jisuan_2 {public static void main(String[] args) {final int COVERAGE = 350,doorsarea = 20,windowarea = 15 ;Scanner scan = new Scanner(System.in);System.out.println("put in length width and height:");int length = scan.nextInt(),width = scan.nextInt(),height =scan.nextInt();int doors,window;System.out.println("windows number and doors number:");window=scan.nextInt();doors=scan.nextInt();char i;double totalSqFt,paintNeeded;paintNeeded = height*width*2 + width*length*2 + length*height*2 - doors*doorsarea - windowarea*window;totalSqFt = paintNeeded/COVERAGE;System.out.print("the paintNeeded:"+ paintNeeded+" the totalSqFt:" +totalSqFt);}}3.7package test_java_01;import java.util.Scanner;public class weight {public static void main(String[] args) {int male1 = 14, female1 = 5;int weightreall,i=0,l;System.out.print("put in male or female:");Scanner scan = new Scanner(System.in);String c=scan.next();String b=new String("male");System.out.print("put in your height:");double height=scan.nextDouble(),ol,k;ol=height%1;ol=(2*ol*100+1)/20;if(b.equals(c)){weightreall = male1*(int)ol + 100;}else{weightreall = female1*(int)ol + 100;}System.out.print(weightreall);}}(2)package test_java_01;import java.util.Scanner;public class weight {public static void main(String[] args) {int male1 = 14, female1 = 5;int weightreall,i=0,l,weightreallf;System.out.print("put in male or female:");Scanner scan = new Scanner(System.in);String c=scan.next();String b=new String("male");System.out.println("put in your height:");double height=scan.nextDouble(),ol,k;ol=height%1;ol=(2*ol*100+1)/20;weightreall = male1*(int)ol + 100;double wei=weightreall,min,max;min=wei*0.85;max=wei*1.15;System.out.println("最佳体重范围男"+min+"-----"+max);weightreallf =female1*(int)ol +100;wei = weightreallf;min=wei*0.85;max=wei*1.15;System.out.println("最佳体重范围男"+min+"-----"+max);if(b.equals(c)){System.out.print("你的体重"+weightreall);}else{System.out.print("你的体重"+weightreallf);}}}3.8(1)四矩形打印//********************************************//Hello.java////Print a Hello, World message.//********************************************package test_java_01;import javax.swing.JApplet;import java.awt.*;public class hello extends JApplet{public void paint (Graphics page){final int MAX_SIZE = 300;final int PAGE_WIDTH = 600;final int PAGE_HEIGHT = 400;int x, y;int width, height;setBackground (Color.yellow);page.setColor (Color.blue);x = 400;y = 400;width = 150;height = 150;// Draw the rectanglepage.fillRect(x, y, width, height);page.setColor (Color.cyan);page.fillRect(x, y, width-50, height-50);page.setColor (Color.green);page.fillRect(x, y-40, width-100, height+50);page.fillRect(x-200, y-200, width-100, height+50);}}(2)来两个椭圆//********************************************//Hello.java////Print a Hello, World message.//******************************************** package test_java_01;import javax.swing.JApplet;import java.awt.*;public class hello extends JApplet{public void paint (Graphics page){final int MAX_SIZE = 300;final int PAGE_WIDTH = 600;final int PAGE_HEIGHT = 400;int x, y;int width, height;setBackground (Color.yellow);page.setColor (Color.blue);x = 400;y = 400;width = 150;height = 150;// Draw the rectanglepage.fillOval(x, y, width, height);page.setColor (Color.cyan);page.fillRect(x, y, width-50, height-50);page.setColor (Color.green);page.fillRect(x, y-40, width-100, height+50);page.fillOval(x-200, y-200, width-10, height+50);}}2) Results data processing3)Answer questions4)Error Analysis and Discussion。
Java程序设计-试验2(学生版)
Java程序设计-试验2(学生版)
华北电力大学
实验报告
实验名称Java程序面向对象设计(一)课程名称Java程序设计
专业班级:学生姓名:
学号:成绩:指导教师:张学斌实验日期:
自己的speed的值增加80
【代码10】//car2调用speedUp方法将自己的speed的值增加80
System.out.println("car1目前的速度:"+car1.getSpeed());
System.out.println("car2目前的速度:"+car2.getSpeed());
car1.speedDown(10);
car2.speedDown(20);
System.out.println("car1目前的速度:"+car1.getSpeed());
System.out.println("car2目前的速度:"+car2.getSpeed());
}
}
1.3 实验指导
●创建一个对象时,成员变量被分配空间,这些内
存空间称作该对象的实体或变量,而对象中存放着引用,以确保这些变量被该对象操作使用。
●空对象不能使用,即不能让一个空对象去调用方
法产生行为。
假如程序中使用了空对象,在运行时会产生异常:NullPointerException,对象时。
《Java语言程序设计》实验报告二
《Java技术》实验报告—实验(二)实验室:C207 2018 年10 月13日6.以'R'开头的事务表示这是一个到货单记录,在'R'后面是Item number和它的数量Quanlity。
处理一条到货单意味着增加库存中相应货物的数量(增加的数量=到货单中的数量)。
注意:如果在Transactions.txt文件中,到货单出现在发货单之后,到货单中的货物数量可以用来填补发货单中的数量(可以理解成在Transactions.txt中,优先处理到货单)。
7.以'A'开头的事务表示向库存中增加一种新的货物(即这种货物以前库存中没有),在'A'后面是Item number,供应商supplier以及货物的描述description。
处理一个新增货物记录意味着向库存中增加一个数量Quantity为0的新的Item。
你可以假设在一个Transactions.txt 中,新增货物记录总是出现在第一个到货单之前。
8.以'D'开头的事务表示从库存中删除一种货物,在'D'后面是Item号。
删除操作总是在所有的事物处理之后才被处理,以保证对于可能出现的同一种货物的发货单的操作能在删除之前被正确处理。
如果要删除的某种货物的库存量Quantity不为0的话,系统应该向Errors.txt记录出错信息。
9.文件Shipping.txt中的每一行代表给某一客户的发货信息。
Shipping.txt中的每一行分别是客户编号、Item号、货物数量,它们之间用tab键分隔。
如果发货单中有两条客户编号和Item 编号一样的记录,在Shipping.txt中应该将这两条发货信息合并(即将它们的数量相加)。
10.Errors.txt文件包含未发送的发货记录和库存量大于0的删除记录。
Errors.txt每一行包含Custom编号、Item编号以及发货单上的数量Quantity。
JAVA实验报告二
import java.io.*;
public class l {
public static void main(String[] args) throws IOException {
/创建一个文件
String filename= "test.txt";
String directory="C:/Documents and Settings/Administrator/Workspaces/MyEclipse 8.6/L";
a)完成以下题目:
题目1:重置(override)如下Student类的equals()方法。如果对象的类型相同且属性值都相等,返回boolean类型true值。如果有一不等返回boolean类型false值.请写出equals()方法的完整实现。
class Student
{int age;}
程序代码:
package L;
public class student {
int age;
void age(int age){
this.age=age;
}
public boolean equals(Object obj){
if(obj==null)return false;
if(this==obj)return true;
s[0]=new student();
s[1]=new student();
s[2]=new student();
s[0].age(12);
s[1].age(20);
s[2].age(12);
System.out.println(s[0]==s[2]);
System.out.println(s[0].equals(s[1]));
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
练习类的调用,使用StackOfIntegers类
【实验过程及结果记录】
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class zhiyinshu{
public static void main (String[] args) {
练习产生随机数的方法(Random).
2.(使用StackOfIntegers类,显示素数因子)编写一个程序,提示用户输入一个正整数,然后以降序显示它的所有最小因子。例如:如果整数为120,那么显示的最小因子为5、3、2、2、2。使用
StackOfIntegers类存储因子(如:2、2、2、3、5),获取之后按倒序显示这些因子。
System.out.println("2002 Tax Table");
System.out.println("taxable\tSingle\tMarried\tMarried\tHead of");
if(size>=zhan.length){
int[] temp = new int[zhan.length * 2];
System.arraycopy(zhan,0,temp,0,zhan.length);
zhan=temp;
}
zhan[size++]=up;
}
public int esc(){
return zhan[--size];
}
public double getRadius() {
return radius;
}
public void setRadius(double newRadius) {
radius = newRadius;
}
public String getColor() {
return color;
}
public void setColor(String newColor) {
int yuanshu = Integer.parseInt(str);
int i=2;
int b=0;
int c=0;
do{
i=2;
b=0;
do{
if(yuanshu%i==0)
{
yuanshu=yuanshu/i;
h1.jilu(i);
b=1;
}
i++;
}while(b==0&&i<yuanshu);
};
double[] ratesFor2002 = {0.1, 0.15, 0.27, 0.30, 0.35, 0.386};
Tax taxFor2001 = new Tax(0, bracketsFor2001, ratesFor2001, 50000);
System.out.println("2001 Tax Table");
System.out.println("taxable\tSingle\tMarried\tMarried\tHead of");
System.out.println("Income\tSingle\tJoint\tSeparate\ta House");
for (int taxableIncome = 50000; taxableIncome <= 60000; taxableIncome += 1000) {
fan1.setRadius(10);
fan1.setColor("yellow");
fan1.setOn(true);
System.out.println(fan1.toString());
Fan1 fan2 = new Fan1();
fan2.setSpeed(Fan1.MEDIUM);
fan2.setRadius(5);
{12000 , 46700, 112850, 171950, 307050}, // married filing jointly
{6000, 23350, 56425, 85975, 153525}, // married filing separately
{10000, 37450, 96700, 156600, 307050} // head of household
}
}
【实验总结】
熟悉类的定义和使用,练习画UML图
4、(Tax类的应用和实现)根据编程练习10.8(P297)的描述,
使用和实现Tax类,计算缴纳的税款额。第一步,画出Tax类的UML 图。第二步,编写一个测试程序,使用Tax类,对所给4种纳税人打
印2009年的税款表(2009税率表参见表2),可征税收入范围在
StackOfIntegers h1 =new StackOfIntegers();
StackOfIntegers h2 =new StackOfIntegers();
String str=JOptionPane.showInputDialog(null,"请输入一个正整数","分解质因数",JOptionPane.QUESTION_MESSAGE);
Fan,可以编写在Fan.java文件中。第四步,编译运行该程序。
【实验预习】
【实验过程及结果记录】
public class JavaText {
public static void main(String[] args) {
Fan1 fan1 = new Fan1();
fan1.setSpeed(Fan1.FAST);
fan2.setColor("blue");
fan2.setOn(false);
System.out.println(fan2.toString());
}
}
class Fan1 {
public static int SLOW = 1;
public static int MEDIUM = 2;
public static int FAST = 3;
50,000~60,000美元之间,间隔为1000美元。第三步,实现Tax类。
第四步,编译、运行该程序。
表2 2009年美国国家联邦个人收入所得税税率表
【实验预习】
【实验过程及结果记录】
public class JavaText {
public static void main(String[] args) {
taxFor2001.setTaxableIncome(taxableIncome);
taxFor2001.setFilingStatus(0);
int taxForStatus0 = (int)taxFor2001.findTax();
taxFor2001.setFilingStatus(1);
int taxForStatus1 = (int)taxFor2001.findTax();
private int speed = SLOW;
private boolean on = false;
private double radius = 5;
private String color = "white";
public Fan1() {
}
public int getSpeed() {
return speed;
实验2 类和对象
【实验宗旨】
每天做一点编程练习。
【实验目的】
1、学习和掌握Java创建对象的方法。
2、学习和掌握通过创建对象、使用类的方法。
3、学习和掌握Java类定义的方法。
4、了解类设计的一般原则。
【实验内容】
1、(Java API类的应用)使用java.util.Random类,创建种子是1000的Random对象,然后使用nextInt(100)方法显示0到100
taxFor2001.setFilingStatus(2);
int taxForStatus2 = (int)taxFor2001.findTax();
taxFor2001.setFilingStatus(3);
int taxForStatus3 = (int)taxFor2001.findTax();
程序(Fan类应用),⑴创建两个Fan对象,⑵用第一个Fan对象表示“最大速度运转、半径为10、颜色为黄色、处于打开状态”的风扇;⑶用第二个Fan对象表示“中等速度运转、半径为5、颜色为蓝
色、处于关闭状态”的风扇;⑷通过调用toString()方法显示这些风扇的属性信息。程序可以TestFan.java中。第三步,实现风扇类
public static void main(String[] args) {
Random random = new Random(1000);
for (int i = 0; i < 50; i++)
System.out.print(random.nextInt(100) + " ");
}
}
【实验总结】
{36250, 93650, 151650, 297350} // head of household
};
double[] ratesFor2001 = {0.15, 0.275, 0.305, 0.355, 0.391};
int[][] bracketsFor2002 = {
{6000, 27950, 67700, 141250, 307050}, // Single filer