程序设计java

程序设计java,第1张

给你写完了 很容易的:

/ Catalanjava

Contains 2 methods factorial and cata

catalan calculates the n-th catalan number

factorial calculates the factorial recursively

Receive: n

Return: n-th catalan number

/

public class Catalan {

private long factorial(int n) {

if (n == 0) {

return 1;

} else {

return n factorial(n - 1);

}

}

public long calCatalan(int n) {

return factorial(2 n) / (factorial(n + 1) factorial(n));

}

}

import javautilScanner;

import javautilregexPattern;

/

Calculates the value of a specific Catalan number

Input: integer value

Output: n-th Catalan number

/

public class CatalanDemo {

public static void main(String[] args) {

Scanner in = new Scanner(Systemin);

Catalan catalan = new Catalan();

while (true) {

Systemoutprintln("Which catalan number do you want to calculate (-1 to stop): ");

String s = innextLine();

if (Patternmatches("[0-9]", s)) {

int n = IntegerparseInt(s);

Systemoutprint("n=" + n);

Systemoutprintln(": catalan number is " + catalancalCatalan(n));

} else {

if (Patternmatches("-[0-9]", s)) {

int n = IntegerparseInt(s);

if (n == -1) {

Systemoutprintln("(program exits)");

break;

} else {

Systemoutprintln("Invalid input:\nValid input range is an input >= 0");

}

}

}

}

}

}

说明:

虽然在语法上可以将Point或Shape定义为接口,但在逻辑上是错误的。

接口是对行为的规范,它不具有对象的特征。抽象类本质是对象的抽象,它具有对象的典型特征,但也有对子类行为的规范。无论是坐标点Point还是形状Shape,它们都具有类的本质特征,不应该将它们定义为接口(虽然语法上并没有错,但在理解上令人费解)。

矩形、圆、椭圆都继承自抽象的形状类,并都包含坐标原点。这样就很好地解决了5个类的关联。另一方面,实际上在几乎所有面向程序设计语言中,并没有圆类,只有椭圆类,因为圆就是一种特殊的椭圆(当a=b时,椭圆就是一种特殊的圆)。

class Point{

protected int x;

protected int y;

public Point(){

}

public Point(int x, int y){

thisx = x;

thisy = y;

}

//返回当前点的横坐标

public int getX(){

return x;

}

//返回当前点的纵坐标

public int getY(){

return y;

}

}

//抽象的形状类

abstract class Shape{

abstract double area(); //抽象的求面积方法

abstract double girth(); //抽象的求周长方法

abstract String getName(); //抽象的返回形状名称的方法

}

//矩形类

class Rectangle extends Shape{

protected Point center; //矩形的中心点坐标

protected double width; //矩形的宽

protected double height; //矩形的高

public Rectangle(){

}

public Rectangle(Point center, double width, double height){

thiscenter = center;

thiswidth = width;

thisheight = height;

}

//返回矩形的面积

public double area() {

return width height;

}

//返回矩形的周长

public double girth() {

return 2 (width + height);

}

//返回矩形的中心点坐标

public Point getCenter(){

return center;

}

//返回当前形状的名称

public String getName() {

return "Rectangle";

}

}

//圆类

class Circle extends Shape{

protected Point center; //圆的中心点坐标

protected double radius; //圆的半径

public Circle(){

}

public Circle(Point center, double radius){

thiscenter = center;

thisradius = radius;

}

//返回圆的面积

public double area() {

return MathPI radius radius;

}

//返回圆的周长

public double girth() {

return 2 MathPI radius;

}

//返回圆的中心点坐标

public Point getCenter(){

return center;

}

//返回当前形状的名称

public String getName() {

return "Circle";

}

}

//椭圆类

class Ellipse extends Shape{

protected Point center; //椭圆的中心点坐标

protected double a; //椭圆的长半轴的长

protected double b; //椭圆的短半轴的长

public Ellipse(){

}

public Ellipse(Point center, double a, double b){

thiscenter = center;

thisa = a;

thisb = b;

}

//返回椭圆的面积

public double area() {

return MathPI a b;

}

//返回椭圆的周长

public double girth() {

return 2 MathPI Mathsqrt((a a + b b) / 2);

}

//返回椭圆的中心点坐标

public Point getCenter(){

return thiscenter;

}

//返回当前形状的名称

public String getName() {

return "Ellipse";

}

}

//测试类

public class Test {

public static void main(String[] args) {

Point center = new Point(100, 100); //定义形状的中心点坐标

Shape s; //定义抽象基类对象

s = new Rectangle(center, 3, 4); //用子类实例化抽象基类对象

Systemoutprintln(sgetName() + " : "); //实现了动态多态

Systemoutprintln("Center : " + ((Rectangle)s)getCenter()getX() + ", " + ((Rectangle)s)getCenter()getY());

Systemoutprintln("Area : " + sarea()); //实现了动态多态

Systemoutprintln("Girth : " + sgirth()); //实现了动态多态

Systemoutprintln();

s = new Circle(center, 2); //用子类实例化抽象基类对象

Systemoutprintln(sgetName() + " : ");

Systemoutprintln("Center : " + ((Circle)s)getCenter()getX() + ", " + ((Circle)s)getCenter()getY());

Systemoutprintln("Area : " + sarea());

Systemoutprintln("Girth : " + sgirth());

Systemoutprintln();

s = new Ellipse(center, 5, 3); //用子类实例化抽象基类对象

Systemoutprintln(sgetName() + " : ");

Systemoutprintln("Center : " + ((Ellipse)s)getCenter()getX() + ", " + ((Ellipse)s)getCenter()getY());

Systemoutprintln("Area : " + sarea());

Systemoutprintln("Girth : " + sgirth());

}

}

1:ObjectInputStream ,ObjectOutputStream

2:try{}

3:10

4:多继承

5:new

6:int , char

7:KeyListener

8:Applet

以上答案尽请参考

以上就是关于程序设计java全部的内容,包括:程序设计java、JAVA程序设计相关、Java语言程序设计,填空题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9393986.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存