选择
java是解释性语言。
short占2字节,范围-2的15次方到2的15次方-1
byte,short,int,long,float,double
-
btn.addActionListener(new ActionListener(){
-
public void actionPerformed(ActionEvent event){
-
String s = input.getText();
-
output.setText("Hello");
-
}
-
})
该内部类用于进行事件处理
-
public class test {
-
public static void main(String[] args) {
-
int x=4,j=0;
-
switch (x){
-
case 1:j++;
-
case 2:j++;
-
case 3:j++;
-
case 4:j++;
-
case 5:j++;
-
break;
-
default:j++;
-
}
-
System.out.println(j);
-
}
-
}
输出结果为:2
-
public class test {
-
private int x;
-
public test(){
-
x = 0;
-
}
-
public void test(int f){
-
x = f;
-
}
-
}
该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()直接访问。
-
class Test{
-
private int m;
-
public static void fun(){
-
}
-
}
static方法才能访问static变量
字符输出流:FileWriter
字符输入流:FileReader
字节输入流:FileInputStream
字节输出流:FileOutputStream
在switch(expression)语句中,expression的数据类型不能是double
内部类不能编译生成
-
public class B{
-
class A{}
-
}
-
class C{
-
}
super指向当前对象的父类对象
类变量是用关键字static声明,实例变量不是用static声明
程序填空
程序改错
数组越界,死循环,不能在类外访问类内的private数据
-
import java.io.*;
-
public class test {
-
public static void main(String[] args) {
-
int i = 0;
-
String Str[]={"1","2","3"};
-
while (i<=3){
-
System.out.println(Str[i]);
-
i++;
-
}
-
}
-
}
原因:数组越界
程序功能
-
public class TestFile {
-
public static void main(String[] args) throws IOException {
-
FileInputStream is = new FileInputStream(args[0]);
-
InputStreamReader in = new InputStreamReader(is);
-
BufferedReader br = new BufferedReader(in);
-
String str ;
-
while((str = br.readLine()) != null){
-
System.out.println(str);
-
}
-
}
-
}
实现了一个什么功能?
程序代码实现了“将从键盘输入的内容通过屏幕显示”功能。
请用符合规定的Java文件命名该文件。
TestFile.java
FileInputStream是节点流还是处理流?
节点流
-
public class SumPower{
-
public static final int N = 30;
-
public static long power(int n){
-
int i = n;
-
long power = 1;
-
do{
-
power *= i;
-
i--;
-
}while (i > 0);
-
return power;
-
}
-
public static long sumPower(int n){
-
int i = 1;
-
long sum = 0;
-
do{
-
sum += power(i++);
-
}while (i <= n);
-
return sum;
-
}
-
public static void main(String[] args) {
-
System.out.println("计算结果: "+sumPower(SumPower.N));
-
}
-
}
该程序的功能是什么?
计算1!+2!+。。。+n!
final关键字是什么含义?
定义常量
B、C、D、E行定义的返回值为long类型,可否将这些long修改为int
不可以,阶乘的返回值有可能超过int表示的范围
-
public class testscan {
-
public static void main(String[] args) throws IOException {
-
Scanner sc = new Scanner(System.in);
-
FileOutputStream outfile = new FileOutputStream("test.txt");
-
OutputStreamWriter osw = new OutputStreamWriter(outfile);
-
BufferedWriter bw = new BufferedWriter(osw);
-
bw.write(sc.nextLine());
-
bw.close();
-
}
-
}
描述该程序所实现的功能。
从控制台读入一行字符串,写入test.txt
请解释main函数声明部分的throws Exception的作用及程序BufferedWriter流的作用。
throws Exception的作用是抛出异常,非彻底处理异常,而是把异常抛给了调用该方法的方法去处理。
BufferedWriter流的作用是将文本写入字符输出流,缓存各个字符,从而提供单个字符、数组和字符串的高效写入。
-
public class ExamDefineException{
-
public static void main(String[] args) {
-
final int MIN = 25,MAX = 40;
-
Scanner scan = new Scanner(System.in);
-
OutOfRangeException problem = new OutOfRangeException();
-
System.out.print("输入"+MIN+"到"+MAX+"之间的数字:");
-
try{
-
int value = scan.nextInt();
-
if(value < MIN || value > MAX){
-
throw problem;
-
}
-
}catch (OutOfRangeException e){
-
System.out.println(e.toString());
-
}
-
System.out.println("主方法结束");
-
}
-
}
-
class OutOfRangeException extends Exception{
-
public OutOfRangeException(){
-
super("输入值超出范围");
-
}
-
}
写出该程序的功能
键盘输入25到40之间的整数,如果不在范围内则抛出异常,使用自定义的异常类。
程序编程
通过自己定义一个“点”(Point)类用来表示三维空间中的点(有三个坐标)。完成下面的设计要求:
(1)可以生成具有特定坐标的点对象
(2)提供可以设置三个坐标值的方法
(3)在这个类中提供可以计算该“点”原点距离平方和的方法
(4)在这个类中提供可以计算该“点”据空间任意一点距离平方和的方法
-
class Point {
-
private int x;
-
private int y;
-
private int z;
-
public Point(int x, int y, int z) {
-
this.x = x;
-
this.y = y;
-
this.z = z;
-
}
-
public void setX(int x) {
-
this.x = x;
-
}
-
public void setY(int y) {
-
this.y = y;
-
}
-
public void setZ(int z) {
-
this.z = z;
-
}
-
public int pingfang() {
-
return x * x + y * y + z * z;
-
}
-
public int pingfanghe(Point p){
-
return (x - p.x)*(x - p.x) + (y - p.y)*(y - p.y) + (z - p.z)*(z - p.z);
-
}
-
}
在控制台输入一个整数,编写一个程序计算该整数的各位数字之和。
如:输入:123456
输出:各位数字之和1+2+3+4+5+6=21
-
public class Sum{
-
public static void main(String[] args) {
-
String str = null;
-
System.out.println("请输入一个整数:");
-
Scanner sc = new Scanner(System.in);
-
str = sc.nextLine();
-
int sum = 0;
-
for(int i = 0; i < str.length(); i++){
-
int temp = Integer.parseInt(str.charAt(i) + "");
-
sum += temp;
-
}
-
System.out.println("该整数的各位数字之和为:" + sum);
-
}
-
}
创建一个复数类 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)
-
class Complex{
-
private double RealPart;
-
private double ImaginaryPart;
-
public Complex(){
-
RealPart = 0.0;
-
ImaginaryPart = 0.0;
-
}
-
public Complex(double a,double b){
-
RealPart = a;
-
ImaginaryPart = b;
-
}
-
public void sub(Complex c){
-
RealPart = RealPart - c.RealPart;
-
ImaginaryPart = ImaginaryPart - c.ImaginaryPart;
-
}
-
}
编写一个有关多态的程序
功能要求:
(1)定义一个名为Number的类和名为INumber的接口,前者实现后者;
(2)INumber接口中有至少两个名字相同的max()方法,其中,一个支持返回两个整型数中的较大者,另一个支持返回2个双精度浮点中的较大者;
(3)在Number类中实现每一个max()方法
(4)要求写出测试该功能 的Exam2类(该包含main()方法)。
-
public class Number implements INumber{
-
@Override
-
public int max(int a, int b) {
-
if(a >= b){
-
return a;
-
}
-
return b;
-
}
-
@Override
-
public double max(double a, double b) {
-
if (a >= b){
-
return a;
-
}
-
return b;
-
}
-
}
-
public interface INumber {
-
int max(int a,int b);
-
double max(double a,double b);
-
}
-
public class Exam2 {
-
public static void main(String[] args) {
-
Number n = new Number();
-
System.out.println(n.max(10,20));
-
System.out.println(n.max(10.5,20.5));
-
}
-
}
编写一个名为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
-
class Exam1{
-
public static void chat(){
-
Scanner sc = new Scanner(System.in);
-
while(true){
-
System.out.print("Input");
-
String text = sc.nextLine();
-
if(text.equals("end")){
-
break;
-
}
-
String str[] = text.split(" ");
-
System.out.print("\nRevEcho:");
-
for(int i=str.length-1;i>=0;i--){
-
System.out.print(str[i]+" ");
-
}
-
System.out.println();
-
}
-
}
-
}
设计一个多线程程序,一个线程完成s=1+2+。。。+10000,一个线程计算m=100!,主线程输出s与m的计算结果。
-
public class TEST {
-
public static void main(String[] args) {
-
Callable<Integer> callable1 = new Callable<Integer>() {
-
public Integer call() {
-
int s = 0;
-
for (int i = 0; i < 100; i++) {
-
s += i;
-
}
-
return s;
-
}
-
};
-
FutureTask<Integer> s = new FutureTask<Integer>(callable1);
-
Callable<BigInteger> callable2 = new Callable<BigInteger>() {
-
public BigInteger call() {
-
BigInteger m = new BigInteger("1");
-
for (int i = 1; i < 100; i++) {
-
m = m.multiply(BigInteger.valueOf(i));
-
}
-
return m;
-
}
-
};
-
FutureTask<BigInteger> m = new FutureTask<BigInteger>(callable2);
-
new Thread(s).start();
-
new Thread(m).start();
-
try {
-
System.out.println(s.get());
-
System.out.println(m.get());
-
}
-
catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
}
编程实现,从控制台上输入一批数据,以end结束,求这批数据的最大值
-
public class TEST {
-
public static void main(String[] args) {
-
Scanner sc = new Scanner(System.in);
-
int max = 0;
-
String number = sc.next();
-
while (!"end".equals(number)){
-
try {
-
max = Integer.parseInt(number)>=max?Integer.parseInt(number):max;
-
}catch (NumberFormatException e){
-
System.out.println("输入错误");
-
}
-
number = sc.next();
-
}
-
System.out.println(max);
-
}
-
}
总结
输入:
-
//输入整数
-
Scanner sc = new Scanner(System.in);
-
String str = sc.nextLine();
-
int n = sc.nextInt();
-
//输入字符串
-
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
-
String str = br.readLine();
java开发语言
来自专栏
大学专业课程
Li清水 4篇文章 0人订阅
AI必读
发布于2024-05-19著作权归作者所有