7.1 | 简述对象与它的定义类之间的关系? 如何声明类?如何声明对象引用变量?如何创建对象?如何用一条语句声明并创建对象? 类是对事物的抽象,是数据模型,类就如同数据类型;对象是具体事物,对象如同变量,是类的实例; 声明类:[修饰符]class 类名[extends 父类名][implements 接口名] { 成员变量声明; 构造方法; 成员方法; } 声明对象引用:类名 对象名; 创建对象 对象名=new 类名(参数列表) 一条语句: 类名 对象名=new 类名(参数列表) | |||
7.2 | 构造方法与成员方法之间的区别是什么? 1.作用不同:构造方法仅用于实例化对象,对成员变量进行初始化;成员方法用于对成员变量进行多种操作; 2.调用方式不同:构造方法通过new运算符调用;成员方法通过对象调用。 | |||
7.3 | 数组是基本数据类型还是对象类型?数组可以包含基本数据类型的元素吗?描述数组元素的默认值? 数组是对象类型;数组中可以包含基本数据类型;对于数组元素是基本类型数值的默认值0,对于数组元素是对象类型的默认值为null。 | |||
7.4 | 指出下列程序的错误。 public class ShowErrors{ public static void main(String[] args){ ShowErrors t=new ShowErrors(5); } } public class ShowErrors{ public static void main(String[] args){ ShowErrors t=new ShowErrors(); t.x(); } } public class ShowErrors{ public static void main(String[] args){ C c=new C(5.0); System.out.println(c.value); } } class C{ int value=2;} | |||
7.5 | 指出下列代码的错误。 1 class Test { 2 public static void main(String[] args) { 3 A a = new A(); 4 a.print(); 5 } 6 } 7 8 class A { 9 String s; 10 11 A(String s) { 12 this.s = s; 13 } 14 15 public void print(){ 16 System.out.print(s); 17 } 18 } | |||
7.6 | 写出输出结果。 public class Foo { private boolean x; public static void main(String[] args) { Foo foo = new Foo(); System.out.println(foo.x); } } false | |||
7.7 | 如何使用Date 创建时间对象以及显示当前时间? 创建日期:java.util.Date date=new java.util.Date(); 显示日期:System.out.println(date); | |||
7.8 | 如何创建 Random 对象并得到一个随机 int 值, 随机double 值, 和随机 boolean 值? 1.可以使用Math.random()方法获得int或double 值; 2.java.util.Random random=new java.util.Random (); Int(); Double(); Boolean(); | |||
7.9 | 哪个包包含 Date, Random, JOptionPane, System,和 Math类? Date : java.util.Date; Random: java.util.Random; JOptionPane: javax.swing. JOptionPane; System:java.lang.System; Math:java.lang.Math; | |||
7.10 | 假设类 Foo 如下定义. f 是 Foo类的对象. (b)中哪个语句是正确的? public class Foo{ int i; static String s; void imethod(){} static void smethod(){} } (b) System.out.println(f.i); System.out.println(f.s); f.imethod(); f.smethod(); System.out.println(Foo.i); (F) System.out.println(Foo.s); Foo.imethod();(F) Foo.smethod(); | |||
7.11 | 在合适的?处添加static 关键字。 public class Test { private int count; public static void main(String[] args) { ... } public ? int getCount() { return count; } public static int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) result *= i; return result; } } | |||
7.12 | 是否能在静态方法中调用实例方法或引用实例变量?是否能在实例方法中调用静态方法或引用静态变量?下面代码的错误是什么? 1 public class Foo { 2 public static void main(String[] args) { 3 method1(); 4 } 5 6 public void method1() { 7 method2(); 8 } 9 10 public static void method2() { 11 System.out.println("What is radius " + c.getRadius()); 12 } 13 14 15 } 不能在静态方法中调用实例方法或引用实例变量; 能在实例方法中调用静态方法或引用静态变量; Foo f=new Foo(); 3 f.method1(); //实例方法需要用对象名调用 | |||
7.13 | 什么是访问器方法?什么是修改器方法?访问器方法和修改器方法的命名习惯是什么? 访问器方法:用来访问成员变量值的方法; 修改器方法:用来修改成员变量值的方法; 访问器方法命名习惯get成员变量名() 修改器方法命名习惯set成员变量名(参数) | |||
7.14 | 数据域封装的优点是什么? | |||
7.15 | 在下面的代码中,Circle类中的radius是私有的,myCircle是Circle类的一个对象,下面的代码会出问题吗?解释原因。 public class Circle { private double radius = 1.0; /** Find the area of this circle */ double getArea() { return radius * radius * Math.PI; } public static void main(String[] args) { Circle myCircle = new Circle(); System.out.println("Radius is " + myCircle.radius); } } | |||
7.16 | 如果一个类只有私有数据域并且没有set方法,它是不可变类吗? | |||
7.17 | 如果一个类的所有数据域都是基本数据类型的私有数据域,并且不包含set方法,它是不可变类吗? | |||
7.18 | 描述传递基本类型参数和传递引用类型参数的区别。给出下面程序的输出: public class Test{ public static void main(String[] args){ Count myCount=new Count(); int times=0; for(int i=0;i<100;i++) increment(myCount,times); System.out.println(“count is ”+ unt); System.out.println(“times is ”+ myCount. times);} public static void increment(Count c,int times){ c.count++; times++;} } public class Count{ public int count; Count(int c){count=c;} Count(){count=1;}} | |||
7.19 | 给出下列程序的输出:: public class Test { public static void main(String[] args) { Circle circle1 = new Circle(1); Circle circle2 = new Circle(2); swap1(circle1, circle2); System.out.println("After swap1: circle1 = " + circle1.radius + " circle2 = " + circle2.radius); swap2(circle1, circle2); System.out.println("After swap2: circle1 = " + circle1.radius + " circle2 = " + circle2.radius); } public static void swap1(Circle x, Circle y) { Circle temp = x; x = y; y = temp; } public static void swap2(Circle x, Circle y) { double temp = x.radius; x.radius = y.radius; y.radius = temp; } } class Circle { double radius; Circle(double newRadius) { radius = newRadius; } } | |||
7.20 | 给出下列代码的输出: (1)public class Test{ public static void main(String[] args){ int[] a={1,2}; swap(a[0],a[1]); System.out.println(“a[0]=”+a[0]+”a[1]=”+a[1]);} public static void swap(int n1,int n2){ int temp=n1; n1=n2; n2=temp;} } (2)public class Test{ public static void main(String[] args){ int[] a={1,2}; swap(a); System.out.println(“a[0]=”+a[0]+”a[1]=”+a[1]);} public static void swap(int[] a){ int temp=a[0]; a[0]=a[1]; a[1]=temp;} } (3) public class Test{ public static void main(String[] args){ T t=new T(); swap(T); System.out.println(“e1=”+t.e1+”e2=”+t.e2);} public static void swap(T t){ int temp=t.e1; t.e1=t.e2; t.e2=temp;} } class T{ int e1=1; int e2=2; } (4) public class Test{ public static void main(String[] args){ T t1=new T(); T t2=new T(); System.out.println(“e1=”+t1.e1+”e2=”+t1.e2); System.out.println(“e1=”+t2.e1+”e2=”+t2.e2); } } class T{ static int e1=0; int e2=0; T(){ e1++; e2=1;} } | |||
7.21 | 下面程序的输出是什么? public class Foo { static int i = 0; static int j = 0; public static void main(String[] args) { int i = 2; int k = 3; { int j = 3; System.out.println("i + j is " + i + j); } k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); } } | |||
7.22 | 简述关键字this的作用,下列代码有什么错误? 1 public class C { 2 int p; 3 4 public void setP(int p) { 5 p = p; 6 } 7 } | |||
7.23 | 下列代码有什么错误? 1 public class Test { 2 public static void main(String[] args) { 3 java.util.Date[] dates = new java.util.Date[10]; 4 System.out.println(dates[0]); 5 System.out.println(dates[0].toString()); 6 } 7 } | |||
7.24 | 如果重新定义没有set方法的Loan类,这个类是不可变的吗? 1 public class Loan { 2 private double annualInterestRate; 3 private int numberOfYears; 4 private double loanAmount; 5 private java.util.Date loanDate; 6 7 /** Default constructor */ 8 public Loan() { 9 this(7.5, 30, 100000); 10 } 11 12 /** Construct a loan with specified annual interest rate, 13 number of years and loan amount 14 */ 15 public Loan(double annualInterestRate, int numberOfYears, 16 double loanAmount) { 17 this.annualInterestRate = annualInterestRate; 18 this.numberOfYears = numberOfYears; 19 this.loanAmount = loanAmount; 20 loanDate = new java.util.Date(); 21 } 22 23 /** Return annualInterestRate */ 24 public double getAnnualInterestRate() { 25 return annualInterestRate; 26 } 27 28 /** Set a new annualInterestRate */ 29 public void setAnnualInterestRate(double annualInterestRate) { 30 this.annualInterestRate = annualInterestRate; 31 } 32 33 /** Return numberOfYears */ 34 public int getNumberOfYears() { 35 return numberOfYears; 36 } 37 38 /** Set a new numberOfYears */ 39 public void setNumberOfYears(int numberOfYears) { 40 this.numberOfYears = numberOfYears; 41 } 42 43 /** Return loanAmount */ 44 public double getLoanAmount() { 45 return loanAmount; 46 } 47 48 /** Set a newloanAmount */ 49 public void setLoanAmount(double loanAmount) { 50 this.loanAmount = loanAmount; 51 } 52 53 /** Find monthly payment */ 54 public double getMonthlyPayment() { 55 double monthlyInterestRate = annualInterestRate / 1200; 56 return loanAmount * monthlyInterestRate / (1 - 57 (Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12))); 58 } 59 60 /** Find total payment */ 61 public double getTotalPayment() { 62 return getMonthlyPayment() * numberOfYears * 12; 63 } 64 65 /** Return loan date */ 66 public java.util.Date getLoanDate() { 67 return loanDate; 68 } 69 } 1 import javax.swing.JOptionPane; 2 3 public class TestLoanClass { 4 /** Main method */ 5 public static void main(String[] args) { 6 // Enter yearly interest rate 7 String annualInterestRateString = JOptionPane.showInputDialog( 8 "Enter yearly interest rate, for example 8.25:"); 9 10 // Convert string to double 11 double annualInterestRate = 12 Double.parseDouble(annualInterestRateString); 13 14 // Enter number of years 15 String numberOfYearsString = JOptionPane.showInputDialog( 16 "Enter number of years as an integer, \nfor example 5:"); 17 18 // Convert string to int 19 int numberOfYears = Integer.parseInt(numberOfYearsString); 20 21 // Enter loan amount 22 String loanString = JOptionPane.showInputDialog( 23 "Enter loan amount, for example 120000.95:"); 24 25 // Convert string to double 26 double loanAmount = Double.parseDouble(loanString); 27 28 // Create Loan object 29 Loan loan = 30 new Loan(annualInterestRate, numberOfYears, loanAmount); 31 32 // Format to keep two digits after the decimal point 33 double monthlyPayment = 34 (int)(MonthlyPayment() * 100) / 100.0; 35 double totalPayment = 36 (int)(TotalPayment() * 100) / 100.0; 37 38 // Display results 39 String output = "The loan was created on " + 40 LoanDate().toString() + "\nThe monthly payment is " + 41 monthlyPayment + "\nThe total payment is " + totalPayment; 42 JOptionPane.showMessageDialog(null, output); 43 } 44 } | |||
7.25 | StackOfIntegers 类是不可变的吗? 1 public class StackOfIntegers { 2 private int[] elements; 3 private int size; 4 public static final int DEFAULT_CAPACITY = 16; 5 6 /** Construct a stack with the default capacity 16 */ java接口可以创建对象吗 7 public StackOfIntegers() { 8 this(DEFAULT_CAPACITY); 9 } 10 11 /** Construct a stack with the specified maximum capacity */ 12 public StackOfIntegers(int capacity) { 13 elements = new int[capacity]; 14 } 15 16 /** Push a new integer into the top of the stack */ 17 public int push(int value) { 18 if (size >= elements.length) { 19 int[] temp = new int[elements.length * 2]; 20 System.arraycopy(elements, 0, temp, 0, elements.length); 21 elements = temp; 22 } 23 24 return elements[size++] = value; 25 } 26 27 /** Return and remove the top element from the stack */ 28 public int pop() { 29 return elements[——size]; 30 } 31 32 /** Return the top element from the stack */ 33 public int peek() { 34 return elements[size - 1]; 35 } 36 37 /** Test whether the stack is empty */ 38 public boolean empty() { 39 return size == 0; 40 } 41 42 /** Return the number of elements in the stack */ 43 public int getSize() { 44 return size; 45 } 46 } | |||
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论