/**Toss coin test that allow the you guess the upside as many times as you wish. When exit, the system would give you a list of information about your guesses.*/import java.io.*;public class TossCoin { public static void main(String[]args) { String input=null; long seed=System.currentTimeMill
接口类只可以继承接口类的,例如Student是一个类,而“public interface CollegeStudentInterface extends Student”就是错误的。如果要写一个CollegeStudent的接口只能先把Student接口化,然后将CollegeStudentInterface继承它。
/**An interface for a class of students.*/public interface StudentInterface{ /**
·实验说明:模拟一个银币,此银币有两个面。方法为‘抛银币’,银币顶面、底面随机向上。抛两银币各50次,并显示两银币顶面向上次数和哪个银币顶面次数较多。
·代码:
import java.util.*;public class GenericCoin { private int front; private int back; GenericCoin(){ front=1; back=0; }//end constructor
/** a person with name and age*/public class Person { private Name name; private int age; public Person(){ name=null; age=0; }//end default constructor public Person(Name personName,int personAg
·实验说明:创建了Student类,数据域中声明了类型为一个先前创建的Name类的私有变量。main方法中使用两种创建Student对象的方法。
·代码public class Student { private Name fullName; private String id;//学号 public Student(){ fullName=new Name(); id="";
·实验说明:创建Name类,构造两个Name对象,并把第一个对象的第一姓名赋予第二个对象。打印相应结果。
·代码public class Name { private String first; //名 private String last; //姓
public Name(){ }//end default constructor public Name(String firstName, String lastN
· 实验
·作用:计数器,实现每次加一或减一运算
·代码:public class Counter {//post:increase or decrease cnt by 1private int cnt;public Counter(){ cnt=0;}//end default constructorpublic void inc(){ ++cnt;}//end inc public void dec(){