关于System类的基本知识点:
2-5:重点是:置换
填空题有的系统给括号和局部变量等,填写信息不能冗余
下面的程序将唐诗《登鹳雀楼》写入登鹳雀楼.tx文件中,然后再将该文件的内容读出,打印在显示器上。 请阅读分析程序并填空。
import java.io.*;
public class FileRW {
public static void main(String[] args) {
String str = "白日依山尽\n";
char[] ch = str.toCharArray();
FileWriter fw = null;
FileReader fr = null;
try {
fw = new FileWriter("登鹳雀楼.txt");
fw.write(ch);
fw.write("黄河入海流\n");
fw.write("欲穷千里目\n");
fw.write("更上一层楼\n");
fw.close();
} catch (IOException e) {
System.err.println("流的创建、写和关闭都可能产生IOException异常。");
System.exit(-1);
}
try {
fr = new FileReader("登鹳雀楼.txt");
int i;
while ((i = fr.read()) != -1) {
System.out.print((char) i);
}
System.out.println();
fr.close();
} catch (FileNotFoundException e) {
System.err.println("文件未找到!");
System.exit(-2);
} catch (IOException e) {
System.err.println("读和关闭都可能产生IOException异常。");
System.exit(-3);
}
}
}
该程序将10个整数写入一个文件,然后读出来并显示。
import java.io.*;
public class TestFileStream {
public static void main(String[] args) throws IOException {
// Create an output stream to the file
FileOutputStream output = new FileOutputStream("temp.dat");
// Output values to the file
for (int i = 1; i <= 10; i++)
output.write(i);
// Close the output stream
output.close();
// Create an input stream for the file
FileInputStream input = new FileInputStream("temp.dat");
// Read values from the file
int value;
while ((value = input.read()) != -1)
System.out.print(value + " ");
// Close the output stream
input.close();
}
}
(检验密码)
一些网站设定了一些制定密码的规则。编写一个方法,检验一个字符串是否合法的密码。假设密码规则如下: 密码必须至少有8个字符。 密码只能包含字母和数字。 密码必须至少有2个数字。 请编写一个程序,提示用户输入密码,如果改密码符合规则就显示“Valid password”,否则显示“Invalid password”。
public class Main {
public static void main(String[] args) {
// Prompt the user to enter a password
java.util.Scanner input = new java.util.Scanner(System.in);
//System.out.print("Enter a string for password: ");
String s = input.nextLine();
if (isValidPassword(s)) {
System.out.println("Valid password");
}
else {
System.out.println("Invalid password");
}
}
/** Check if a string is a valid password */
public static boolean isValidPassword(String s) {
// Only letters and digits?
for (int i = 0; i < s.length(); i++) {
if ((!Character.isLetter(s.charAt(i))) && !Character.isDigit(s.charAt(i)))
return false;
}
// Check length
if (s.length()< 8)
return false;
// Count the number of digits
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (Character.isDigit(s.charAt(i)))
count++;
}
if (count >= 2)
return true;
else
return false;
}
}
相同的数组。
上次练习题中函数调用错误
如果两个数组list1和list2的内容相同,那么就说它们是相同的。使用下面的程序可以判断两个数组是否相同,请填空补全程序。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int size1 = input.nextInt();
int[] list1 = new int[size1];
// Enter values for list1
for (int i = 0; i < list1.length; i++)
list1[i] = input.nextInt();
int size2 = input.nextInt();
int[] list2 = new int[size2];
// Enter values for list2
for (int i = 0; i < list2.length; i++)
list2[i] = input.nextInt();
input.close();
if (equals(list1,list2)) {
System.out.println("Two lists are identical");
} else {
System.out.println("Two lists are not identical");
}
}
public static boolean equals(int[] list1, int[] list2) {
if (list1.length!=list2.length)
return false;
Arrays.sort(list1);
Arrays.sort(list2);
for (int i = 0; i < list1.length; i++)
if (list1[i] != list2[i])
return false;
return true;
}
}
函数题和编程题(一般是Main)必须按照题目所给类名和变量名进行命名
继承和接口多态
java中控制输出浮点数的位数:
方法一:
方法二:
6-1 从抽象类shape类扩展出一个圆形类Circle
请从下列的抽象类shape类扩展出一个圆形类Circle,这个类圆形的半径radius作为私有成员,类中应包含初始化半径的构造方法。 public abstract class shape {// 抽象类 public abstract double getArea();// 求面积 public abstract double getPerimeter(); // 求周长 } 主类从键盘输入圆形的半径值,创建一个圆形对象,然后输出圆形的面积和周长。保留4位小数。
圆形类名Circle
裁判测试程序样例:
import java.util.Scanner;
import java.text.DecimalFormat;
abstract class shape {// 抽象类
/* 抽象方法 求面积 */
public abstract double getArea( );
/* 抽象方法 求周长 */
public abstract double getPerimeter( );
}
/* 你提交的代码将被嵌入到这里 */
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat d = new DecimalFormat("#.####");// 保留4位小数
double r = input.nextDouble( );
shape c = new Circle(r);
System.out.println(d.format(c.getArea()));
System.out.println(d.format(c.getPerimeter()));
input.close();
}
}
输入样例:
3.1415926
输出样例:
31.0063
19.7392
class Circle extends shape{
double r;
public Circle(double r){
this.r=r;
}
public double getArea(){
return r*r*Math.PI;
}
public double getPerimeter( ) {
return 2*Math.PI*r;
}
}
6-2 计算正五边形面积
从下列的抽象类shape类扩展出一个正五边形(regular pentagon)类RPentagon,这个类将正五边形的边长作为私有成员,类中包含初始化这个值的构造方法。
public abstract class shape {// 抽象类
public abstract double getArea();// 求面积
public abstract double getPerimeter(); // 求周长
}
正五边形类名:
RPentagon
裁判测试程序样例:
import java.util.Scanner;
import java.text.DecimalFormat;
abstract class shape {// 抽象类
/* 抽象方法 求面积 */
public abstract double getArea();
/* 抽象方法 求周长 */
public abstract double getPerimeter();
}
/* 你提交的代码将被嵌入到这里 */
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat d = new DecimalFormat("#.####");// 保留4位小数
double side = input.nextDouble();
shape rp = new RPentagon(side);
System.out.println(d.format(rp.getArea()));
System.out.println(d.format(rp.getPerimeter()));
input.close();
}
}
输入样例:
16.8
输出样例:
485.5875
84
class RPentagon extends shape{
double side;
public RPentagon(double side){
this.side=side;
}
public double getArea(){
return side*side* Math.sqrt(25+Math.sqrt(5)*10)/4.0;
}
public double getPerimeter( ) {
return 5*side;
}
}
6-3 求解正六边形的类实现
创建一个正六边形(regular hexagon)RHexagon类,实现下列接口IShape。RHexagon类将正六边形的边长作为私有成员,类中包含初始化这个值的构造方法。
interface IShape {// 接口
double getArea(); // 求面积
double getPerimeter();// 求周长
}
请编程从键盘输入正六边形的边长值,创建一个正六边形对象,然后输出正六边形的面积和周长。保留4位小数。
正六边形类名:
RHexagon
裁判测试程序样例:
import java.util.Scanner;
import java.text.DecimalFormat;
interface IShape {
double getArea();
double getPerimeter();
}
//你提交的代码将被嵌入到这里
public class Main {
public static void main(String[] args) {
DecimalFormat d = new DecimalFormat("#.####");
Scanner input = new Scanner(System.in);
double a = input.nextDouble();
IShape r = new RHexagon (a);
System.out.println(d.format(r.getArea()));
System.out.println(d.format(r.getPerimeter()));
input.close();
}
}
输入
16.8 (边长)
输出
733.281 (输出的面积)
100.8 (输出的周长)
输入样例:
5
输出样例:
64.9519
30
class RHexagon implements IShape{
private double side;
public RHexagon(double side){
this.side=side;
}
public double getArea(){
return Math.sqrt(3)*side*side*3.0/2;
}
public double getPerimeter( ) {
return 6*side;
}
}
7-1 找出最大的对象
(找出最大的对象)编写一个方法,返回对象数组中最大的对象。方法签名如下:
public static Object max(Comparable[] a)
public static Object max(Comparable[] a)
import java.util.*; public class Main
{ public static Object max(Comparable[] a)
{ /// 请填写此部分内容 }
public static void main(String[] args){
String[] sArray = new String[5];
Integer[] intArray = new Integer[5];
Scanner input = new Scanner(System.in);
for(int i=0;i<sArray.length;i++) {
sArray[i] = input.next();
}
for(int i=0;i<intArray.length;i++) {
intArray[i] = new Integer(input.nextInt());
}
System.out.println("Max string is " +(String)max(sArray));
System.out.println("Max integer is " + (Integer)max(intArray));
}
}
所有对象都是Comparable接口的实例。对象在数组中的顺序是由compareTo方法决定的。 编写测试程序,从键盘输入5个字符串和5个整数,创建一个由5个字符串构成的数组、一个由5个整数构成的数组。找出数组中最大的字符串、整数并输出。
请在这里描述输出格式。例如:对每一组输入,在一行中输出A+B的值。
输入样例:
在这里给出一组输入。例如:
Xi'an (输入5个字符串,每行一个)
Beijing
ShangHai
GuangZhou
ShenZhen
8 9 12 7 6 (输入5个整数,以空格分隔)
输出样例:
在这里给出相应的输出。例如:
Max string is Xi'an (输出最大的字符串)
Max integer is 12 (输出最大的整数)
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static Object max(Comparable[] a)
{
Arrays.sort(a);
return a[4];
}
public static void main(String[] args){
String[] sArray = new String[5];
Integer[] intArray = new Integer[5];
Scanner input = new Scanner(System.in);
for(int i=0;i<sArray.length;i++) {
sArray[i] = input.next();
}
for(int i=0;i<intArray.length;i++) {
intArray[i] = new Integer(input.nextInt());
}
System.out.println("Max string is " +(String)max(sArray));
System.out.println("Max integer is " + (Integer)max(intArray));
}
}
7-2 给定一元二次方程的系数,求一元二次方程的根
给定一元二次方程的系数,求一元二次方程的根。简便起见,不要求是复根,而且相等的根只输出一个解。注意判断浮点数为零与否用|x|<10e-6来判断。要求可以重复输入输出。要求结果保留小数点后两位数字。
输入输出形式范例:
输入样例1:
1 2 1
输出样例1:
The root is:-1.00.
输入样例2:
1 2 -1
输出样例2:
The roots are 0.41 and -2.41.
输入样例3:
1 2 2
输出样例3:
The equation has no real roots.
输入格式:
输入一组一元二次方程的系数
输出格式:
输出一元二次方程的根
输入样例:
1 2 1
输出样例1:
The root is:-1.00.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Double x1, x2;
Double a = input.nextDouble();
Double b = input.nextDouble();
Double c = input.nextDouble();
Double delta = Math.pow(b, 2) - 4 * a * c;
if (delta > 0) {
x1 = ((-1) * b + Math.sqrt(delta)) / (2 * a);
x2 = ((-1) * b - Math.sqrt(delta)) / (2 * a);
if (Math.abs(x1) < Math.pow(10, -6)) {
x1 = 0.0;
}
if (Math.abs(x2) < Math.pow(10, -6)) {
x2 = 0.0;
}
System.out.printf("The roots are %.2f and %.2f.", x1, x2);
} else if (delta == 0) {
x1 = ((-1) * b) / (2 * a);
if (Math.abs(x1) < Math.pow(10, -6)) {
x1 = 0.0;
}
System.out.printf("The root is:%.2f.", x1);
} else {
System.out.println("The equation has no real roots.");
}
input.close();
}
}
7-3 字符串替换
输入格式:
Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology. The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture. end (表示结束) Institute (第一个字符串,要求用第二个字符串替换) University (第二个字符串)
输出格式:
Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
输入样例:
Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.
The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
end
Institute
University
输出样例:
Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String temp;
while (sc.hasNextLine()) {
temp = sc.nextLine();
if (temp.equals("end"))
break;
s = s + '\n' + temp;
}
String str1 = sc.next();
String str2 = sc.next();
String newstr = s .replaceAll(str1, str2);
System.out.println(newstr);
sc.close();
}
}
7-4 直角三角形类(和函数题题型一致)
创建一个直角三角形类(regular triangle)RTriangle类,实现下列接口IShape。两条直角边长作为RTriangle类的私有成员,类中包含参数为直角边的构造方法。 interface IShape {// 接口 // 抽象方法 求面积 public abstract double getArea(); // 抽象方法 求周长 public abstract double getPerimeter(); } 请编程从键盘输入两条直角边长值,创建一个直角三角形对象,然后输出直角三角形的面积和其周长。保留4位小数。
输入格式:
输入两条直角边长值。例如:3 4。
输出格式:
在这里输出直角三角形的面积和周长。例如: 6 12
输入样例:
3.1 4.2
输出样例:
6.51
12.5202
import java.text.DecimalFormat;
import java.util.Scanner;
interface IShape {
public abstract double getArea( );
/* 抽象方法 求周长 */
public abstract double getPerimeter( );
}
class RTriangle implements IShape{
private double side1;
private double side2;
public RTriangle (double side1,double side2){
this.side1=side1;
this.side2=side2;
}
public double getArea(){
return side1*side2/2;
}
public double getPerimeter( ) {
return side1+side2+Math.sqrt(side1*side1+side2*side2);
}
}
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat d = new DecimalFormat("#.####");// 保留4位小数
double side1= input.nextDouble( );
double side2=input.nextDouble( );
IShape rt= new RTriangle(side1,side2);
System.out.println(d.format(rt.getArea()));
System.out.println(d.format(rt.getPerimeter()));
input.close();
}
}
7-5 查找成绩并折算后输出
文件:期中考试成绩.txt中有若干学生的姓名和数学期中考试成绩。 Smith 67 Anderson 75 Lewis 83 Cook 58 David 96 请你编写一个简单的查询成绩程序,当从键盘输入一个姓名时查找到他的数学期中考试分数并按照21%折算后输出。如果没找到则显示Not found. 由于目前的OJ系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的姓名和成绩,当输入的名字为noname时,表示结束。noname后面有一个名字,需要查找其成绩。
输入格式:
Smith 67 Anderson 75 Lewis 83 Cook 58 David 96 noname (表示结束) Bill
输出格式:
Not found.
输入样例:
Smith 67
Anderson 75
Lewis 83
Cook 58
David 96
noname
Lewis
输出样例:
17.43
import java.util.*;
public class Main{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
HashMap<String,Double> m=new HashMap<>();
String s;
double n;
s=sc.next();
while(s.compareTo("noname")!=0)
{
n=sc.nextDouble();
m.put(s, n);
s=sc.next();
}
s=sc.next();
if(m.get(s)!=null)
System.out.println(m.get(s).intValue()*0.21);
else
System.out.println("Not found.");
sc.close();
}
}