带你一小时精通JAVA

选择 

java是解释性语言。


short占2字节,范围-2的15次方到2的15次方-1

byte,short,int,long,float,double


 
  1. btn.addActionListener(new ActionListener(){

  2.  
  3. public void actionPerformed(ActionEvent event){

  4. String s = input.getText();

  5. output.setText("Hello");

  6. }

  7. })

 该内部类用于进行事件处理


 
  1. public class test {

  2. public static void main(String[] args) {

  3. int x=4,j=0;

  4. switch (x){

  5. case 1:j++;

  6. case 2:j++;

  7. case 3:j++;

  8. case 4:j++;

  9. case 5:j++;

  10. break;

  11. default:j++;

  12. }

  13. System.out.println(j);

  14. }

  15. }

输出结果为:2


 
  1. public class test {

  2. private int x;

  3. public test(){

  4. x = 0;

  5. }

  6. public void test(int f){

  7. x = f;

  8. }

  9. }

该test类中,只有1个构造方法。

构造方法没有返回值。 


在Java中,private不可以用来修饰类


 现有一个一维数组a,若要实现对其的第一个元素到第七个元素进行排序,那么以下语句正确的是

Array.sort(a,0,7)


类装载的功能是为执行程序寻找和装载所需要的类 


this可以出现在类的构造方法中,可以出现在非static修饰的成员方法中,可以代表实例对象本身,

但不可以用来引用父类的成员变量


 super()必须是子类构造函数的第一条语句

子类对象向上转为父类对象是安全的(父类对象向下转为子类对象是不安全的)


 

通过方法重载,方法覆盖可以实现多态

父类引用指向子类对象可以实现多态

Java语言支持运行时多态


Java语言中,与static修饰符无关的是:实例变量和实例方法

有关的是:类变量和类方法,System类的in和out对象,入口main()方法


 可以使用String类,StringBuffer类封装字符串

字符串中的一个中文字符和英文字符都占2个字节


如下代码,将private int m改为private static int m可以使成员变量m被函数fun()直接访问。

 
  1. class Test{

  2. private int m;

  3. public static void fun(){

  4.  
  5. }

  6. }

 static方法才能访问static变量


字符输出流:FileWriter

字符输入流:FileReader

字节输入流:FileInputStream

字节输出流:FileOutputStream


在switch(expression)语句中,expression的数据类型不能是double


内部类不能编译生成

 
  1. public class B{

  2. class A{}

  3. }

  4. class C{

  5. }


super指向当前对象的父类对象


 类变量是用关键字static声明,实例变量不是用static声明


程序填空

 

 

 

 

程序改错

数组越界,死循环,不能在类外访问类内的private数据

 
  1. import java.io.*;

  2.  
  3. public class test {

  4. public static void main(String[] args) {

  5. int i = 0;

  6. String Str[]={"1","2","3"};

  7. while (i<=3){

  8. System.out.println(Str[i]);

  9. i++;

  10. }

  11. }

  12. }

 原因:数组越界

 

 

 

 

 

 程序功能

 
  1. public class TestFile {

  2. public static void main(String[] args) throws IOException {

  3. FileInputStream is = new FileInputStream(args[0]);

  4. InputStreamReader in = new InputStreamReader(is);

  5. BufferedReader br = new BufferedReader(in);

  6. String str ;

  7. while((str = br.readLine()) != null){

  8. System.out.println(str);

  9. }

  10. }

  11. }

实现了一个什么功能?

程序代码实现了“将从键盘输入的内容通过屏幕显示”功能。

请用符合规定的Java文件命名该文件。

TestFile.java

FileInputStream是节点流还是处理流?

节点流


 
  1. public class SumPower{

  2. public static final int N = 30;

  3. public static long power(int n){

  4. int i = n;

  5. long power = 1;

  6. do{

  7. power *= i;

  8. i--;

  9. }while (i > 0);

  10. return power;

  11. }

  12. public static long sumPower(int n){

  13. int i = 1;

  14. long sum = 0;

  15. do{

  16. sum += power(i++);

  17. }while (i <= n);

  18. return sum;

  19. }

  20.  
  21. public static void main(String[] args) {

  22. System.out.println("计算结果: "+sumPower(SumPower.N));

  23. }

  24. }

该程序的功能是什么?

计算1!+2!+。。。+n!

final关键字是什么含义?

定义常量

B、C、D、E行定义的返回值为long类型,可否将这些long修改为int

不可以,阶乘的返回值有可能超过int表示的范围


 
  1. public class testscan {

  2. public static void main(String[] args) throws IOException {

  3. Scanner sc = new Scanner(System.in);

  4. FileOutputStream outfile = new FileOutputStream("test.txt");

  5. OutputStreamWriter osw = new OutputStreamWriter(outfile);

  6. BufferedWriter bw = new BufferedWriter(osw);

  7. bw.write(sc.nextLine());

  8. bw.close();

  9. }

  10. }

描述该程序所实现的功能。

从控制台读入一行字符串,写入test.txt

 请解释main函数声明部分的throws Exception的作用及程序BufferedWriter流的作用。

throws Exception的作用是抛出异常,非彻底处理异常,而是把异常抛给了调用该方法的方法去处理。

BufferedWriter流的作用是将文本写入字符输出流,缓存各个字符,从而提供单个字符、数组和字符串的高效写入。 

 


 
  1. public class ExamDefineException{

  2. public static void main(String[] args) {

  3. final int MIN = 25,MAX = 40;

  4. Scanner scan = new Scanner(System.in);

  5. OutOfRangeException problem = new OutOfRangeException();

  6. System.out.print("输入"+MIN+"到"+MAX+"之间的数字:");

  7. try{

  8. int value = scan.nextInt();

  9. if(value < MIN || value > MAX){

  10. throw problem;

  11. }

  12. }catch (OutOfRangeException e){

  13. System.out.println(e.toString());

  14. }

  15. System.out.println("主方法结束");

  16. }

  17. }

  18. class OutOfRangeException extends Exception{

  19. public OutOfRangeException(){

  20. super("输入值超出范围");

  21. }

  22. }

 写出该程序的功能

键盘输入25到40之间的整数,如果不在范围内则抛出异常,使用自定义的异常类。

 


程序编程

 通过自己定义一个“点”(Point)类用来表示三维空间中的点(有三个坐标)。完成下面的设计要求:

(1)可以生成具有特定坐标的点对象

(2)提供可以设置三个坐标值的方法

(3)在这个类中提供可以计算该“点”原点距离平方和的方法

(4)在这个类中提供可以计算该“点”据空间任意一点距离平方和的方法

 
  1. class Point {

  2. private int x;

  3. private int y;

  4. private int z;

  5. public Point(int x, int y, int z) {

  6. this.x = x;

  7. this.y = y;

  8. this.z = z;

  9. }

  10. public void setX(int x) {

  11. this.x = x;

  12. }

  13. public void setY(int y) {

  14. this.y = y;

  15. }

  16. public void setZ(int z) {

  17. this.z = z;

  18. }

  19. public int pingfang() {

  20. return x * x + y * y + z * z;

  21. }

  22. public int pingfanghe(Point p){

  23. return (x - p.x)*(x - p.x) + (y - p.y)*(y - p.y) + (z - p.z)*(z - p.z);

  24. }

  25. }


 

在控制台输入一个整数,编写一个程序计算该整数的各位数字之和。

如:输入:123456

        输出:各位数字之和1+2+3+4+5+6=21

 
  1. public class Sum{

  2. public static void main(String[] args) {

  3. String str = null;

  4. System.out.println("请输入一个整数:");

  5. Scanner sc = new Scanner(System.in);

  6. str = sc.nextLine();

  7. int sum = 0;

  8. for(int i = 0; i < str.length(); i++){

  9. int temp = Integer.parseInt(str.charAt(i) + "");

  10. sum += temp;

  11. }

  12. System.out.println("该整数的各位数字之和为:" + sum);

  13. }

  14. }

 


创建一个复数类 Complex,复数具有如下格式:RealPart+ImaginaryPart*i,其中,i为-1的平方根。要求如下

(1)包括 两个私有成员变量RealPart、ImaginaryPart,分别保存复数的实部与虚部。

(2)提供两个构造方法,public Complex()和public Complex(double a,double b),前者用于将复数的实部和虚部初始化为0,后者用于将复数的实部与虚部分别初始化为a、b。

(3)实现复数相减的运算方法sub(Complex c)

 
  1. class Complex{

  2. private double RealPart;

  3. private double ImaginaryPart;

  4. public Complex(){

  5. RealPart = 0.0;

  6. ImaginaryPart = 0.0;

  7. }

  8. public Complex(double a,double b){

  9. RealPart = a;

  10. ImaginaryPart = b;

  11. }

  12. public void sub(Complex c){

  13. RealPart = RealPart - c.RealPart;

  14. ImaginaryPart = ImaginaryPart - c.ImaginaryPart;

  15. }

  16. }

 


编写一个有关多态的程序

功能要求:

(1)定义一个名为Number的类和名为INumber的接口,前者实现后者;

(2)INumber接口中有至少两个名字相同的max()方法,其中,一个支持返回两个整型数中的较大者,另一个支持返回2个双精度浮点中的较大者;

(3)在Number类中实现每一个max()方法

(4)要求写出测试该功能 的Exam2类(该包含main()方法)。

 
  1. public class Number implements INumber{

  2. @Override

  3. public int max(int a, int b) {

  4. if(a >= b){

  5. return a;

  6. }

  7. return b;

  8. }

  9.  
  10. @Override

  11. public double max(double a, double b) {

  12. if (a >= b){

  13. return a;

  14. }

  15. return b;

  16. }

  17. }

  18.  
  19. public interface INumber {

  20. int max(int a,int b);

  21. double max(double a,double b);

  22. }

  23.  
  24. public class Exam2 {

  25. public static void main(String[] args) {

  26. Number n = new Number();

  27. System.out.println(n.max(10,20));

  28. System.out.println(n.max(10.5,20.5));

  29. }

  30. }


编写一个名为Exam1类,至少包含一个chat()方法,可以实现如下功能。

(1)接收来自键盘输入的一句英语(不包含标点符号,各单词间用空格分隔);

(2)单词能立刻逆序回显在屏幕上,并等待输入下一句;

(3)当输入end是结束循环。 

效果示例:

 Input:My name is White

RevEcho:White is name My

Input:I am a student

RevEcho:student a am I

Input:end

 
  1. class Exam1{

  2. public static void chat(){

  3. Scanner sc = new Scanner(System.in);

  4. while(true){

  5. System.out.print("Input");

  6. String text = sc.nextLine();

  7. if(text.equals("end")){

  8. break;

  9. }

  10. String str[] = text.split(" ");

  11. System.out.print("\nRevEcho:");

  12. for(int i=str.length-1;i>=0;i--){

  13. System.out.print(str[i]+" ");

  14. }

  15. System.out.println();

  16. }

  17. }

  18. }


设计一个多线程程序,一个线程完成s=1+2+。。。+10000,一个线程计算m=100!,主线程输出s与m的计算结果。

 
  1. public class TEST {

  2. public static void main(String[] args) {

  3. Callable<Integer> callable1 = new Callable<Integer>() {

  4. public Integer call() {

  5. int s = 0;

  6. for (int i = 0; i < 100; i++) {

  7. s += i;

  8. }

  9. return s;

  10. }

  11. };

  12. FutureTask<Integer> s = new FutureTask<Integer>(callable1);

  13. Callable<BigInteger> callable2 = new Callable<BigInteger>() {

  14. public BigInteger call() {

  15. BigInteger m = new BigInteger("1");

  16. for (int i = 1; i < 100; i++) {

  17. m = m.multiply(BigInteger.valueOf(i));

  18. }

  19. return m;

  20. }

  21. };

  22. FutureTask<BigInteger> m = new FutureTask<BigInteger>(callable2);

  23. new Thread(s).start();

  24. new Thread(m).start();

  25. try {

  26. System.out.println(s.get());

  27. System.out.println(m.get());

  28. }

  29. catch (Exception e) {

  30. e.printStackTrace();

  31. }

  32. }

  33. }


编程实现,从控制台上输入一批数据,以end结束,求这批数据的最大值

 
  1. public class TEST {

  2. public static void main(String[] args) {

  3. Scanner sc = new Scanner(System.in);

  4. int max = 0;

  5. String number = sc.next();

  6. while (!"end".equals(number)){

  7. try {

  8. max = Integer.parseInt(number)>=max?Integer.parseInt(number):max;

  9. }catch (NumberFormatException e){

  10. System.out.println("输入错误");

  11. }

  12. number = sc.next();

  13. }

  14. System.out.println(max);

  15. }

  16. }

 

总结 

 输入:

 
  1. //输入整数

  2.  
  3. Scanner sc = new Scanner(System.in);

  4. String str = sc.nextLine();

  5. int n = sc.nextInt();

  6.  
  7. //输入字符串

  8. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

  9. String str = br.readLine();

 

 

java开发语言

来自专栏

大学专业课程

resize,m_fixed,h_64,w_64 Li清水 4篇文章  0人订阅

pointRight.png

subject.pngAI必读

发布于2024-05-19著作权归作者所有

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值