2021-01-09

jmu-Java-07多线程-交替执行 (6
import java.util.*;
class Repo {
static ArrayList a = new ArrayList();
static boolean k=false;
public Repo(String items) {
String e[] = items.split("\s+");
for (int i = 0; i < e.length; i++) {
a.add(e[i]);
}
}

public Repo() {
}

public int getSize() {
	return a.size();
}

public synchronized void getNum() {
	if(k==false) {
		try {
			this.wait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}else {
		System.out.println(Thread.currentThread().getName()+" finish "+a.get(0));
		a.remove(0);
		k=false;
		this.notify();
	}
}
public synchronized void getNum2() {
	if(k==true) {
		try {
			this.wait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}else {
		System.out.println(Thread.currentThread().getName()+" finish "+a.get(0));
		a.remove(0);
		k=true;
		this.notify();
	}
}

}
class Worker1 implements Runnable {
Repo repo = new Repo();

public Worker1(Repo repo) {
	super();
	this.repo = repo;
}

public void run() {
	while(repo.a.size()>0) {
		repo.getNum2();
	}
	
}

}

class Worker2 implements Runnable {
Repo repo = new Repo();
public Worker2(Repo repo) {
super();
this.repo = repo;
}
public void run() {
while(repo.a.size()>0) {
repo.getNum();
}
}
}
jmu-Java-05集合-List中指定元素的删除 (6
public static List convertStringToList(String line) {
String s[]=line.split(" ");
List list=new ArrayList();
for(int i=0;i<s.length;i++) {
if(s[i].length()>=1)list.add(s[i]);
}
return list;
}
/remove函数代码/
public static void remove(List list, String str) {
for(int i=0;i<list.size();i++) {
if(list.get(i).equals(str)) {
list.remove(i);
i–;
}
}
}
3jmu-Java-07多线程-同步访问 
public void deposit(int money) {
synchronized(this) {
this.balance+=money;
}

}
public  void withdraw(int money) {    
    synchronized(this) {
        if(this.balance>=money) {
            this.balance -= money;
        }
    } 

设计一个长方体类Cuboid 
class Cuboid{
public double length;
public double width;
public double height;
Cuboid(){
length = 1;
width = 1;
height = 1;
}
Cuboid(double a,double b,double c){
length = a;
width = b;
height =c;
}
public double getArea() {
return 2*(lengthwidth + widthheight + lengthheight); }
public double getVolume() {
return length
widthheight;
}
}
写出派生类构造方法(Java) 
super(id, name);
this.sid = sid;
this.score = score;
6从抽象类shape类扩展出一个圆形类Circle 
class Circle extends shape{
private double radius;
public Circle(double radius){
this.radius=radius;
}
public double getArea( ){
return Math.PI
radiusradius;
}
public double getPerimeter( ){
return Math.PI
radius*2;
}
}
7异常:物品安全检查 
class DangerException extends Exception{
String message;
DangerException(){
super(“属于危险品!”);
}
}

class Goods{
private String name;
private boolean isDanger;
void setDanger(boolean isDanger) {
this.isDanger=isDanger;
}
void setName(String name) {
this.name=name;
}
boolean getDanger() {
return isDanger;
}
String getName() {
return name;
}
}

class Machine{
void checkBag(Goods goods) throws DangerException {
if(goods.getDanger()==true) {
throw new DangerException();
}
}
}
8sdust-Java-可实现多种排序的Book类 
class Book{
private String name;
private Date publishDate;
private double price;
public Book(String name, Date publishDate, double price) {
this.name = name;
this.publishDate = publishDate;
this.price = price;
}
public String getName() {
return name;
}
public Date getPublishDate() {
return publishDate;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return “书名:” + name + “,定价:” + price;
}

}

class BookComparatorByPubDate implements Comparator{
@Override
public int compare(Object o1, Object o2) {
Book b1 = (Book) o1;
Book b2 = (Book) o2;
return b2.getPublishDate().compareTo(b1.getPublishDate());
}

}

class BookComparatorByPrice implements Comparator{

@Override
public int compare(Object o1, Object o2) {
	Book b1 = (Book) o1;
	Book b2 = (Book) o2;
	if(b1.getPrice() == b2.getPrice()) {
		return b1.getName().compareTo(b2.getName());
	}
	else {
		return b1.getPrice() < b2.getPrice() ? -1 : 1;
	}
}

}
9使用继承,实现“剪刀石头布的游戏” 
class ComputerPlayer extends Player{
ComputerPlayer(String name){
super(name);
}
}
class PersonPlayer extends Player{
PersonPlayer(String name){
super(name);
}
int choice() {
Scanner scan=new Scanner(System.in);
int c=scan.nextInt();
return c;
}
}
class Game{
ComputerPlayer cp;
PersonPlayer pp;
public Game(ComputerPlayer cp,PersonPlayer pp) {
this.cp=cp;
this.pp=pp;
}
void start() {
int c=cp.show(),p=pp.choice();
if(cp) {
System.out.println(“A Draw.”);
}
else if(c
1&&p3) {
System.out.println(“Winner is computerPlayer.”);
}
else if(p
1&&c==1) {
System.out.println(“Winner is personPlayer.”);
}
else if(p<c) {
System.out.println(“Winner is computerPlayer.”);
}
else {
System.out.println(“Winner is personPlayer.”);
}
}
}
10普通账户和支票账户 
class Account{
int id;
int balance;
public Account() {
id=0;
balance=0;
}
public Account(int id,int balance) {
this.id=id;
this.balance=balance;
}
public void setBalance(int balance) {
this.balance=balance;
}
public int getBalance() {
return balance;
}
public boolean withdraw(int money) {
if(this.balance<money)return false;
else {
this.balance-=money;
return true;
}
}
public void deposit(int money) {
balance=this.balance+money;
}
public String toString() {
return "The balance of account “+id+” is "+balance;
}
}
class CheckingAccount extends Account{
private int overdraft;
public CheckingAccount() {
super(0,0);
overdraft=0;
}
public CheckingAccount(int id,int balance,int overdraft) {
super(id,balance);
this.overdraft=overdraft;
}
public boolean withdraw(int money) {
if(balance-money>overdraft||balance-money<-overdraft)return false;
else {
balance-=money;
return true;
}
}
}
11人口统计 
public static int numofHan(String data[]){
String s = “汉族”;
int num = 0;
for(String s_t: data){
if( s_t.indexOf(s) >= 0 ){
num ++;
}
}
return num;
}
教师、学生排序 
class Student extends Person{
private int sno;
private String major;

public Student(int sno, String name, String gender, int age, String major) {
    super(name, gender, age);
    this.sno = sno;
    this.major = major;
}

public int getSno() {
    return sno;
}

public void setSno(int sno) {
    this.sno = sno;
}

public String getMajor() {
    return major;
}

public void setMajor(String major) {
    this.major = major;
}

@Override
public int compareTo(Object o) {
    Student temp = (Student)o;
    if (this.sno < temp.sno){
        return 1;
    }
    if (this.sno == temp.sno){
        return 0;
    }
    return -1;
}

}

class Teacher extends Person{
private int tno;
private String subject;

public Teacher(int tno, String name, String gender, int age, String subject) {
    super(name, gender, age);
    this.tno = tno;
    this.subject = subject;
}

public int getTno() {
    return tno;
}

public void setTno(int tno) {
    this.tno = tno;
}

public String getSubject() {
    return subject;
}

public void setSubject(String subject) {
    this.subject = subject;
}

@Override
public int compareTo(Object o) {
    Teacher temp = (Teacher)o;
    if (this.getAge() > temp.getAge()){
        return 1;
    }
    if (this.getAge() == temp.getAge()){
        return 0;
    }
    return -1;
}

}

class MyTool{
public static void separateStu_T(List persons,List teachers,List students){
for (int i = 0; i < persons.size(); i++){
if (persons.get(i) instanceof Student){
students.add(persons.get(i));
}
if (persons.get(i) instanceof Teacher){
teachers.add(persons.get(i));
}
}
}
}

矩形 
class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x=x;
this.y=y;
}
@Override
public String toString() {
return"("+x+","+y+")";
}
public void move(int dx, int dy) {
x=x+dx;
y=y+dy;
}
public double distance(Point p) {
int xx=(this.x-p.x)(this.x-p.x);
int yy=(this.y-p.y)
(this.y-p.y);
double sum=xx+yy;
return Math.sqrt(sum);
}
}
class Dimension {
private int width;
private int height;
public Dimension(int width, int height) {
this.height=height;
this.width=width;
}
@Override
public String toString() {
return width+" by “+height;
}
public void resize(double widthScale, double heightScale) {
width*=widthScale;
height*=heightScale;
}
public int area() {
return width*height;
}
}
class Rectangle {
private Point topleft;
private Dimension size;
public Rectangle(Point topleft, Dimension size) {
this.topleft=topleft;
this.size=size;
}
public String toString() {
return"Rectangle at “+topleft.toString()+”:”+size.toString();
}
public void move(int dx, int dy) {
topleft.move(dx, dy);
}
public void resize(double widthScale, double heightScale) {
size.resize(widthScale, heightScale);
}
public double area() {
return size.area();
}
public double distance(Rectangle r) {
return this.topleft.distance(r.topleft);
}
}
TDVector 
TDVector(){
this.x=0;
this.y=0;
}
TDVector(double x,double y){
this.x=x;
this.y=y;
}
TDVector(TDVector t){
this.x=t.x;
this.y=t.y;
}
void setX(double x) {
this.x=x;
}
void setY(double y) {
this.y=y;
}
double getX() {
return x;
}
double getY() {
return y;
}
TDVector add(TDVector t) {
TDVector tt=new TDVector();
tt.x=this.x+t.x;
tt.y=this.y+t.y;
return tt;
}

顺序表-插入元素函数练习 
public void insert(int pos,E e) {
this.element[length++]=1;
this.element[pos]=e;
}

设计Worker类及其子类 
class Worker {
private String Name;
private double Rate;
public Worker(String _Name,double _Rate){
Name = _Name;
Rate = _Rate;
}
void setName(String _name) {
Name = _name;
}
double getRate(){
return Rate;
}
void setRate(double _Rate){
Rate = _Rate;
}
void pay(int time){
System.out.println(“Not Implemented”);
}
}

class HourlyWorker extends Worker {
public HourlyWorker(String string, double d) {
super(string ,d);
}

void pay(int time){
	if(time > 40)
		System.out.println(this.getRate()*time*2);
	else
		System.out.println(this.getRate()*time);
}

}
class SalariedWorker extends Worker {
public SalariedWorker(String string, double d) {
super(string ,d);
}
void pay(){
System.out.println(this.getRate()*40);
}
void pay(int time){
System.out.println(this.getRate()*40);
}
}

二维向量定义及相加(Java) 
TDVector(){
this.x=0;
this.y=0;
}
TDVector(double x,double y){
this.x=x;
this.y=y;
}
TDVector(TDVector t){
this.x=t.x;
this.y=t.y;
}
void setX(double x) {
this.x=x;
}
void setY(double y) {
this.y=y;
}
double getX() {
return x;
}
double getY() {
return y;
}
TDVector add(TDVector t) {
TDVector tt=new TDVector();
tt.x=this.x+t.x;
tt.y=this.y+t.y;
return tt;
}
倒序存放20以内的奇数数组并输出。 
public class Main{
public static void main(String []args) {
int []a= {1,3,5,7,9,11,13,15,17,19};
System.out.println(“数组的初始条件为:”);
show(a);
System.out.println();
System.out.println(“数组逆序存放后的状态为:”);
reverse(a);
}
public static void reverse(int []a) {
int []b=new int[a.length];
for(int i=0;i<a.length;i++) {
b[i]=a[a.length-1-i];
}
show(b);
}
public static void show(int []b) {
System.out.print(" “);
for(int i=0;i<b.length;i++) {
if(i==b.length-1) {
System.out.print(b[i]);
}else {
System.out.print(b[i]+” ");
}
}
}
}

单词替换 
import java.util.Scanner;

public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String oldSub = sc.nextLine();
String newSub = sc.nextLine();
sc.close();

	String[] strs = str.split(" ");
	for(int i=0;i<strs.length;i++) {
		if(strs[i].equals(oldSub)) {
			strs[i] = newSub;
		}
	}
	{
	String result = new String();
	for(int i=0;i<strs.length;i++) {
		result += strs[i]+" ";
	}
	
	System.out.println(result.trim());//删除尾部空白符
	}

}
}

接口–四则计算器 
import java.util.Scanner;

public class Main{

public static void main(String[] args) {
	Scanner in=new Scanner(System.in);
	int n=in.nextInt();
	int m=in.nextInt();
	Add l=new Add();
	Sub j=new Sub();
	System.out.println(l.computer(n, m));
	System.out.println(j.computer(n, m));
	
	in.close();

}

}
interface IComputer{
abstract public int computer(int n ,int m);
}
class Add implements IComputer
{
int m,n;
public int computer(int n ,int m) {
return n+m;
}
}
class Sub implements IComputer
{
int m,n;
public int computer(int n ,int m) {
return n-m;
}
}
5圆柱体类设计 
import java.util.Scanner;

public class Main{

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner in=new Scanner(System.in);
	int r=in.nextInt();
	int h=in.nextInt();
	Cylinder c1=new Cylinder(r,h);
	System.out.println(c1.getVolumn());
	Cylinder c2=new Cylinder();
	System.out.println(c2.getVolumn());

}

}
class Cylinder{
private int radius,height ;
public Cylinder(int radius,int height){
this.height=height;
this.radius=radius;
System.out.println(“Constructor with para”);
}
public Cylinder() {
this.radius=2;
this.height=1;
System.out.println(“Constructor with para”);
System.out.println(“Constructor no para”);
}

public int getVolumn(){
	return (int) (Math.PI*radius*radius*height);
			
} 


public int getRadius() {
	return radius;
}

public void setRadius(int radius) {
	this.radius = radius;
}
public int getHeight() {
	return height;
}

public void setHeight(int height) {
	this.height = height;
}

}
6图书价格汇总 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String[] s = str.split(" “);//分割字符串
ArrayList list = new ArrayList();
for (int i = 0; i < s.length; i++){
list.add(Integer.parseInt(s[i]));//转成Integer存入ArrayList集合
}
//调用Collections类的排序函数
Collections.sort(list);
//删除最大最小值
list.remove(0);
list.remove(list.size() - 1);
for (int i = 0; i < list.size(); i++){
if (i == 0){
System.out.print(list.get(i));
}else{
System.out.print(” " + list.get(i));
}
}
}
}
7图书价格汇总 
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int total = 0;
String str;
str = sc.nextLine();//将输入转换为字符串类型

	boolean flag = false;//true:zhange zai chu li
	int linshi = 0;
	
	for(int i=0;i<str.length();++i){
		if(str.charAt(i) == ';'){//字符串中下标为i的字符
			System.out.println();
		}
		else{
			System.out.print(str.charAt(i));
		}
		if(str.charAt(i) >= '0' && str.charAt(i) <= '9'){
			linshi = linshi * 10 + str.charAt(i) - '0';
			flag = true;
		}
		else if(i + 1<str.length()){
			total += linshi;
			linshi = 0;
		}
		else{
			total += linshi;
			linshi = 0;
			
		}
	}
	total += linshi;
	System.out.println();
	System.out.println("总价格为"+total);
	
	sc.close();
}

}

class Book{
public String name;
public int p;
}
8创建一个倒数计数线程 
import java.util.Scanner;
public class Main {

public static void main(String[] args) {
    Test t=new Test();
    Thread th=new Thread(t);
    th.start();
}

}
class Test implements Runnable {
public void run() {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=n;i>=0;i–){
System.out.println(i);
try
{
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}

}
9设计圆和圆柱体 
import java.util.*;
class Circle{
private double r;
Circle(double radius)
{
this.r=radius;
}
Circle(){
this.r=0;
}
void setRadius(double r)
{
this.r=r;
}
double getRadius()
{
return r;
}
double getArea()
{
return (Math.PI)rr;
}
double getPerimeter()
{
return (Math.PI)r2;
}
public String toString()
{
return “Circle(r:”+r+")";
}
}
class Cylinder{
private double h;
private Circle c;
Cylinder(double height,Circle circle)
{
this.h=height;
this.c=circle;
}
Cylinder(){
this.h=0.0;
this.c= new Circle();
}
void setHeight(double height)
{
this.h=height;
}
double getHeight()
{
return h;
}
void setCircle(Circle circle) {
this.c=circle;
}
Circle getCircle()
{
return c;
}
double getArea() {
double s1=c.getPerimeter()*h;
double s2=c.getArea()*2;
return s1+s2;
}
double getVolume()
{
double v;
v=c.getArea()h;
return v;
}
public String toString()
{
return “Cylinder(h:”+h+",Circle(r:"+c.getRadius()+"))";
}
}
public class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for(int i = 0; i < n; i++) {
String str = input.next();
if(str.equals(“Circle”)) {
Circle c = new Circle(input.nextDouble());
System.out.println(“The area of " + c.toString() + " is " + String.format(”%.2f",c.getArea()));
System.out.println(“The perimeterof " + c.toString() + " is “+ String.format(”%.2f”,c.getPerimeter()));
} else if(str.equals(“Cylinder”)) {
Cylinder r = new Cylinder(input.nextDouble(), new Circle(input.nextDouble()));
System.out.println(“The area of " + r.toString() + " is " + String.format(”%.2f",r.getArea()));
System.out.println(“The volume of " + r.toString() + " is " + String.format(”%.2f",r.getVolume()));
}
}
}
}
10设计一个Tiangle异常类 
import java.util.
;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double s1 = input.nextDouble();
double s2 = input.nextDouble();
double s3 = input.nextDouble();
try {
Triangle t = new Triangle(s1,s2,s3);
System.out.println(t);
}catch (IllegalTriangleException ex) {
System.out.println(ex.getMessage());
}
}
}
class IllegalTriangleException extends Exception{
private double side1;
private double side2;
private double side3;

	public IllegalTriangleException(double side1,double side2 ,double side3) {
		super("Invalid: "+side1+","+side2+","+side3);//message是父类成员变量,使用super向上传参!
		this.side1=side1;
		this.side2=side2;
		this.side3=side3;
	}

}
class Triangle{

    private double side1;
	private double side2;
	private double side3;
	
	public Triangle(double side1,double side2,double side3) 
	  throws IllegalTriangleException{
			if((side1+side2)>side3&(side1+side3)>side2&(side2+side3)>side1) 
				{this.side1=side1;this.side2=side2;this.side3=side3;}
			else
				throw new  IllegalTriangleException(side1,side2,side3);
	}
    @Override
    public String toString(){
        return "Triangle ["+"side1="+this.side1+", side2="+this.side2+", side3="+this.side3+"]";
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值