1、編寫一個Java程序在屏幕上輸出“Hello!”。
public class test
{
public static void main(String[] args)
{
System.out.println("Hello!");
}
}
2、編寫一個Java程序在屏幕上輸出“Hello, world!”。
public class test
{
public static void main(String[] args)
{
System.out.println("Hello,world");
}
}
3、編寫一個Java程序在屏幕上輸出“This is java!”。
public class test
{
public static void main(String[] args)
{
System.out.println("This is java!");
}
}
4、編寫一個Java程序在屏幕上輸出“This is my program!”
public class test
{
public static void main(String[] args)
{
System.out.println("This is my program!");
}
}
5、編寫一個Java程序在屏幕上輸出“How are you?”
public class test
{
public static void main(String[] args)
{
System.out.println("How are you?");
}
}
6、編寫一個圓類Circle,該類擁有:
①一個成員變量
Radius(私有,浮點型);? // 存放圓的半徑;
②兩個構(gòu)造方法
Circle( )???????????????? // 將半徑設(shè)為0
Circle(double? r )???????? //創(chuàng)建Circle對象時將半徑初始化為r
class Circle{
private double Radius;
Circle(){
Radius=0.0;}
public Circle(double r){
this. Radius= r;}
}
7、定義一個表示學(xué)生信息的類Student,要求如下:
。1)類Student的成員變量:
sNO 表示學(xué)號;sName表示姓名;sSex表示性別;sAge表示年齡;sJava:表示Java課程成績。
(2)類Student的方法成員:
getNo():獲得學(xué)號;
getName():獲得姓名;
class Student{
private String sNO;
private String sName;
private char sSex;
private int sAge;
private int sJava;
public Student(String sNo, String sName){
this.sNo = sNo;
this.sName = sName;? }
public String getsNo() {
return sNo; }
public String getsName() {
return sName; }
}
8、定義一個表示水果的類Fruit,要求如下:
(1)類的成員變量: fName表示水果名稱,fColor表示水果顏色。
(2)類的成員方法:getName( ):獲得水果名稱。
class Fruit{
private String fName;
private String fColor;
public Fruit(String fName, String Fcolor ){
this.sName = sName;
this.fColor = fColor;? }
public String getfName() {
return fName; }
}
9、先定義一個類A(該類有成員變量x和y),再定義類A的子類B(要求B新增成員變量z)。
class A{
private int x;
private int y;
public A(int x,int y ){
this.x = x;
this.y = y;? }
}
class B extendsA{
private int z;
public B(int z){
this.z=z;}
}
10、先定義一個類A(該類有成員變量x,成員方法f1( )對x賦初值),再定義類A的子類B(要求B新增成員變量y,新增成員方法f2(? )對y賦初值)。
class A{
private int x;
public A(int x, ){
this.x = x;? }
public int f1x() {
return x; }
}
class B extendsA{
private int y;
public B(int z){
this.y = y;? }
public int f2y() {
return y; }
}