자바 프로 그래 밍 실 용 튜 토리 얼 (제4 판, 예 하 아) 시험 복습 문제 - 순 전 히 개인 사상
14011 단어 자바자바 초보 자의 길자바 시험 복습 자료
93、 : this super
this
1)
2)
3)
super([ ])
1)
2)
94、 :
import java.util.Scanner;
public class JudgeLeepYear {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(" :" + isLeepYear(sc.nextInt()));
}
public static boolean isLeepYear(int year) {
return year % 400 == 0 || year % 100 != 0 && year % 4 == 0;
}
}
95、 : java.lang.Math 10 0-99 , 。
public class GenerateRandom {
public static void main(String[] args) {
rand(10);
}
public static void rand(int number){
for (int i = 0; i < number; i++) {
System.out.print((int)(Math.random()*100)+" ");
}
}
}
96、 : 1+1/2!+1/3!+``````
import java.util.Scanner;
public class Factories {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int fact = 1;
double sum = 0;
for (int i = 1; i <= n; i++) {
fact *= i;
sum += 1.0 / fact;
}
System.out.println(sum);
}
}
97、 : ( :Rect), :① protected , : 、 ;② : , ;③ : area()、 perimeter()
public class Rect {
protected int width, height;
public Rect(int width, int height) {
this.width = width;
this.height = height;
}
public Rect() {
this(0, 0);
}
public int area() {
return width * height;
}
public int perimeter() {
return (width + height) * 2;
}
}
98、 : ( :Complex), :① protected , : 、 ;② : , ;③ : complexAdd(Complex a) , 、 a+bi String toString( ) ;④ main( ) : complexAdd( ) 1+2i 3+4i 4+6i, toString( ) “4+6i”, System.out.println( ) 。
public class Complex {
protected int real, imaging;
public Complex() {
this(0, 0);
}
public Complex(int real, int imaging) {
this.real = real;
this.imaging = imaging;
}
//
public void complexAdd(Complex a) {
this.real += a.real;
this.imaging += a.imaging;
}
public String toString() {
return real + "+" + imaging + "i";
}
public static void main(String[] args) {
Complex c1 = new Complex(1, 2);
Complex c2 = new Complex(3, 4);
c1.complexAdd(c2);
System.out.println(c1.toString());// 4+6i
}
}
99、 :① Frame ;② ;③ (200,200)、 (240,150);④ ;⑤ 、 ;⑥ : “Ok” , 。
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyFrame extends Frame {
private static final long serialVersionUID = 1L;
private Button btn = null;
private TextField text1 = null, text2 = null;
public MyFrame() {
super(" !");
this.setLocation(200, 200);
this.setSize(240, 150);
this.setLayout(new FlowLayout());
this.add(new Label(" "));
this.add(text1 = new TextField(20));
this.add(new Label(" "));
this.add(text2 = new TextField(20));
this.add(btn = new Button("OK"));
btn.addActionListener(new ActionListener() {
//
public void actionPerformed(ActionEvent e) {
String t1 = text1.getText();
text2.setText(t1);
}
});
this.setVisible(true);
}
public static void main(String[] args) {
new MyFrame();
}
}
100、 : TextArea x,y 。
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class TextAreaSystem extends Frame implements WindowListener,
MouseListener {
private static final long serialVersionUID = 1L;
private TextArea ta = null;
private int x = 0, y = 0;
private int click = 0, clicks = 0;// 1
public TextAreaSystem() {
this.setBounds(100, 200, 300, 300);
// new
ta = new TextArea();
//
this.add(ta);
// this.addMouseListener(this);//
ta.addMouseListener(this);
this.addWindowListener(this);
this.setVisible(true);
}
public static void main(String[] args) {
new TextAreaSystem();
}
public void windowActivated(WindowEvent arg0) {//
}
public void windowClosed(WindowEvent arg0) {//
}
public void windowClosing(WindowEvent arg0) {
System.out.println("x:" + x + " ,y:" + y);
System.exit(0);
}
public void windowDeactivated(WindowEvent arg0) {//
}
public void windowDeiconified(WindowEvent arg0) {//
}
public void windowIconified(WindowEvent arg0) {//
}
public void windowOpened(WindowEvent arg0) {//
}
public void mouseClicked(MouseEvent arg0) {//
// ,
// :TextArea Frame , this , x y 0,
x = arg0.getX();
y = arg0.getY();
if (arg0.getClickCount() > 1) {
clicks++;//
} else {
click++;//
}
//
ta.setText(" : X-" + x + " , Y-" + y);
ta.append("
:" + click);
ta.append("
:" + clicks);
}
public void mouseEntered(MouseEvent arg0) {//
}
public void mouseExited(MouseEvent arg0) {//
}
public void mousePressed(MouseEvent arg0) {//
}
public void mouseReleased(MouseEvent arg0) {
}
}
101、 : / , :① Thread ;② Runnable
public class Test_Run {
public Test_Run() {
Thread o = new Odd();
Thread e = new Thread(new Even());
o.start();
e.start();
}
public static void main(String[] args) {
new Test_Run();
}
//
private class Odd extends Thread {
public void run() {
for (int i = 100; i < 200; i+=2) {
System.out.print(i+" ");
}
System.out.println("
");
}
}
//
private class Even implements Runnable {
public void run() {
for (int i = 201; i < 300; i+=2) {
System.out.print(i+" ");
}
System.out.println("
");
}
}
}
102、 : , , 12, 12 1、2、3、4、6、12
import java.util.Scanner;
public class Divisor {// Divisor
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int factor = sc.nextInt();// factor
for (int i = 1; i <= factor; i++) {
if (factor % i == 0) {
System.out.print(i + " ");
}
}
}
}
103、 : , [1,1000] 3、5 。
public class DividedExactlyByThreeAndFive {
public static void main(String[] args) {
int count = 0;//
for (int i = 1; i <= 1000; i++) {
if (i % 3 == 0 || i % 5 == 0) {
count++;
System.out.print(i + " ");
if (count % 5 == 0) {
System.out.println();
}
}
}
}
}
104、 : , 5 , 、 。
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class UserMaxMin extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton btnVerify = null;//
// textInput ta
private JTextField textInput = null;
private JTextArea ta = null;
public UserMaxMin() {
super(" ");
this.setBounds(200, 100, 325, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setBackground(Color.cyan);
this.setLayout(null);//
addModule();
this.setVisible(true);
}
private void addModule() {
// new
JLabel helpJLabel = new JLabel(" : “,” 。");
JLabel inJLabel = new JLabel(" :");
textInput = new JTextField(20);
ta = new JTextArea();
btnVerify = new JButton(" ");
//
helpJLabel.setBounds(20, 5, 300, 40);
inJLabel.setBounds(20, 35, 100, 40);
textInput.setBounds(70, 45, 220, 20);
ta.setBounds(20, 80, 220, 100);
btnVerify.setBounds(100, 200, 60, 40);
// ta
ta.setEditable(false);
ta.setText(" \t ");
ta.append("
0\t0");
ta.append("
,
。");
// btnVerify
btnVerify.addActionListener(this);
// this
this.getContentPane().add(helpJLabel);
this.getContentPane().add(inJLabel);
this.getContentPane().add(textInput);
this.getContentPane().add(ta);
this.getContentPane().add(btnVerify);
}
public void actionPerformed(ActionEvent arg0) {
String s = textInput.getText();// bug
String[] strs = s.split(",");
if (strs.length != 5) {
JOptionPane.showConfirmDialog(this, " ");
return;
}
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i = 0; i < strs.length; i++) {
int temp = Integer.parseInt(strs[i]);
if (max < temp) {
max = temp;
}
if (min > temp) {
min = temp;
}
}
ta.setEditable(false);
ta.setText(" \t ");
ta.append("
" + max + "\t" + min);
ta.append("
,
。");
}
public static void main(String[] args) {
new UserMaxMin();
}
}
105、 : , 。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyFileChar {
public static void main(String[] args) {
// ” !!!“; 。
copyFileFromByChar("d:/ex/a/", "fileStream.txt", "d:/ex/b/");//
}
public static void copyFileFromByChar(String dirOld, String fileName,
String dirNew) {
FileReader fr = null;
FileWriter fw = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
fr = new FileReader(dirOld + fileName);
fw = new FileWriter(dirNew + fileName);
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
while(true){
String aline = null;
try {
aline = br.readLine();
bw.write(aline);
bw.newLine();
bw.flush();
} catch (Exception e) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {//
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
106、 : , “ Welcome you!”
import java.util.Scanner;
public class Welcom {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
System.out.println(s+" Welcome you!");
}
}
107、 : 10 , 。
import java.util.Scanner;
public class TenInt {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] mat = new int[10];
for (int i = 0; i < mat.length; i++) {
mat[i] = sc.nextInt();
}
for (int i = 0; i < mat.length-1; i++) {
for (int j = i+1; j < mat.length; j++) {
if(mat[i]= 1; i--) {//
for (int j = i; j <= 9; j++) {//
System.out.print(i + "*" + j + "=" + (i * j) + "\t");
}
System.out.println();
}
}
public static void lowerTriangular() {//
for (int i = 1; i <= 9; i++) {//
for (int j = 1; j <= i; j++) {//
System.out.print(j + "*" + i + "=" + (i * j) + "\t");
}
System.out.println();
}
}
public static void turnLlowerTriangular() {//
for (int i = 9; i >= 1; i--) {//
for (int j = 1; j <= i; j++) {//
System.out.print(j + "*" + i + "=" + (i * j) + "\t");
}
System.out.println();
}
}
}
110、 。
import java.util.Scanner;
public class Primer {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(isPrime(sc.nextInt()));
}
public static boolean isPrime(int prime) {
if (prime == 2 || prime == 3 || prime == 5) {
return true;
}
for (int i = 2; i * i <= prime; i++) {// 25=5*5, 5 。
if (prime % i == 0) {
return false;
}
}
return true;
}
}
111、 : , 。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile {
public static void main(String[] args) {
// ” !!!“; 。
copyFileFrom("d:/ex/a/", "text.java", "d:/ex/b/");//
}
public static void copyFileFrom(String dirOld, String fileName,
String dirNew) {
FileInputStream fin = null;
FileOutputStream fout = null;
try {
fin = new FileInputStream(dirOld + fileName);
byte[] buffer = new byte[512];
fout = new FileOutputStream(dirNew + fileName);
// , 。 ; , 。
int count = 0;
while ((count = fin.read(buffer)) > 0) {
fout.write(buffer, 0, count);
}
} catch (IOException e) {
e.printStackTrace();
} finally {//
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fout != null) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.