《Java程序设计》
编程题
- 编程实现将char类型的字符’语’的Unicode值输出,并转换为int类型输出,最后再将改int值转换为字符输出。
使用Character类的charValue()方法和intValue()方法来完成这个任务。charValue()方法用于将字符类型转换为int类型,而intValue()方法则用于将int类型转换回字符类型。 public class Main { public static void main(String[] args) { //定义字符’语’ char character = ‘语’;
//输出字符的Unicode值 System.out.println(“字符’语’的Unicode值是: ” + character);
//将字符转换为int类型并输出 int unicodeAsInt = Character.codePointAt(Character.toString(character)); System.out.println(“字符’语’的Unicode值转换为int类型是: ” + unicodeAsInt);
//将int类型转换回字符并输出 char characterFromInt = (char) unicodeAsInt; System.out.println(“int类型转换回字符是: ” + characterFromInt); } } |
- 通过文件字符流实现文件farrago.txt的内容复制到outagainc.txt文件。
首先创建两个File对象,分别对应输入和输出的文件。然后,它使用FileInputStream和FileOutputStream来读取和写入文件。通过一个循环,它从输入文件中读取数据,并将数据写入到输出文件中。当读取完输入文件后,复制操作就完成了。 import java.io.*;
public class Main { public static void main(String[] args) { File inputFile = new File(“farrago.txt”); File outputFile = new File(“outagainc.txt”);
try (FileInputStream fis = new FileInputStream(inputFile); FileOutputStream fos = new FileOutputStream(outputFile)) {
byte[] buffer = new byte[1024]; int length;
// 读取文件并写入到输出文件 while ((length = fis.read(buffer)) > 0) { fos.write(buffer, 0, length); } System.out.println(“文件复制成功!”); } catch (IOException e) { e.printStackTrace(); } } } |
- 编写Count类的对象赋予递增的序列号,并给出测试程序。
使用Java编写的Count类,它具有一个私有的整数字段currentCount,用于存储当前递增的序列号。该类还具有一个公共的静态方法nextCount(),该方法返回下一个递增的序列号。 public class Count { private static int currentCount = 0;
public static int nextCount() { return ++currentCount; } } 测试程序用于测试Count类的正确性: public class TestCount { public static void main(String[ ] args) { System.out.println(Count.nextCount()); // 输出 1 System.out.println(Count.nextCount()); // 输出 2 System.out.println(Count.nextCount()); // 输出 3 // 依此类推… } } |
- 编程实现测试程序类Test:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?(注:满足Math.sqrt()% 1 == 0条件的是一个完全平方数)
可以使用两层嵌套的循环来遍历整数x的可能取值。外层循环用于遍历x的范围,内层循环用于判断x + 100和x + 100 + 168是否是完全平方数。在内层循环中,我们可以使用Math.sqrt()函数来计算平方根,并判断是否为整数。 public class PerfectSquare { public static void main(String[] args) { for (int x = 1; ; x++) { if (isPerfectSquare(x + 100) && isPerfectSquare(x + 100 + 168)) { System.out.println(“符合条件的整数x是:” + x); break; } } } // 判断一个数是否是完全平方数 public static boolean isPerfectSquare(int num) { double sqrt = Math.sqrt(num); int sqrtInt = (int) sqrt; return sqrtInt * sqrtInt == num; } } |
- 用if语句实现将变量ch中的小写字母变为大写字母。
因为字符在计算机中是以ASCII值存储的,可以利用这个特性进行转换。 首先检查字符ch是否为小写字母。如果是,将其转换为大写字母。通过将字符ch减去’a’,然后加上’A’来实现这个转换。 public class Main { public static void main(String[] args) { char ch = ‘a’; // 假设这是你要转换的字符
if(ch >= ‘a’ && ch <= 'z'){ // 判断是否为小写字母 ch = (char)(ch – ‘a’ + ‘A’); // 转换小写字母为大写字母 }
System.out.println(ch); // 输出转换后的字符 } } |
- 编写程序输出下列结果
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
public class Main { public static void main(String[] args) { for (int i = 1; i <= 6; i++) { //外层循环变量i从1到6,表示需要输出的行数 for (int j = 1; j <= i; j++) { //内层循环变量j从1到i,表示当前行需要输出的数字个数 System.out.print(j + ” “); //输出每个数字和一个空格 } System.out.println(); //结束时使用输出一个换行符,以便开始下一行的输出 } } } 程序使用两个嵌套的for循环。外层循环变量i从1到6,表示需要输出的行数。内层循环变量j从1到i,表示当前行需要输出的数字个数。在内层循环中,使用System.out.print(j + ” “)输出每个数字和一个空格,然后在外层循环结束时使用System.out.println()输出一个换行符,以便开始下一行的输出。 |
- 编写程序输出1000以内的所有奇数
public class Main { public static void main(String[] args) { for (int i = 1; i < 1000; i += 2) { System.out.println(i); } } } 程序使用一个for循环来遍历1到999之间的所有整数。循环变量i从1开始,每次增加2,这样就可以保证每次循环时i都是奇数。在循环体内,使用System.out.println(i)输出当前的奇数。 |
- 用ifelse语句实现程序,将学时成绩等级按照ABCED输出。
import java.util.Scanner; public class GradeConverter { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(“请输入学时成绩:”); int hours = scanner.nextInt(); if (hours >= 90) { System.out.println(“A”); } else if (hours >= 80) { System.out.println(“B”); } else if (hours >= 70) { System.out.println(“C”); } else if (hours >= 60) { System.out.println(“D”); } else { System.out.println(“E”); } } } 程序首先定义了一个成绩数组,然后遍历这个数组。对于数组中的每个成绩,程序都会使用if-else语句判断其等级。如果成绩大于或等于90,则等级为A;如果成绩在80到89之间,则等级为B;如果成绩在70到79之间,则等级为C;如果成绩在60到69之间,则等级为D;否则,等级为E。 |
- 编写 Manager类对Employee类方法的重写
class Employee {
String name ;
int salary;
public Employee(String name,int salary){
this.name = name;
this.salary = salary;
}
public String getDetails( ){
return “Name: “+name+”
Salary: “+salary;
}
}
class Manager extends Employee { public Manager(String name, int salary) { super(name, salary); }
@Override public String getDetails() { return “Employee Name: ” + this.name + ” } } 在Employee类中,已经定义了一个名为getDetails的方法,该方法返回员工的姓名和薪水。 在这个Manager类中,首先调用父类Employee的构造方法来创建新的Manager对象。然后,使用@Override注解来表明要重写父类的方法。在getDetails方法中,在返回的字符串前添加了”Employee Name: “和”Employee Salary: “,使得输出的结果更加明确。 |
- 定义描述雇员信息的类EmpInfo,实例化该类的对象并进行访问。要求如下:
1)类EmpInfo含有name、designation、department三个属性;一个构造方法;一个print方法,用于输出类EmpInfo的三个属性。
2)编写一个测试类,要求创建两个对象 (“Robert Java”,”Manager”,”Coffee shop” );和(“Tom Java”,”Worker”,”Coffee shop” );,然后用print方法输出这两个对象。
定义 EmpInfo 类: public class EmpInfo { private String name; private String designation; private String department;
public EmpInfo(String name, String designation, String department) { this.name = name; this.designation = designation; this.department = department; }
public void print() { System.out.println(“Name: ” + name); System.out.println(“Designation: ” + designation); System.out.println(“Department: ” + department); } } 测试类,在主类中实例化 EmpInfo 对象,并使用 print 方法输出两个对象: public class Main { public static void main(String[] args) { EmpInfo empInfo1 = new EmpInfo(“Robert Java”, “Manager”, “Coffee shop”); empInfo1.print(); System.out.println();
EmpInfo empInfo2 = new EmpInfo(“Tom Java”, “Worker”, “Coffee shop”); empInfo2.print(); } } 运行主类 Main,将看到如下输出: Name: Robert Java Designation: Manager Department: Coffee shop
Name: Tom Java Designation: Worker Department: Coffee shop |
- 编程实现将字符串数组{ “String One”, “String Two”, “String Three” }中的每一个串转换成全部小写字母,然后按顺序输出。
使用Java的toLowerCase()方法,用一个for循环来遍历数组并按顺序打印每个字符串。 public class Main { public static void main(String[] args) { // 定义字符串数组 String[] strArray = { “String One”, “String Two”, “String Three” };
// 遍历数组并打印每个字符串的小写版本 for (String str : strArray) { System.out.println(str.toLowerCase()); } } } 当你运行这段代码时,它将打印出以下内容: string one string two string three |
- 补充完整HelloWorldApplet类, 使其完成功能就是在坐标(50,10)处打印出字符串:”Hello!”;并且其可以作为一个Application运行,在控制台打印出字符串:”Hello!” (注:请加上必要的注释)
// 导入必要的库 import java.applet.Applet; import java.awt.Graphics; // 创建一个名为HelloWorldApplet的类,继承自java.applet.Applet public class HelloWorldApplet extends Applet implements Runnable { // 初始化 Applet public void init() { // 设置 Applet 的大小为 200×100 setSize(200, 100); } // 绘制 Applet 的内容 public void paint(Graphics g) { // 在坐标(50,10)处打印字符串 “Hello!” g.drawString(“Hello!”, 50, 10); } // 作为 Application 运行时的主方法 public static void main(String[] args) { // 创建 HelloWorldApplet 对象并启动线程 HelloWorldApplet applet = new HelloWorldApplet(); Thread thread = new Thread(applet); thread.start(); } // 作为 Applet 运行时的线程RunMethod public void run() { // 作为 Application 运行时,在控制台打印字符串 “Hello!” System.out.println(“Hello!”); } } |
- 读下面程序,给出最后屏幕输出结果。
public class BitwiseDemo {
static final int VISIBLE = 1; static final int DRAGGABLE = 2;
static final int SELECTABLE = 4; static final int EDITABLE = 8;
public static void main(String[] args) {
int flags = 0;
flags = flags | VISIBLE;
flags = flags | DRAGGABLE;
if ((flags & VISIBLE) == VISIBLE) {
if ((flags & DRAGGABLE) == DRAGGABLE) {
System.out.println(“Flags are Visible and Draggable.”);
}
}
flags = flags | EDITABLE;
if ((flags & EDITABLE) == EDITABLE) {
System.out.println(“Flags are now also Editable.”);
}
}
}
该程序定义了一个名为BitwiseDemo的类,其中包含四个静态常量:VISIBLE、DRAGGABLE、SELECTABLE和EDITABLE,分别赋值为1、2、4和8。 在main方法中,首先定义了一个整型变量flags并初始化为0。然后通过按位或运算符(|)将flags与VISIBLE和DRAGGABLE进行按位或运算,将结果重新赋值给flags。接下来,使用按位与运算符(&)检查flags是否同时具有VISIBLE和DRAGGABLE标志,如果是,则输出”Flags are Visible and Draggable.”。 接着,再次使用按位或运算符将flags与EDITABLE进行按位或运算,将结果重新赋值给flags。最后,再次使用按位与运算符检查flags是否同时具有EDITABLE标志,如果是,则输出”Flags are now also Editable.”。 由于flags初始值为0,且没有其他代码修改flags的值,因此最终屏幕上的输出结果为: 程序的输出结果是: Flags are Visible and Draggable. Flags are now also Editable. |
- 按以下要求编写程序
(1) 编写Animal接口,接口中声明run() 方法
(2) 定义Bird类和Fish类实现Animal接口
(3) 编写Bird类和Fish类的测试程序,并调用其中的run()方法
// Animal接口 public interface Animal { |