java的if函数格式_Java期末练习(三)

一、判断题

1-1Every element in an array has the same type. (1分)T        F

1-2接口中的方法默认是public abstract方法。 (1分)T      F

1-3java.lang包是Java的核心类库,它包含了运行Java程序必不可少的系统类。 (1分)T        F

1-4声明构造方法时,可以使用private访问修饰符。 (1分)T         F

1-5对于abstract类,不能创建该类的对象。 (1分)T        F

1-6StringBuffer类是线程安全的,StringBuilder类是线程不安全的。 (1分)T         F

1-7可以使用protected修饰符来防止方法和数据被不同包的非子类访问。 (1分)T       F

1-8把数组中元素按某种顺序排列的过程叫做查找 。 (1分)T        F

1-9修饰符protected主要是允许其他包中的子类来访问父类的特定属性。 (1分)T        F

1-10所有异常都必须捕获。 (1分)T        F

二、单选题

2-1下面说法不正确的是( ) (2分)

父类比它的子类的方法更多;

当子类对象和父类对象能接收同样的消息时,它们针对消息产生的行为可能不同;

子类在构造函数中可以使用super( )来调用父类的构造函数;

一个子类的对象可以接收父类对象能接收的消息;

2-2_ returns the selected item on a ComboBox cbo. (2分)

cbo.getValue()

cbo.getSelectedIndex()

cbo.getSelectedIndices()

cbo.getSelectedItems()

cbo.getSelectedItem()

2-3执行完以下代码int [ ] x = new int[10];后,以下哪项说明是正确的( ) (2分)

x[10]为0

x[9]未定义

x[0]为空

x[9]为0

2-4To handle the key pressed event on a pane p, register the handler with p using __. (2分)

p.setOnKeyClicked(handler);

p.setOnKeyTyped(handler);

p.setOnKeyPressed(handler);

p.setOnKeyReleased(handler);

2-5下面哪个流类属于面向字符的输入流( ) 。 (2分)

InputStreamReader

FileInputStream

ObjectInputStream

BufferedWriter

2-6To add two nodes node1 and node2 into a pane, use __.(2分)

pane.add(node1, node2);

pane.getChildren().add(node1, node2);

pane.addAll(node1, node2);

pane.getChildren().addAll(node1, node2);

2-7Fill in the code in the underlined location to display the mouse point location when the mouse is pressed in the pane. (2分)

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.layout.Pane;

import javafx.stage.Stage;

public class Test extends Application {

@Override // Override the start method in the Application class

public void start(Stage primaryStage) {

Pane pane = new Pane();

( __ )

Scene scene = new Scene(pane, 200, 250);

primaryStage.setTitle("Test"); // Set the stage title

primaryStage.setScene(scene); // Place the scene in the stage

primaryStage.show(); // Display the stage

}

/**

* The main method is only needed for the IDE with limited JavaFX

* support. Not needed for running from the command line.

*/

public static void main(String[] args) {

launch(args);

}

}

pane.setOnMouseDragged((e) -> System.out.println(e.getX() + ", " + e.getY()));

pane.setOnMouseReleased(e -> {System.out.println(e.getX() + ", " + e.getY())});

pane.setOnMousePressed(e -> System.out.println(e.getX() + ", " + e.getY()));

pane.setOnMouseClicked((e) -> System.out.println(e.getX() + ", " + e.getY()));

2-8为AB类的一个无形式参数无返回值的方法method书写方法头,使得使用类名AB作为前缀就可以调用它,该方法头的形式为( )。(2分)

final void method( )

abstract void method( )

public void method( )

static void method( )

2-9Which correctly create an array of five empty Strings? ( ) (2分)

String [] a = new String[5]; for (int i = 0; i < 5; a[i++] = null);

String [5] a;

String a [5];

String a [] = {"", "", "", "", "", ""};

2-10To add a node to the the first row and second column in a GridPane pane, use __. (2分)

pane.add(node, 0, 1);

pane.getChildren().add(node, 0, 1);

pane.add(node, 1, 2);

pane.add(node, 1, 0);

pane.getChildren().add(node, 1, 2);

2-11关于类中成员变量的作用范围,下述说法中正确的是( )。 (2分)

用static修饰的成员变量只能在用static修饰的方法中使用

类中所有成员变量在所有成员方法中有效

用private修饰的成员变量可以在main方法中直接使用

只有用public修饰的变量才能在所有方法中使用

2-12下面是People和Child类的定义和构造方法,每个构造方法都输出编号。在执行new Child("mike")的时候都有哪些构造方法被顺序调用?请选择输出结果 ( ) (2分)

class People {

String name;

public People() {

System.out.print(1);

}

public People(String name) {

System.out.print(2);

this.name = name;

}

}

class Child extends People {

People father;

public Child(String name) {

System.out.print(3);

this.name = name;

father = new People(name + ":F");

}

public Child(){

System.out.print(4);

}

}

132

432

32

312

2-13The setOnAction method is defined in _. (2分)

Node

Label

ButtonBase

Button

Labelled

2-14在面向对象的软件系统中,不同类对象之间的通信的一种构造称为_。 (2分)

消息

封装

属性

2-15下面哪个Set是按照插入顺序排序的? (2分)

HashSet

TreeSet

AbstractSet

LinkedHashSet

三、程序填空题

5-1(检验密码)一些网站设定了一些制定密码的规则。编写一个方法,检验一个字符串是否合法的密码。假设密码规则如下: 密码必须至少有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 ((3分)) {

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 ((3分)) &&

!Character.isDigit(s.charAt(i)))

return false;

}

// Check length

if ((2分) < 8)

return false;

// Count the number of digits

int count = 0;

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

if ((2分))

count++;

}

if (count >= 2)

return true;

else

return false;

}

}

isValidPassword(s)

!Character.isLetter(s.charAt(i))

s.length()

Character.isDigit(s.charAt(i))

四、函数题

6-1 定义一个股票类Stock (10 分)

定义一个名为Stock的股票类,这个类包括:一个名为symbol的字符串数据域表示股票代码。一个名为name的字符串数据域表示股票名称。一个名为previousClosingPrice的double数据域,它存储前一日的股票交易价格。一个名为currentPrice数据域,它存储当前的股票交易价格。创建一个有特定代码和名称的股票的构造方法。一个名为changePercent()方法返回从previousClosingPrice变化到currentPrice的百分比。

类名为:

Stock

裁判测试程序样例:

import java.util.Scanner;

/* 你提交的代码将被嵌入到这里 */

public class Main {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

String symbol1=input.next();

String name1=input.next();

Stock stock = new Stock(symbol1, name1);

stock.previousClosingPrice = input.nextDouble();

// Input current price

stock.currentPrice = input.nextDouble();

// Display stock info

System.out.println(stock.name+"price changed: " + stock.changePercent() * 100 + "%");

input.close();

}

}

输入样例:

002594

比亚迪

56.98

55.40

输出样例:

比亚迪股价涨跌: -2.77290277290277%

class Stock

{

String symbol;

String name;

double previousClosingPrice;

double currentPrice;

Stock(String symbol,String name)

{

this.symbol = symbol;

this.name = name;

}

double changePercent()

{

return (currentPrice - previousClosingPrice)/previousClosingPrice;

}

}

6-2 创建一个正六边形类实现接口IShape (10 分)

创建一个正六边形(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

{

double regular;

RHexagon(double regular)

{

this.regular = regular;

}

public double getArea()

{

return 3*Math.sqrt(3)*regular*regular/2;

}

public double getPerimeter()

{

return 6*regular;

}

}

五、编程题

7-1 找素数 (10 分)

请编写程序,从键盘输入两个整数m,n,找出等于或大于m的前n个素数。

输入格式:

第一个整数为m,第二个整数为n;中间使用空格隔开。例如:

103 3

输出格式:

从小到大输出找到的等于或大于m的n个素数,每个一行。例如:

103

107

109

输入样例:

9223372036854775839 2

输出样例:

9223372036854775907

9223372036854775931

import java.math.BigInteger;

import java.util.Scanner;

public class Main

{

public static void main(String[] args)

{

Scanner cin = new Scanner(System.in);

BigInteger begin = cin.nextBigInteger();

int sum = cin.nextInt();

int count = 0;

while(count

7-2 验证手机号码(C++ Java) (10 分)

某系统在新用户注册时必须输入手机号,为了提高系统效率,防止输错手机号,需要对手机号进行验证。 验证规则为: (1)长度为11位 (2)由数字0~9组成 (3)必须是1开头 以上3个条件同时满足,则验证通过,否则为不通过。

输入格式:

在一行中一个字符串,长度不超过50个字符。例如: 13802988920

输出格式:

如果验证通过则输出Yes,否则输出No。

输入样例:

13812345678

输出样例:

Yes

import java.util.Scanner;

public class Main

{

public static void main(String[] args)

{

Scanner cin = new Scanner(System.in);

String PhoneNumber = cin.nextLine();

char[] ch_str = PhoneNumber.toCharArray();

boolean flag = true;

for(int i=0;i='0'&&ch_str[i]<='9'))

{

flag = false;

break;

}

}

if( PhoneNumber.length()==11 && ch_str[0]=='1' && flag == true )

System.out.print("Yes");

else

System.out.print("No");

cin.close();

}

}

7-3 电话号码同步(Java) (10 分)

文件phonebook1.txt和phonebook2.txt中有若干联系人的姓名和电话号码。请你设计一个程序,将这两个文件中的电话号码同步。(所谓同步,就是将两个文件中的电话号码合并后剔除相同的人名和电话号码。请将同步后的电话号码按照姓名拼音顺序排序后保存到文件phonebook3.txt中。)

由于目前的OJ系统暂时不能支持用户读入文件和写文件,我们编写程序从键盘输入文件中的姓名和电话号码,当输入的单词为end时,表示文件结束。将同步后的电话号码按照姓名拼音顺序排序后输出。

输入格式:

张三 13012345678

李四 13112340000

王五 13212341111

马六 13312342222

陈七 13412343333

孙悟空 13512345555

end (表示文件phonebook1.txt结束)

张三 13012345678

孙悟空 13512345555

王五 13212341111

陈七 13412343333

唐三藏 13612346666

猪悟能 13712347777

沙悟净 13812348888

end (表示文件phonebook2.txt结束)

输出格式:

陈七 13412343333

李四 13112340000

马六 13312342222

沙悟净 13812348888

孙悟空 13512345555

唐三藏 13612346666

王五 13212341111

张三 13012345678

猪悟能 13712347777

输入样例:

Zhang3 13012345678

Li4 13112340000

Wang5 13212341111

Ma6 13312342222

Chen7 13412343333

SunWuKong 13512345555

end

Zhang3 13012345678

SunWuKong 13512345555

Wang5 13212341111

Chen7 13412343333

TangSanZang 13612346666

ZhuWuneng 13712347777

ShaWuJing 13812348888

end

输出样例:

Chen7 13412343333

Li4 13112340000

Ma6 13312342222

ShaWuJing 13812348888

SunWuKong 13512345555

TangSanZang 13612346666

Wang5 13212341111

Zhang3 13012345678

ZhuWuneng 13712347777

import java.util.*;

public class Main {

public static void main(String[] args) {

SetA = new TreeSet();

String s1;

Scanner cin = new Scanner(System.in);

for(int i=0;i<2;i++)

{

s1 = cin.nextLine();

while(!s1.equals("end"))

{

A.add(s1);

s1 = cin.nextLine();

}

}

Iteratoriterator = A.iterator();

while (iterator.hasNext()){

String str = iterator.next();

System.out.println(str);

}

cin.close();

}

}

7-4 人名地名排序 (10 分)

从键盘输入若干人名、地名或者国家名,要求按照升序排序之后输出。

输入格式:

7(表示将输入7个人名或者地名)

Zhang3

Li4

Wang5

Ma6

Chen7

Shu8

Ren9

输出格式:

Chen7

Li4

Ma6

Ren9

Shu8

Wang5

Zhang3

输入样例:

5

Xi'an

HanZhong

BaoJi

Yan'an

WeiNan

输出样例:

BaoJi

HanZhong

WeiNan

Xi'an

Yan'an

import java.util.Scanner;

public class Main

{

public static void main(String[] args)

{

Scanner cin = new Scanner(System.in);

int n = cin.nextInt();

String[] name = new String[n];

char[][] str_ch = new char[n][];

for(int i=0;istr_ch[j][k])

{

char[] temp = str_ch[i];

str_ch[i] = str_ch[j];

str_ch[j] = temp;

}

}

}

for(int i=0;i

import java.util.*;

public class Main

{

public static void main(String[] args)

{

SetB = new TreeSet();

Scanner cin = new Scanner(System.in);

int n = cin.nextInt();

for(int i=0;i<=n;i++)

{

String s2 = cin.nextLine();

B.add(s2);

}

Iteratoriterator = B.iterator();

while(iterator.hasNext())

{

String str = iterator.next();

System.out.println(str);

}

cin.close();

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值