java实验报告汇总
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第一章实验报告1、安装与调试JDK
第二章实验报告1、运行Welcome.java
2、运行ImageView.java
第三章实验报告
1、一个简单的java应用程序:
2、关于java基本程序设计结构的实验:
3、常量:
4、字符串操作:
5、构建字符串:
6、输入输出:
7、条件语句
用for、while、和do while编写计算n!的小程序。
输入欲求其阶乘的正整数n,返回n!,
如下:
8、字符串
以上是为foreach循环。
第四章实验报告
1、类、对象、访问器方法、修改器方法、静态实例域、静态方法等概念的一个小程序。
以宿舍四位室友的学号和假定收入为依据。
代码如下:
package p;
public class R
{
/**
*This program demonstrates static methods.
*@version 1.012013-05-08
*@author SCC
*/
public static void main(String[] args)
{
//fill the roommates with 4 objects
RoomMate[] roommates = new RoomMate[4];
roommates[0] = new RoomMate("SLK",10000);
roommates[1] = new RoomMate("SCC",9000);
roommates[2] = new RoomMate("WHL",8000);
roommates[3] = new RoomMate("MJP",7000);
//print out information about all roommates
for (RoomMate any:roommates)
{
any.setId();
System.out.println("name="+any.getName()+",id="+any.getId()+",sal ary="+any.getSalary());
}
int n = RoomMate.getNextId();//calls static method
System.out.println("Next avaliable id = "+n);
}
}
class RoomMate
{
public RoomMate(String n,double s)
{
name = n;
salary = s;
id = 0;
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public int getId()
{
return id;
}
public void setId()
{
id = nextId;//set id to next available id
nextId++;
}
public static int getNextId()
{
return nextId;
}
public static void main(String[] args)
{
RoomMate any = new RoomMate("SCC",5000);
System.out.println(any.getName()+" "+any.getSalary());
}
private String name;
private double salary;
private int id;
private static int nextId = 2011051526;
}
运行结果如下:
2、论证一个方法不能改变值参数的值,但可以通过对对象参数的拷贝修改所引用的对象的状态,以及java的参数引用是值调用而不是引用调用。
代码如下:
package p;
public class R
{
/**
*This program demonstrates parameter passing in java.
*@version 1.012013-05-08
*@author SCC
*/
public static void main(String[] args)
{
/*
* Test 1: Methods can not modify numeric parameters.
*/
System.out.println("Test tripleValue:");
double a = 10;
System.out.println("Before: a="+a);
tripleValue(a);
System.out.println("After: a="+a);
/*
* Test 2:Methods can change the state of object parameters.
*/
System.out.println("\nTest tripleSalary:");
RoomMate mjp = new RoomMate("MJP",7000);
System.out.println("Before: salary="+mjp.getSalary());
tripleSalary(mjp);
System.out.println("After: salary= "+mjp.getSalary());
/*
* Test 3: Methods can not attach new object to object parameters.
*/
System.out.println("\nTest swap");
RoomMate slk = new RoomMate("SLK",9000);
RoomMate whl = new RoomMate("WHL",8000);
System.out.println("Before: slk="+slk.getName());
System.out.println("Before: whl="+whl.getName());
swap(slk,whl);
System.out.println("After: slk="+slk.getName());
System.out.println("After: whl="+whl.getName());
}
public static void tripleValue(double x)
{
x = 3*x;
System.out.println("End of method: x="+x);
}
public static void tripleSalary(RoomMate any)
{
any.raiseSalary(200);
System.out.println("End of the method:
salary="+any.getSalary());
}
public static void swap(RoomMate a,RoomMate b)
{
RoomMate temp = a;
a=b;
b=temp;
System.out.println("End of method: a="+a.getName());
System.out.println("End of method: b="+b.getName());
}
}
class RoomMate
{
public RoomMate(String n,double s)
{
name = n;
salary = s;
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public void raiseSalary(double byPercent)
{
double raise = salary*byPercent/100;
salary += raise;
}
private String name;
private double salary;
}
运行结果:
3、重载构造器
用this(……)调用另一个构造器
默认构造器
对象初始化块
静态初始化块
实例域初始化
代码如下:
package p;
import java.util.*;
public class R
{
/**
*This program demonstrates object construction.
*@version 1.012013-05-011
*@author SCC
*/
public static void main(String[] args)
{
//fill the roommates with three RoomMate objects.
RoomMate[] any = new RoomMate[3];
any[0] = new RoomMate("SLK",10000);
any[1] = new RoomMate(9000);
any[2] = new RoomMate();
//print out information about all roommates
for (RoomMate one:any)
{
System.out.println("name="+one.getName()+",id="+one.getId()+",sal ary="+one.getSalary());
}
}
}
class RoomMate
{
//three overloaded constructors.
public RoomMate(String n,double s)
{
name = n;
salary = s;
}
public RoomMate(double s)
{
//calls the RoomMate(String,double) constructor.
this("RoomMate #"+nextId,s);
}
public RoomMate()
{
//name initialized to ""
//salary is initialized to 0
//id initialized in initialization block.
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public int getId()
{
return id;
}
private static int nextId;
private int id;
private String name="";//instance field initialization private double salary;
//static initialization block
static
{
Random get = new Random();
nextId = get.nextInt(1000);
}
//object initialization block
{
id = nextId;
nextId++;
}
}
运行结果如下:
第五章实验报告
1、类继承
方法的重载
super关键字继承对象的构造
对超类RoomMate的继承生成子类RoomCaptain,即4个室友中有一个室长,预测未来工资情况,其中室长有奖学金scholarship。
代码如下:
package test;
import java.util.*;
/*
* This program demonstrates inheritance
* @version 1.01 2013-05-15
* @author SCC
*/
public class RoomCaptainTest
{
public static void main(String[] args)
{
//construct the RoomCaptain object
RoomCaptain slk = new RoomCaptain("SLK",10000);
slk.setScholarship(500);
RoomMate[] roommate = new RoomMate[4];
roommate[0] = slk;
roommate[1] = new RoomMate("SCC",9000);
roommate[2] = new RoomMate("MJP",8000);
roommate[3] = new RoomMate("Whl",7000);
for (RoomMate guys: roommate)
{
System.out.println("name:"+guys.getName()+",salary:"+guys.getSala ry());
}
}
}
class RoomMate
{
public RoomMate(String n,double s)
{
name = n;
salary = s;
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
private String name;
private double salary;
}
class RoomCaptain extends RoomMate
{
public RoomCaptain(String n,double s)
{
super(n,s);
scholarship = 0;
}
public double getSalary()
{
double baseSalary = super.getSalary();
return baseSalary + scholarship;
}
public void setScholarship(double b)
{
scholarship = b;
}
private double scholarship;
}
运行结果如下:
2、抽象类
抽象方法的继承
代码如下:
package eighth_p158;
import java.util.*;
/**
*This program demonstrate abstract classes
*@version 1.012013-05-15
*@author SCC
*/
public class PersonTest
{
public static void main(String[] args)
{
EachRoomMate[] each = new EachRoomMate[4];
each[0] = new RoomMate("SLK","roomcaptain");
each[1] = new RoomMate("SCC","roommtes");
each[2] = new RoomMate("MJP","roommtes");
each[3] = new RoomMate("WHL","roommtes");
for (EachRoomMate one:each)
{
System.out.println("name="+one.getName()+",identity="+one.getDesc ription());
}
}
}
abstract class EachRoomMate
{
public EachRoomMate(String n)
{
name = n;
}
public abstract String getDescription();
public String getName()
{
return name;
}
private String name;
}
class RoomMate extends EachRoomMate
{
public RoomMate(String n,String m)
{
super(n);
description = m;
}
public String getDescription()
{
return description;
}
private String description;
}
运行结果:
第六章实验报告
接口
import java.util.*;
/**
*This program demonstrates the use of the Comparable interface.
*@2013-06-17
*@author SCC
*/
public class EmployeeSortTest
{
public static void main(String[] args)
{
Employee[] staff = new Employee[3];
staff[0] = new Employee("Harry Hacker", 35000,0);
staff[1] = new Employee("Carl Cracker", 75000,0);
staff[2] = new Employee("Tony Tester", 38000,0);
Arrays.sort(staff);
// print out information about all Employee objects
for(Employee e : staff)
{
e.calculateBonus(0.042);
}
for (Employee e : staff)
System.out.println("name=" + e.getName() + ",salary=" +
e.getSalary() + ",bonus=" + e.getBonus());
if (staff[2].compareTo(staff[1]) == 1)
System.out.println(staff[2].getName() + "'s salary is higer than " +staff[1].getName() + "'s. ");
;
}
}
class Employee implements Comparable<Employee>
{
public Employee(String n, double s, double b)
{
name = n;
salary = s;
bonus = b;
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public double getBonus()
{
return bonus;
}
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}
public void calculateBonus(double byPercent)
{
double bonus = salary * 12 * byPercent;
this.bonus += bonus;
}
/**
*Compares employees by salary
*@param other another Employee object
*@return a negative value if this employee has a lower salary than *otherObject,0if the salaries are the same,a positive value otherwise
*/
public int compareTo(Employee other)
{
if (salary < other.salary) return -1;
if (salary > other.salary) return 1;
return 0;
}
// public int compareTo(Employee other) // {
// if(bonus < other.bonus) return -1;
// if(bonus > other.bonus) return 1;
// return 0;
// }
private String name;
private double salary;
private double bonus;
}
运行结果如下:
第七章实验报告
equals、hashcode和toString方法。
import java.util.*;
/**
*This program demonstrates the equals method.
*@version 1.112013-06-17
*@author SCC
*/
public class EqualsTest
{
public static void main(String[] args)
{
Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15); Employee alice2 = alice1;
Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15); Employee bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);
System.out.println("alice1 == alice2: " + (alice1 == alice2));
System.out.println("alice1 == alice3: " + (alice1 == alice3));
System.out.println("alice1.equals(alice3): " +
alice1.equals(alice3));
System.out.println("alice1.equals(bob): " + alice1.equals(bob));
System.out.println("bob.toString(): " + bob);
Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15); Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15); boss.setBonus(5000);
System.out.println("boss.toString(): " + boss);
System.out.println("carl.equals(boss): " + carl.equals(boss));
System.out.println("alice1.hashCode(): " + alice1.hashCode());
System.out.println("alice3.hashCode(): " + alice3.hashCode());
System.out.println("bob.hashCode(): " + bob.hashCode());
System.out.println("carl.hashCode(): " + carl.hashCode());
}
}
class Employee
{
public Employee(String n, double s, int year, int month, int day) {
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
hireDay = calendar.getTime();
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public Date getHireDay()
{
return hireDay;
}
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}
public boolean equals(Object otherObject)
{
// a quick test to see if the objects are identical
if (this == otherObject) return true;
// must return false if the explicit parameter is null
if (otherObject == null) return false;
// if the classes don't match, they can't be equal
if (getClass() != otherObject.getClass()) return false;
// now we know otherObject is a non-null Employee
Employee other = (Employee) otherObject;
// test whether the fields have identical values
return name.equals() && salary == other.salary && hireDay.equals(other.hireDay);
}
public int hashCode()
{
return 7 * name.hashCode() + 11 * new Double(salary).hashCode() + 13 * hireDay.hashCode();
}
public String toString()
{
return getClass().getName() + "[name="+ name+ ",salary="+ salary + ",hireDay=" + hireDay
+ "]";
}
private String name;
private double salary;
private Date hireDay;
}
class Manager extends Employee
{
public Manager(String n, double s, int year, int month, int day)
{
super(n, s, year, month, day);
bonus = 0;
}
public double getSalary()
{
double baseSalary = super.getSalary();
return baseSalary + bonus;
}
public void setBonus(double b)
{
bonus = b;
}
public boolean equals(Object otherObject)
{
if (!super.equals(otherObject)) return false;
Manager other = (Manager) otherObject;
// super.equals checked that this and other belong to the same class return bonus == other.bonus;
}
public int hashCode()
{
return super.hashCode() + 17 * new Double(bonus).hashCode();
}
public String toString()
{
return super.toString() + "[bonus=" + bonus + "]";
}
private double bonus;
}
运行结果如下:
第八章实验报告
对象克隆
import java.util.*;
/**
*This program demonstrates cloning.
*@version 1.102013-06-17
*@author SCC
*/
public class CloneTest
{
public static void main(String[] args)
{
try
{
Employee original = new Employee("John Q. Public", 50000); original.setHireDay(2000, 1, 1);
Employee copy = original.clone();
copy.raiseSalary(10);
copy.setHireDay(2002, 12, 31);
System.out.println("original=" + original);
System.out.println("copy=" + copy);
}
catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
}
}
class Employee implements Cloneable
{
public Employee(String n, double s)
{
name = n;
salary = s;
hireDay = new Date();
}
public Employee clone() throws CloneNotSupportedException
{
//call Object.clone()
Employee cloned = (Employee) super.clone();
// clone mutable fields
cloned.hireDay = (Date) hireDay.clone();
return cloned;
}
/**
*Set the hire day to a given date.
*@param year the year of the hire day
*@param month the month of the hire day
*@param day the day of the hire day
*/
public void setHireDay(int year, int month, int day)
{
Date newHireDay = new GregorianCalendar(year, month - 1,
day).getTime();
// Example of instance field mutation
hireDay.setTime(newHireDay.getTime());
}
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}
public String toString()
{
return"Employee[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]";
}
private String name;
private double salary;
private Date hireDay;
}
运行结果:
第九章实验报告
测试内部类:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
/**
*This program demonstrates the use of inner classes.
*@version 1.102013-06-17
*@author SCC
*/
public class InnerClassTest
{
public static void main(String[] args)
{
TalkingClock clock = new TalkingClock(1000, true);
clock.start();
// keep program running until user selects "Ok"
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
}
/**
*A clock that prints the time in regular intervals.
*/
class TalkingClock
{
/**
*Constructs a talking clock
*@param interval the interval between messages(in milliseconds) *@param beep true if the clock should beep
*/
public TalkingClock(int interval, boolean beep)
{
this.interval = interval;
this.beep = beep;
}
/**
*Starts the clock.
*/
public void start()
{
ActionListener listener = new TimePrinter();
Timer t = new Timer(interval, listener);
t.start();
}
private int interval;
private boolean beep;
public class TimePrinter implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Date now = new Date();
System.out.println("At the tone, the time is " + now);
if (beep) Toolkit.getDefaultToolkit().beep();
}
}
}
运行结果:
第十章实验报告
静态内部类:
/**
*This program demonstrates the use of static inner classes. *@version 1.012013-06-17
*@author SCC
*/
public class StaticInnerClassTest
{
public static void main(String[] args)
{
double[] d = new double[20];
for (int i = 0; i < d.length; i++)
d[i] = 100 * Math.random();
ArrayAlg.Pair p = ArrayAlg.minmax(d);
System.out.println("min = " + p.getFirst());
System.out.println("max = " + p.getSecond());
}
}
class ArrayAlg
{
/**
*A pair of floating-point numbers
*/
public static class Pair
{
/**
*Constructs a pair from two floating-point numbers
*@param f the first number
*@param s the second number
*/
public Pair(double f, double s)
{
first = f;
second = s;
}
/**
*Returns the first number of the pair
*@return the first number
*/
public double getFirst()
{
return first;
}
/**
*Returns the second number of the pair
*@return the second number
*/
public double getSecond()
{
return second;
}
private double first;
private double second;
}
/**
*Computes both the minimum and the maximum of an array
*@param values an array of floating-point numbers
*@return a pair whose first element is the minimum and whose second element
*is the maximum
*/
public static Pair minmax(double[] values)
{
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
for (double v : values)
{
if (min > v) min = v;
if (max < v) max = v;
}
return new Pair(min, max);
}
}
运行结果:。