类体是类的主要部分,包括对 (1) 和 (2) 的定义
查看答案
在定义方法时,如果方法没有返回值,则返回值类型要声明为____。
以下程序的运行结果是____。(可能有多行)public class Program { public static void main(String[] args) { Power p = new Power(); p.DoPower(10, 10); System.out.println("x=" + p.x + ",y=" + p.y); }}class Power{ public int x, y; public void DoPower(int passX, int passY) { x = passX * passX; y = passY * passY; }}
以下程序的运行结果是____。(可能有多行) public class Program{ static int x=1; public static void main(String[] args) { int x = 4; System.out.println("x=" + x); DoubleNum(); System.out.println("x=" + Program.x); } static void DoubleNum() { x*=2; System.out.println("x=" + x); }}
编写长方形类,计算面积及周长。创建两个对象,输出它们的面积及周长。 public class Rectangle{ private int width; //字段,宽,类中的变量,分配内存 private int height; //字段,高 public Rectangle() {width = 0; height = 0;} public Rectangle(int width, int height) throws Exception { setWidth((1) ); setHeight((2) ); } public(3) getWidth() { return width; } public void setWidth(int width) throws Exception { if (width <0) { throw new Exception("宽不能小于0"); } this.width =(4); } public(5)getHeight() { return height; } public void setHeight(int height) throws Exception { if (height <0) { throw new Exception("高不能小于0"); } this.height =(6) ; } public(7)Area() { //方法,计算面积 return width * height; } public (8)Perimeter() { return 2 * (width + height); }} class Program //测试类{ public static void main(String[] args) { try { //可能出现异常的代码 Rectangle rect1 = new Rectangle(); rect1.setWidth(5); rect1.setHeight(4); System.out.printf("宽为%d\n",rect1.getWidth()); System.out.printf("面积为%d,周长为%d\n", rect1.Area(), rect1.Perimeter()); } catch (Exception(9) ) { System.out.println(e.getMessage()); } try { Rectangle rect2 = new Rectangle(-7, 6); System.out.printf("面积为%d,周长为%d\n", rect2.Area(), rect2.Perimeter()); } catch (Exception (10) ) { System.out.println(e.getMessage()); } }}