1.Java简介和环境搭建
public class Hello {
public static void main(String[] args) {
System.out.println("I am the best!");
}
}
2.Java变量和数据类型
创建一个Calculator类,在main函数中创建两个变量,并分别赋予不同的整数,最后使用Math的内置函数打印出较大的那一个。
public class Calculator {
public static void main(String[] args ) {
int num1 = 15;
int num2 = 20;
int largerNum = Math.max(num1, num2);
System.out.println(largerNum);
}
}
3.条件判断和循环
请创建一个永久循环,在循环中,提示用户输入两个数字,并打印出两个数字区间内的所有奇数。如果用户输入1和5,就打印出1,3,5。如果用户输入的第一个数字大于第二个数字,就结束循环。
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.print("Please enter num1: ");
int num1 = scanner.nextInt();
System.out.print("Please enter num2: ");
int num2 = scanner.nextInt();
if(num1 > num2) {
break;
}
for (int i = num1; i <= num2; i++) {
if (i % 2 != 0) {
System.out.println(i);
}
}
}
4.方法和参数
创建一个自己的方法sumList(array1, array2),参数必须是两个长度相同的数组,然后在方法中将这两个数组的元素依次相加,返回一个含有相加元素的新数组含有参数。以下是调用此方法的效果:
int[] array1 = {1, 5, 8, 9};
int[] array2 = {2, 4, 8, 1};
int[] sumArray = sumArray(array1, array2); // {3, 9, 16, 10}
答案
public static int[] sumArray(int[] array1, int[] array2) {
int[] newArray = new int[array1.length];
for (int i = 0; i < newArray.length; i++) {
newArray[i] = array1[i] + array2[i];
}
return newArray;
}
5. 类和实例,包的调用
创建一个Car类,其中包含brand,model,year属性。这个类初始化的时候必须要传入品牌名字(比如Subaru),初始化的时候车的型号和生产年要被默认设定为”xxx“和0,Car类还需要提供内置方法用来修改车型号和生产年,还有可以打印出完整车信息的方法。
请根据要求定义出这个完整的类,并在另一个类中调用此类,创建一个实例,然后使用内置方法修改车的型号和生产年,最后使用类的方法打印出车的完整信息。
class Car {
private String brand;
private String model;
private int year;
public Car(String brand) {
this.brand = brand;
this.model = "xxx";
this.year = 0;
}
public void setModel(String model) {
this.model = model;
}
public void setYear(int year) {
this.year = year;
}
public String getInfo() {
return String.format("Brand: %s, Model: %s, Year: %s", brand, model, year);
}
}
public class Main {
public static void main(String[] args) {
Car newCar = new Car("Subaru");
newCar.setModel("WRX");
newCar.setYear(2020);
System.out.println(newCar.getInfo());
}
}
6. 封装,继承,多态,接口
请创建一个Game类,其中包含score数据,请使用封装的概念来设计此类,并在其中创建一个displayInfo方法。然后再创建两个子类VideoGame,PhoneGame继承Game类,并分别重写displayInfo方法,然后调用这两个类的实例来查看区别。(display的逻辑可以很简单,打印出游戏的类型和分数即可)
public class Game {
private int score;
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public void displayInfo() {
System.out.printf("Type: general, Score: %s\n", score);
}
}
public class VideoGame extends Game{
public void displayInfo() {
System.out.printf("Type: video, Score: %s\n", getScore());
}
}
public class PhoneGame extends Game{
public void displayInfo() {
System.out.printf("Type: phone, Score: %s\n", getScore());
}
}
7. 异常处理, 文件读写, 进阶学习
创建一个专门操作文件的类FileHandler,其中包括三个static方法:
- createOrDeleteFile(String, boolean) 用于创建和删除文件,第一个参数是要操作的文件名,第二个布尔参数决定操作类型:True就是创建,否则删除。
- writeToFile(String, String) 将内容写入文件,第一个参数是文件名,第二个参数是具体的字符串内容。
- readFile(String) 用于读取文件,参数是文件名,返回数据是文件内容。
完成类的设计后,如果要创建一个新文件 myFile.txt,并将 Hello Word 写入文件,最后读取文件后再删除文件,我只需要调用以下的代码即可:
String filename = "myFile.txt";
FileHandler.createOrDeleteFile(filename, true);
FileHandler.writeToFile(filename, "Hello World");
System.out.println(FileHandler.readFile(filename));
FileHandler.createOrDeleteFile(filename, false);
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class FileHandler {
public static void createOrDeleteFile(String filename, boolean create) {
try {
File newFile = new File(filename);
if (create) {
newFile.createNewFile();
} else {
newFile.delete();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void writeToFile(String filename, String content) {
try {
FileWriter writer = new FileWriter(filename);
writer.write(content);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String readFile(String filename) {
String content = "";
try {
File myFile = new File(filename);
Scanner scanner = new Scanner(myFile);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
content += line;
}
scanner.close();
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
}