分苹果的java代码 java苹果好吃

求Java代码

package com.zhuq;

澄城ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

public class Apple extends Fruit{

@Override

public void eat() {

System.out.println("吃苹果");

}

@Override

public void shape() {

System.out.println("苹果形状");

}

}

class Banana extends Fruit{

@Override

public void eat() {

System.out.println("吃香蕉");

}

@Override

public void shape() {

System.out.println("香蕉形状");

}

}

class Orange extends Fruit{

@Override

public void eat() {

System.out.println("吃橘子");

}

@Override

public void shape() {

System.out.println("橘子形状");

}

}

abstract class Fruit{

abstract void eat();

abstract void shape();

public void String(){

this.eat();

this.shape();

}

}

/**  */

public class Game {

public static Fruit 中奖(){

Integer i =(int) (Math.random()*3);

Fruit f=null;

switch (i){

case 0: f=new Apple();break;

case 1: f=new Banana();break;

case 2:f=new Orange();break;

}

return f;

}

}

/**  */

public class Zhuq{

public static void main(String[] args) {

Fruit[] fruits = {null,null,null,null,null,null,null,null,null,null} ;

for(int m=0;m10;m++){

fruits[m] =Game.中奖();

}

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

fruits[i].String();

}

}

}

java程序题:定义一个抽象类-水果,其中包括getWeight()方法,编写程序分别创建苹果、

水果类

abstract public class Fruit {

abstract public double getWeight();

}

苹果类

public class Apple extends Fruit {

private double weight;

public Apple(double weight) {

this.weight = weight;

}

@Override

public double getWeight() {

return weight;

}

}

橘子类

public class Orange extends Fruit {

private double weight;

public Orange(double weight) {

this.weight = weight;

}

@Override

public double getWeight() {

return weight;

}

}

桃子类

public class Peach extends Fruit {

private double weight;

public Peach(double weight) {

this.weight = weight;

}

@Override

public double getWeight() {

return weight;

}

}

主类

public class Main {

public static void main(String[] args) {

// TODO Auto-generated method stub

Fruit[] fruits = { new Peach(12), new Apple(2), new Orange(5) };

for (Fruit fruit : fruits) {

System.out.println(fruit.getClass().getName() + "的重量是"

+ fruit.getWeight());

}

}

}

运行结果

Peach的重量是 12.0

Apple的重量是 2.0

Orange的重量是 5.0

5种水果,用java来按照字典顺序排列输出

public class Fruit {

String[] fruits=new String[5];

public void inputfruit(){

Scanner input=new Scanner(System.in);

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

System.out.print("请输入第"+(i+1)+"种水果:");

fruits[i]=input.next();

}

}

public String[] getNames(){

Arrays.sort(fruits);

return fruits;

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

Fruit sg=new Fruit();

String[] newFruits=new String[5];

sg.inputfruit();

newFruits=sg.getNames();

System.out.println("这些水果在字典种出现的顺序是:");

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

if(newFruits[i]!=null){

System.out.print(newFruits[i]+"\t");

}

}

}

java算法,例子N个苹果放到20个袋子中,操作时间为4个小时,其中每个袋子的各苹果放入时间间隙大于30分钟

需要使用多线程

package org.xuyh.design;

import java.util.Scanner;

/**

* N个苹果放到20个袋子中,操作时间为4个小时,其中每个袋子的各苹果放入时间间隙大于30分钟

* @author XuYanhang

*

*/

public class AppleOperation implements Runnable{

public static final int ONE_SECOND = 1000;

private final int[] packages;

public final int operationTime;

public final int minSpacetTime;

private int appleCount;

private boolean timeEnd = false;

public AppleOperation(int appleCount,int packCount,int operationTime,int minSpacetTime){

this.appleCount = appleCount;

packages = new int[packCount];

this.operationTime = operationTime;

this.minSpacetTime = minSpacetTime;

}

public int getAppleCount(){

return appleCount;

}

public int getPackCount(){

return packages.length;

}

public int getOperationTime(){

return operationTime;

}

public boolean hasEnd(){

return timeEnd || appleCount == 0;

}

public synchronized void setTimeEnd(){

timeEnd = true;

}

public void run() {

int pack = Integer.parseInt(Thread.currentThread().getName());

while(appleCount0){

synchronized(this){

if(hasEnd()){

break;

}

appleCount--;

packages[pack]++;

System.out.println("一个苹果放入了第"+(pack+1)+"个袋子中,还剩"+appleCount+"个苹果");

}

for(int i = 0 ; i minSpacetTime ; i++){

synchronized(this){

if(hasEnd()){

break;

}

}

try {

Thread.sleep(ONE_SECOND);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

synchronized(this){

System.out.println("第"+(pack+1)+"个袋子最终有"+packages[pack]+"个苹果");

}

}

//Test main method

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int appleCount = -1;

do{

System.out.print("N个苹果放到20个袋子中,操作时间为4个小时,其中每个袋子的各苹果放入时间间隙大于30分钟。\n"

+ "输入苹果总数N:");

String string = scanner.next();

if(null != string string.matches("^\\d{1,8}$")){

appleCount = Integer.parseInt(string);

}

}while(appleCount == -1);

scanner.close();

AppleOperation operation = new AppleOperation(appleCount,20,4*60*60,30*60);

new WaitThread(operation).start();

for (int i = 0; i operation.getPackCount(); i++) {

Thread thread = new Thread(operation,""+i);

thread.start();

}

}

static class WaitThread extends Thread{

private AppleOperation operation;

public WaitThread(AppleOperation operation){

this.operation = operation;

}

public void run(){

try {

Thread.sleep(operation.getOperationTime()*ONE_SECOND);

} catch (InterruptedException e) {

e.printStackTrace();

}

operation.setTimeEnd();

System.out.println("时间到,苹果装袋操作结束,最后为装袋的苹果个数:"+operation.getAppleCount());

}

}

}

这里我们额外启动了21个线程,其中一个是计4小时的定时的,其他20个是向每个袋子中放苹果的线程。


分享名称:分苹果的java代码 java苹果好吃
网页网址:http://cdiso.cn/article/dddepdi.html

其他资讯