package animal;
interface Animal {
void cry();
void getAnimalName();
}
class Simulator {
void playSound(Animal animal) {
animal.cry();
animal.getAnimalName();
}
}
class Dog implements Animal {
String name;
Dog(String s) {
name = s;
}
public void cry() {
System.out.println("汪 汪 汪 ~");
}
@Override
public void getAnimalName() {
System.out.println(name);
}
}
class Cat implements Animal {
String name;
Cat(String s) {
name = s;
}
public void cry() {
System.out.println("喵 喵 喵 ~");
}
@Override
public void getAnimalName() {
System.out.println(name);
}
}
public class Application {
public static void main(String args[]) {
Simulator simulator = new Simulator();
simulator.playSound(new Dog("藏獒"));
simulator.playSound(new Cat("加菲猫"));
}
}
package newyear;
class MobileShop {
InnerPurchaseMoney purchaseMoney1 =new InnerPurchaseMoney(20000);
InnerPurchaseMoney purchaseMoney2 =new InnerPurchaseMoney(10000);
private int mobileAmount;
MobileShop(){
purchaseMoney1.moneyValue = 20000;
purchaseMoney2.moneyValue = 10000;
}
void setMobileAmount(int m) {
mobileAmount = m;
}
int getMobileAmount() {
return mobileAmount;
}
class InnerPurchaseMoney {
int moneyValue;
InnerPurchaseMoney(int m) {
moneyValue = m;
}
void buyMobile() {
if (moneyValue >= 20000) {
mobileAmount = mobileAmount - 6;
System.out.println("用价值" + moneyValue + "的内部购物券买了6部手机");
} else if (moneyValue < 20000 && moneyValue >= 10000) {
mobileAmount = mobileAmount - 3;
System.out.println("用价值" + moneyValue + "的内部购物券买了3部手机");
}
}
}
}
public class NewYear{
public static void main(String args[]){
MobileShop shop=new MobileShop();
shop.setMobileAmount(30);
System.out.println("手机专卖店目前有"+shop.getMobileAmount()+"部手机");
shop.purchaseMoney1.buyMobile();
shop.purchaseMoney2.buyMobile();
System.out.println("手机专卖店目前有"+shop.getMobileAmount()+"部手机");
}
}
package scorre;
import java.util.Scanner;
interface ComputerAverage{
void average(int n ,float []s);
}
class Gymnastics implements ComputerAverage{
public void average(int n ,float []s) {
float max = -1000f;
float min = 1000f;
float sum = 0;
for(int i =1;i<=n;i++)
{
if(s[i]>max)max = s[i];
if(s[i]<min)min = s[i];
sum+=s[i];
}
float ans = 0;
ans =(sum-max-min)/(n-2);
System.out.println("体操比赛计算选手成绩为:"+ans);
}
}
class School implements ComputerAverage{
public void average(int n, float[] s) {
float sum = 0;
for(int i =1;i<=n;i++)
{
sum+=s[i];
}
sum = sum/n;
System.out.println("学校考察班级的考试情况成绩为:"+sum);
}
}
public class Application {
public static void main(String args[])
{
Scanner reader = new Scanner(System.in);
float studentsscorre[];
studentsscorre = new float [1000];
System.out.println("请输入学生的人数:");
int peoplenum = reader.nextInt();
System.out.println("请输入学生的分数:");
for(int i = 1;i<= peoplenum;i++) {
float n = reader.nextFloat();
studentsscorre[i] = n;
}
ComputerAverage class1 ;
class1 = new Gymnastics();
class1.average(peoplenum,studentsscorre);
ComputerAverage class2 ;
class2 = new School();
class2.average(peoplenum, studentsscorre);
}
}
package station;
public class Goods{
boolean isDanger;
String name;
void setIsDanger(boolean a)
{
isDanger = a;
}
boolean isDanger() {
if(isDanger==false)return false ;
else return true;
}
void setName(String s)
{
name =s;
}
String getName(String s)
{
return s;
}
}
public class Machine{
Goods good;
public void checkBag(Goods goods) throws DangerException{
if(goods.isDanger() ==true ) {
throw new DangerException();
}
}
}
public class DangerException extends Exception{
String message;
public DangerException() {
System.out.println("发生异常!");
}
void toShow() {
System.out.println("DANGER!");
}
}
public class station {
public static void main(String args[]) {
Goods G = new Goods();
G.setName("这是个危险品!");
G.isDanger=true ;
Machine M1 = new Machine();
try {
M1.checkBag(G);
}
catch(DangerException e) {
e.toShow();
System.out.println("发生异常!"+e.message);
}
}
}