Java期末
大题部分
第一题:多线程
package kaoshi ;
public class SellTicket {
public static void main ( String [ ] args) {
Ticket ticket= new Ticket ( ) ;
new Thread ( ticket, "一号窗口" ) . start ( ) ;
new Thread ( ticket, "二号窗口" ) . start ( ) ;
new Thread ( ticket, "三号窗口" ) . start ( ) ;
new Thread ( ticket, "四号窗口" ) . start ( ) ;
new Thread ( ticket, "五号窗口" ) . start ( ) ;
}
}
class Ticket implements Runnable {
private int ticket= 100 ;
public void run ( ) {
this . sell ( ) ;
}
public synchronized void sell ( ) {
while ( true ) {
if ( ticket< 1 ) {
System . out. println ( "票卖完了!" ) ;
System . exit ( 0 ) ;
}
System . out. println ( Thread . currentThread ( ) . getName ( ) + "卖出第" + ( ticket-- ) + "张票" ) ;
try {
Thread . sleep ( 100 ) ;
notifyAll ( ) ;
wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
}
第二题:文件读写
package kaoshi ;
import java. io. BufferedReader ;
import java. io. BufferedWriter ;
import java. io. FileReader ;
import java. io. FileWriter ;
import java. io. IOException ;
public class FileRead {
public static void main ( String [ ] args) throws IOException {
BufferedReader read = null ;
BufferedWriter out = null ;
try {
read= new BufferedReader ( new FileReader ( "a.txt" ) ) ;
out= new BufferedWriter ( new FileWriter ( "b.txt" ) ) ;
String r;
while ( ( r= read. readLine ( ) ) != null )
{
String w= null ;
String [ ] reads = r. split ( "" ) ;
w= reads[ 0 ] + "+" + reads[ 1 ] + "=" + ( Integer . parseInt ( reads[ 0 ] ) + Integer . parseInt ( reads[ 1 ] ) ) ;
out. write ( w+ "\r\n" ) ;
}
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
read. close ( ) ;
out. close ( ) ;
}
}
}
第三题:Point和Circle类
package kaoshi ;
public class PointAndCircle {
public static void main ( String [ ] args) {
Point point= new Point ( 1 , 1 ) ;
Circle cirlce= new Circle ( 1 , 1 , 1 ) ;
System . out. println ( point. getDistance ( 2 , 2 ) ) ;
System . out. println ( cirlce. getArea ( ) ) ;
}
}
class Point {
private double x;
private double y;
public Point ( double x, double y) {
this . x= x;
this . y= y;
}
public void setX ( double x) {
this . x= x;
}
public void setY ( double y) {
this . y= y;
}
public double getX ( ) {
return this . x;
}
public double getY ( ) {
return this . y;
}
public double getDistance ( double x, double y) {
return Math . sqrt ( Math . pow ( this . x- x, 2 ) + Math . pow ( this . y- y, 2 ) ) ;
}
}
class Circle extends Point {
private double r;
public Circle ( double x, double y, double r) {
super ( x, y) ;
this . r = r;
}
public double getR ( ) {
return r;
}
public void setR ( double r) {
this . r = r;
}
public Point getPoint ( ) {
return new Point ( super . getX ( ) , super . getY ( ) ) ;
}
public double getArea ( ) {
return Math . PI* this . r* this . r;
}
}
第四题:Rect类和梯形类
class Rect {
private double len;
private double width;
public Rect ( double len, double width) {
this . len= len;
this . width= width;
}
public double getArea ( ) {
return this . len* this . width;
}
public double getLen ( ) {
return this . len* 2 + 2 * this . width;
}
}
class Tixing {
private double slen;
private double xlen;
private double high;
public Tixing ( double slen, double xlen, double high) {
this . slen = slen;
this . xlen = xlen;
this . high = high;
}
public double getArea ( ) {
return ( this . slen+ this . xlen) * this . high/ 2 ;
}
}
第五题:最大公约数最小公倍数
package kaoshi ;
import java. time. chrono. MinguoChronology ;
import java. util. Scanner ;
public class MaxAndMin {
public static int Max ( int a, int b) {
while ( b!= 0 ) {
int i= a% b;
a= b;
b= i;
}
return a;
}
public static int Min ( int a, int b) {
int i;
for ( i= a> b? a: b; i<= a* b; i++ )
{
if ( i% a== 0 && i% b== 0 ) {
break ;
}
}
return i;
}
public static void main ( String [ ] args) {
Scanner scanner= new Scanner ( System . in ) ;
int a= scanner. nextInt ( ) ;
int b= scanner. nextInt ( ) ;
System . out. println ( Min ( a, b) ) ;
System . out. println ( Max ( a, b) ) ;
}
}
第六题:手机以及固话号码正则表达式(暂定)
package kaoshi ;
import java. util. regex. Matcher ;
import java. util. regex. Pattern ;
public class PatternAndMatcher {
public static void main ( String [ ] args) {
Pattern p1= null ;
Matcher m= null ;
String phonenum= "17249231180 17369237180xxd1736-00092371801236-01202237180" ;
p1= Pattern . compile ( "([1][0-9]{10})|([0-9]{4}-[0-9]{10})" ) ;
m= p1. matcher ( phonenum) ;
while ( m. find ( ) )
{
System . out. println ( m. group ( ) ) ;
}
}
}