一道Java初学者练习题,拜托了各位编程大神!

Python021

一道Java初学者练习题,拜托了各位编程大神!,第1张

public abstract class Animal

{

protected string _Name

public abstract string Name

{

get

}

public abstract void Show()

public void MakeVoice()

{

Console.WriteLine("{0}在叫",_Name)

}

}

public interface IAction

{

void Move()

}

public class Duck:Animal,IAction

{

public override string Name

{

get

{

return _Name

}

}

public Duck(string name)//构造函数

{

_Name = name

Console.WriteLine(_Name)

}

public override void Show() //重写

{

Console.WriteLine("{0}闪亮登场",Name)

}

public void Move() //接口的实现

{

Console.WriteLine("{0}快爬.", Name)

}

}

public class Dog : Animal, IAction

{

public override string Name

{

get

{

return _Name

}

}

public Dog(string name)

{

_Name = name

Console.WriteLine(_Name)

}

public override void Show()

{

Console.WriteLine("{0}闪亮登场", Name)

}

public void Move()

{

Console.WriteLine("{0}快爬", Name)

}

}

#endregion

class Program

{

static void Main(string[] args)

{

Duck duck = new Duck("鸭子")

duck.MakeVoice()

duck.Show()

duck.Move()

Dog dog = new Dog("小狗")

dog.MakeVoice()

dog.Show()

dog.Move()

IAction iaction = (IAction)duck

iaction.Move()

IAction iaction2 = (IAction)dog

iaction2.Move()

Console.ReadKey()

}

}

第一题:

(输入每个值的时候都按回车,这个值中夹杂"stop"列入停止范围)

具体程序如下:

import java.util.Scanner

public class WhenStopOthers1 {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input = new Scanner(System.in)

System.out.println("请输入:")

String str = input.next()

while (!str.equals("stop")) {

System.out.println("请继续输入,直到输入stop为止:")

str = input.next()

}

System.out.println("输入结束.")

}

}

/** *********************我是分隔线******************* **/

第二题:

(对已经赋值的一维数组进行逆序输出)

程序如下:

public class BackOut {

public static void main(String[] args) {

String[] s = {"!","了","我","出","输","序","逆","就","样","这"}

for(int i = s.length - 1i >= 0 i--){

System.out.print(s[i])

if(i == 0){

System.out.println()

}

}

}

}

public class Circle {

private float radius

public Circle() {

this.radius = 0

}

public Circle(float radius) {

this.radius = radius

}

public double getArea() {

return 3.14 * this.radius * this.radius

}

public double getPerimeter() {

return 2 * 3.14 * this.radius

}

public void show() {

System.out.println("圆的半径:" + this.radius)

System.out.println("圆的面积:" + this.getArea())

System.out.println("圆的周长:" + this.getPerimeter())

}

}

运行结果: