栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

函数java答案

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

函数java答案

是否偶数
public static boolean isOdd(int data){
        if(data%2==0)
            return true;
        else
            return false;
    }
创建一个直角三角形类实现IShape接口
class RTriangle implements IShape{
    double a,b;
    
    public RTriangle(double a, double b) {
        super();
        this.a = a;
        this.b = b;
    }
    public  double getArea() {
        return 0.5*a*b;
    }
    public double getPerimeter() {
        return a+b+Math.sqrt(a*a+b*b);
    }
}

判断一个数列是否已排好序
public static boolean isSorted(int[] list)
{
    if(list[0]==1) return true;
    for (int i=1;i     {
            if (list[i]>list[i+1]) 
            {
                return false;
            }
    }
    return true;
}
编写Matrix类,使用二维数组实现矩阵,实现两个矩阵的乘法。
class Matrix
{
    int row, column; 
    int[][] matrix;
    static Scanner reader = new Scanner(System.in);
    public static Matrix inputMatrix()
    {
        int i, j;
        Matrix a = new Matrix();
        a.row = reader.nextInt();
        a.column = reader.nextInt();
        a.matrix = new int[a.row][a.column];
        for(i = 0 ; i < a.row; i ++)
        {
            for( j = 0 ; j < a.column ; j ++)
            {
                a.matrix[i][j] = reader.nextInt();
            }
        }
        return a;
    }
    public Matrix multiply(Matrix mat)   
    {
        int i, j, k;
        Matrix c = new Matrix();
        c.row = this.matrix.length;
        c.column = mat.matrix[0].length;
        c.matrix = new int[c.row][c.column];
        for ( i = 0; i < c.row; i ++ )
        {
            for ( j = 0; j < c.column; j ++ )
            {
                for( k = 0; k < mat.matrix.length; k ++)
                {
                    c.matrix[i][j] += this.matrix[i][k] * mat.matrix[k][j];
                }
            }
        }
        return c;
    }
}

jmu-Java-03面向对象基础-覆盖与equals
public boolean equals(Object obj) {
    if(this == obj)
        return true;
    if(obj == null)
        return false;
    if(!super.equals(obj))
        return false;
    DecimalFormat df = new DecimalFormat("#.##");
    Employee other = (Employee) obj;
    if(company == null&&other.company == null)
        return df.format(salary).equals(df.format(other.salary));
    else if(company == null || other.company == null)
        return false;
    return company.equals(other.company)
            && df.format(salary).equals(df.format(other.salary));
}
 图书和音像租赁
class Media {
    String name;
    double dailyRent;
    void getDailyRent() {
    }
}

class Book extends Media {
    double price;
    Book(String name,double price){
        this.name=name;
        this.price=price;
        getDailyRent();
    }
    void getDailyRent() {
        this.dailyRent=price*0.01;
    }

}

class DVD extends Media {

    DVD(String name){
        this.name=name;
        getDailyRent();
    }
    void getDailyRent() {
        dailyRent=1;
    }
}

class MediaShop {
    static double calculateRent(Media[] medias, int days) {
        int i;
        double rent=0;
        for(i=0;i         {
            rent=rent+medias[i].dailyRent;
        }
        return rent*days;
    }
}

设计门票(抽象类)
abstract class Ticket {
    int number;
    public Ticket(int number){
        this.number = number;
        }
    abstract public int getPrice();
    abstract public String toString();
}

class WalkupTicket extends Ticket {
    int price;
    public WalkupTicket(int number){
        super(number);
        this.price = 50;
        }
    public int getPrice(){
        return this.price;
        }
    public String toString(){
        return "Number:" + this.number + ",Price:" + this.price;
        }
}
class AdvanceTicket extends Ticket {
    int leadTime;
    int price;
    AdvanceTicket(int number,int leadTime){
        super(number);
        this.leadTime=leadTime;
        if(leadTime>10)
            this.price = 30;
        else
            this.price = 40;
        }
    public int getPrice(){
        return this.price;
    }
    public String toString(){
        return "Number:"+this.number+",Price:"+this.price;
    }
}
    
class StudentAdvanceTicket extends AdvanceTicket {
    int height;
    int price;
    public StudentAdvanceTicket(int number,int leadTime,int height) {
        super(number,leadTime);
        this.height=height;
        if(height>120) {
            if(leadTime>10)this.price=20;
            else           this.price=30;
        }else {
            if(leadTime>10)this.price=10;
            else           this.price=15;
        }
    }
    
    public int getPrice() {
        return this.price;
    }
    
    public String toString() {
        return "Number:"+this.number+",Price:"+this.price;
    }
}

TDVector
public TDVector()
{
    x = 0;
    y = 0;
}
public TDVector(double x, double y)
{
    this.x = x;
    this.y = y;
}
public TDVector(TDVector b)
{
    this.x = b.x;
    this.y = b.y;
}
public TDVector add(TDVector c)
{
    TDVector a = new TDVector();
    a.x = this.x + c.x;
    a.y = this.y + c.y;
    return a;
}
public void setY(double y)
{
    this.y = y;
}
public void setX(double x)
{
    this.x = x;
}

public double getX()
{
    return x;
}
public double getY()
{
    return y;
}

Book类的设计
class Book
{
    private String id;
    private int price;
    private String ren;
    private int year;
    public Book(String id,int price,String ren,int year)
    {
        this.id = id;
        this.price = price;
        this.ren = ren;
        this.year = year;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public String getRen() {
        return ren;
    }
    public void setRen(String ren) {
        this.ren = ren;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
}

 模拟题: 重写父类方法equals
    public boolean equals(Object ps)
    {
        Point p = (Point) ps;
        return (this.xPos == p.xPos && this.yPos == p.yPos);
    }

 jmu-Java-03面向对象基础-Object
java.util.List dic = new java.util.ArrayList<>(16);
        String cmd, input;
        int inInt;
        double inDouble;
        int n = sc.nextInt();
        for (int i = 0; i < n; ++i) {
            cmd = sc.next();
            switch (cmd) {
                case "c":
                    dic.add(new Computer());
                    break;
                case "s":
                    dic.add(sc.next());
                    break;
                case "d":
                    dic.add(sc.nextDouble());
                    break;
                case "i":
                    dic.add(sc.nextInt());
                    break;
                default:
                    break;
            }
        }
        for (int i = dic.size() - 1; i >= 0; --i) {
            System.out.println(dic.get(i).toString());
        }

jmu-Java-06异常-finally

            System.out.println("resource open success");
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            try {
                resource.close();
                System.out.println("resource release success");
            } catch (RuntimeException e) {
                System.out.println(e);
            }
        }


租车服务
abstract class Vehicle{
    abstract double getMoney();
}

class Truck extends Vehicle{
    double load;
    public Truck(double load) {
        this.load=load;
    }
    
    public double getMoney() {
        return 1000*this.load;
    }
    
}

class Keche extends Vehicle{
    int seat;
    public Keche(int seat) {
        this.seat=seat;
    }
    
    public double getMoney() {
        return 50*this.seat;
    }
}

class Car extends Vehicle{
    int level;
    int year;
    
    public Car(int level,int year) {
        this.level=level;
        this.year=year;
    }
    
    public double getMoney() {
        return 200*this.level/(Math.sqrt(this.year));
    }
}

class CarRentCompany{
    public static double rentVehicles(Vehicle[] vs) {
        double sum=0;
        for(int i=0;i
            sum+=vs[i].getMoney();
        }
        return sum;
    }
}

图书列表
import java.util.LinkedList;

class Book{
    String _n;
    int _p;
    String _w;
    int _no;
    
    Book(String name, int price, String author, int edition){
        this._n = name;
        this._p = price;
        this._w = author;
        this._no = edition;
    }
    
    String get_n() {return this._n;}
    String get_w() {return this._w;}
    int get_p() {return this._p;}
    int get_no() {return this._no;}
    
    
    public String toString(){
        String s = "name: "+_n+", price: "+_p+", author: "+_w+", edition: "+_no;
        System.out.println("name: "+_n+", price: "+_p+", author: "+_w+", edition: "+_no);
        return s;
    }
    public boolean equals(Book t) {
        if(this.get_n().equalsIgnoreCase(t.get_n()) == true 
        && this.get_w().equalsIgnoreCase(t.get_w()) == true
        && this.get_no()== t.get_no()) {
            return true;
        }
        return false;
    }
}

class BookList{
    LinkedList booklist;
    
    BookList(){
        this.booklist = new LinkedList<>();
    }
    
    public void addBook(Book t) {
        booklist.add(t);
    }
    
    public void searchBook(Book t) {
        boolean ok = true;
        for(int i = 0, len = booklist.size(); i < len; ++i) {
            if( t.equals(booklist.get(i)) ) {
                System.out.println("found: " + i);
                ok = false;
                break;
            }
        }
        if(ok) {
            System.out.println("not found");
        }
    }
}

jmu-Java-05集合-List中指定元素的删除
    public static List convertStringToList(String line) {
        List a=new ArrayList();
        String s[]=line.split("\s+");
        for(int i=0;i
            a.add(s[i]);
        }
        return a;
    }
    public static void remove(List list, String str) {
        for(int i=0;i
            if(list.get(i).equals(str)) {
                list.remove(i);
                i=0;
            }else {
                i++;
            }
        }
    }

jmu-Java-06异常-多种类型异常的捕获

catch(Exception e){
                if (choice.equals("number"))
                    System.out.println ("number format exception");
                if (choice.equals("illegal"))
                    System.out.println ("illegal argument exception");
                if (choice.equals("except"))
                    System.out.println ("other exception");
                System.out.println (e);
}

 可排序的学生类
class Student implements Comparable{
    int no;
    String name;
    public Student() {
        no = 0;
        name = "";
 
    }
 
    public Student(int no, String name) {
        this.no = no;
        this.name = name;
    }
 
    public int getNo() {
        return no;
    }
 
    public void setNo(int no) {
        this.no = no;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }

    public int compareTo(Student t1) {
        // TODO Auto-generated method stub
        return this.getName().compareTo(t1.getName());
    }

    public int hashCode() {
        // TODO Auto-generated method stub
        return no;
    }

    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (no != other.no)
            return false;
        return true;
    }

    public String toString()
    {
        //return "no="+this.no+"&name="+this.name;
        return "no="+this.no+"&name="+this.name;
 
    }
    
}


汽车类
class Car{
    public int speed = 0;
    public String status = "off";
    public void start() {
        status="on";
        speed=0;
    }
    public void stop() {
        if(speed==0)
            status="off";
    }
    public void speedUp() {
        if(status=="on" && speed<160)
            speed+=10;
    }
    public void slowDown() {
        if(status=="on" && speed>0)
            speed-=10;
    }
}


设计直线类
class Point
{
    public double x;
    public double y;
    public Point(double _x,double _y)
    {
        this.x=_x;
        this.y=_y;
    }
}
class Line
{
    Point p1,p2;
    public Line(Point p1,Point p2)
    {
        this.p1=p1;
        this.p2=p2;
    }
    public double getLength()
    {
        return Math.sqrt(Math.pow((p1.x-p2.x), 2)+Math.pow((p1.y-p2.y), 2));
    }
}

学生数统计
class Student {
    String name;
    int sex;
    int age;
    static int maleCount = 0;
    static int femaleCount = 0;

    public Student(String name, int sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        if (this.sex == 1) {
            maleCount++;
        } else if (this.sex == 0) {
            femaleCount++;
        }
    }

    public static int getMaleCount() {
        return maleCount;
    }
    
    public static int getFemaleCount() {
        return femaleCount;
    }

}

Person类
class Person{
    private String name;
    private String sex;
    private int age;
    public void setName(String name_) {
        name = name_;
    }
    public void setSex(String sex_) {
        sex =sex_;
    }
    public void  setAge(int age_) {
        age = age_;
    }
    public void print() {
        System.out.println("name:"+name+"; "+"sex:"+sex+"; "+"age:"+age);
    }
}

Person类2
class Person{
    private String name;
    private String sex;
    private int age;
    Person(String name,String sex,int age){
        this.name=name;
        this.sex=sex;
        this.age=age;
    }
    public void print() {
        System.out.println("name:"+name+"; sex:"+sex+"; age:"+age);
    }
}


学生类
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        final int N = 3; // 学生个数
        Scanner cin = new Scanner(System.in);
        Student[] stu = new Student[N];
        
        for(int i = 0; i < N; i ++ ) {
            stu[i] = new Student(cin.nextInt(), cin.next(), cin.nextInt());
        }
        
        Arrays.sort(stu, new Comparator( ) { // 使用 Comparator 建立以什么属性进行排序
            @Override
            public int compare(Student o1, Student o2) {
                // TODO Auto-generated method stub
                return o2.getScore() - o1.getScore();
            }
        });
        
        for(int i = 0; i < N; i ++) {
            stu[i].print();
        }
    }
}

家具类
class Furniture {
    public int height;
    public int length;
    public int width;

    public Furniture(int _length, int _width, int _height) {
        this.length = _length;
        this.width = _width;
        this.height = _height;
    }


    public int getHeight(){
        return this.height;
    }
    public int getLength(){
        return this.length;
    }
    public int getWidth(){
        return this.width;
    }
}


Shape类
abstract class Shape{
    abstract double getPerimeter();
    abstract double getArea();
}
class Square extends Shape{
    double len;
    Square(double len){
        this.len=len;
    }
    @Override
    double getPerimeter() {
        // TODO Auto-generated method stub
        return 4*len;
    }

    @Override
    double getArea() {
        // TODO Auto-generated method stub
        return len*len;
    }
    
}
class Rectangle extends Square{
    double width;
    Rectangle(double len,double width) {
        super(len);
        // TODO Auto-generated constructor stub
        this.width=width;
    }
    
    @Override
    double getPerimeter() {
        // TODO Auto-generated method stub
        return 2*(len+width);
    }

    @Override
    double getArea() {
        // TODO Auto-generated method stub
        return len*width;
    }
    
}
class Circle extends Shape{
    double r;
    double pi=3.142;
    Circle(double r){this.r=r;}
    @Override
    double getPerimeter() {
        // TODO Auto-generated method stub
        return 2*pi*r;
    }

    @Override
    double getArea() {
        // TODO Auto-generated method stub
        return pi*r*r;
    }
    
}


学生、大学生、研究生类
class Student{
    int no;
    String name,sex;
    Student(int no,String name,String sex){this.no=no;this.name=name;this.sex=sex;}
    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        this.no = no;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    
     void attendClass(String className) {}
     void print() {
         System.out.println("no: "+no+"n"+
                            "name: "+name+"n"+
                             "sex: "+sex);
     }
        
    
}
class CollegeStudent extends Student{
    String major;
    CollegeStudent(int no,String name,String sex,String major){
        super(no,name,sex);
        this.major=major;
    }
    public String getMajor() {
        return major;
    }
    public void setMajor(String major) {
        this.major = major;
    }
    
     void print() {
         super.print();
         System.out.println("major: "+major);
                            
     }
}
class GraduateStudent extends CollegeStudent{
    String supervisor;
    GraduateStudent(int no, String name, String sex, String major,String supervisor) {
        super(no, name, sex, major);
        // TODO Auto-generated constructor stub
        this.supervisor=supervisor;
    }
    public String getSupervisor() {
        return supervisor;
    }
    public void setSupervisor(String supervisor) {
        this.supervisor = supervisor;
    }
    
    void doResearch() {System.out.println(name+" is doing research");}
     void print() {
         super.print();
         System.out.println("supervisor: "+supervisor);
                            
     }
}


 租车服务
abstract class Vehicle{
    abstract double daycost();
}
class Truck extends Vehicle{
    double load;
    Truck(double load){this.load=load;}
    @Override
    double daycost() {
        return load*1000;
    }
    
}
class Keche extends Vehicle{
    int seat;
    Keche(int seat){this.seat=seat;}
    @Override
    double daycost() {
        // TODO Auto-generated method stub
        return (double)(seat*50.0);
    }
    
}
class Car extends Vehicle{
    int level;
    int year;
    Car(int level,int year){this.level=level;this.year=year;}
    @Override
    double daycost() {
        // TODO Auto-generated method stub
        return (double)(200*level)/Math.sqrt(year);
    }
    
}
class CarRentCompany{
    static double rentVehicles(Vehicle[] vs) {
        double sum=0;
        for(Vehicle a:vs) {
            sum+=a.daycost();
        }
        return sum;
    
    }
}


 可定制排序的矩形
class Rectangle implements Comparable{
    int length,width;
    Rectangle(int a,int b){
        this.length=a;
        this.width=b;
    }
    public int getArea(){
        return this.length*this.width;
    }
    @Override
    public int compareTo(Object o) {
        Rectangle a=(Rectangle) o;
        if(a.getArea()>this.getArea())
            return 1;
        if(a.getArea()             return -1;
        return 0;
    }
}

 数群

import java.util.LinkedList;
import java.util.Scanner;
interface Integer{
 public boolean contains(int a);
}
abstract class IntegerGroup implements Integer {
 public boolean contains(int a) {
  return true;
 }
}
class Range extends IntegerGroup {
 private int[] num;
 public Range(int min,int max) {
  num=new int[max-min+1];
  int t=min;
  for(int i=0;i
   num[i]=t;
   t++;
  }
 }
 public boolean contains(int a) {
  for(int i=0;i   {
   if(num[i]==a)
    return true;
  }
  return false;
 }
}
class Enum extends IntegerGroup {
 private int[] num;
 
 public Enum(int[] a) {
  num=new int[a.length];
  System.arraycopy(a, 0, num, 0, a.length);
  }
 public boolean contains(int a) {
  for(int i=0;i   {
   if(num[i]==a)
    return true;
  }
  return false;
 }
}
class MultipleGroups{
 LinkedList mu=new LinkedList();
 public void add(IntegerGroup inter)
 {
  mu.add(inter);
 }
 public boolean contains(int a) {
  for(int i=0;i
   if(mu.get(i).contains(a)==true)
    return true;
  }
  return false;
 }
}

 图书和音像租赁
import java.util.*;
public class Main {

     public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            Media[] ms  = new Media[n];
            for (int i=0; i
                String type = sc.next();
                if (type.equals("book")) {
                    ms[i] = new Book(sc.next(), sc.nextDouble());
                }else {
                    ms[i] = new DVD(sc.next());
                }
            }
            double rent = MediaShop.calculateRent(ms, sc.nextInt());
            System.out.printf("%.2f", rent);
        }
}

abstract class Media{//根据题意Media时抽象类
    abstract double getDailyRent();
}

class Book extends Media{//Book继承自Media
    String name;
    double price;
    
    public Book(String name,double price) {
        this.name=name;
        this.price=price;
    }
    
    public double getDailyRent() {
        return this.price*0.01;
    }
}

class DVD extends Media{//DVD继承自Media
    String name;
    
    public DVD(String name) {
        this.name=name;
    }
    
    public double getDailyRent() {
        return 1;
    }
}

class MediaShop{//MediaShop类
    //提供静态方法!!!
    public static double calculateRent(Media[] medias, int days) {
        double sum=0;
        for(Media m:medias) {
            if(m instanceof DVD) {
                DVD d=(DVD)m;
                sum+=d.getDailyRent()*days;
            }else if(m instanceof Book) {
                Book b=(Book)m;
                sum+=b.getDailyRent()*days;
            }
        }
        return sum;
    }
}

 动物体系

class Animal {
    public String name;
    public String color;

    public Animal(String name, String color) {
        super();
        this.name = name;
        this.color = color;
    }

    public void introduce() {

    }
}

class Cat extends Animal {

    public String eyeColor;

    public Cat(String name, String color, String eyeColor) {
        super(name, color);
        this.eyeColor = eyeColor;
    }

    public void introduce() {
        System.out.printf("My name is %s, my color is %s, my eyecolor is %sn", this.name, this.color, this.eyeColor);
    }

    public void catchMouse() {
        System.out.println("catch mouse");
    }

}

class Dog extends Animal {
    public int IQ;

    public Dog(String name, String color, int iQ) {
        super(name, color);
        IQ = iQ;
    }

    public void catchFrisbee() {
        System.out.println("catch frisbee");
    }

    public void introduce() {
        System.out.printf("My name is %s, my color is %s, my IQ is %dn", this.name, this.color, this.IQ);
    }

}

class TestAnimal {
    public static void introduce(Animal a) {
        if (a instanceof Dog) {
            Dog d = (Dog) a;
            d.introduce();
        } else if (a instanceof Cat) {
            Cat c = (Cat) a;
            c.introduce();
        }
    }

    public static void action(Animal a) {
        if (a instanceof Dog) {
            Dog d = (Dog) a;
            d.catchFrisbee();
        } else if (a instanceof Cat) {
            Cat c = (Cat) a;
            c.catchMouse();
        }
    }
}

设计一个Duck类及其子类
import java.util.*;
public class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scanner=new Scanner(System.in);
        Duck rduck = new RedheadDuck();
        rduck.display();
        rduck.quack();
        rduck.swim();
        rduck.fly();    
        Duck gduck = new MallardDuck();
        gduck.display();
        gduck.quack();
        gduck.swim();
        gduck.fly();         
    }

}

//Duck类的定义
abstract class Duck { //吧Duck写成抽象类更易实现 
    public void quack() {
        System.out.println("我会呱呱呱");
    }
    public void swim() {
        System.out.println("我会游泳");
    }
    public void fly() {
        System.out.println("我会飞");
    }
    
    abstract public void display();
}

//RedheadDuck类的定义
class RedheadDuck extends Duck {
    public void display() {
        System.out.println("我是一只红头鸭");
    }
}

//MallardDuck类的定义
class MallardDuck extends Duck {
    public void display() {
        System.out.println("我是一只绿头鸭");
    }
}

 图书类
 class Book {
    String name;
    int price;
    String author;
    int edition;

    public Book(String name, int price, String author, int edition) {
        this.name = name;
        this.price = price;
        this.author = author;
        this.edition = edition;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getEdition() {
        return edition;
    }

    public void setEdition(int edition) {
        this.edition = edition;
    }

    @Override
    public String toString() {
        return "name: " + this.name +
                ", price: " + this.price +
                ", author: " + this.author +
                ", edition: " + this.edition;
    }

    @Override
    public boolean equals(Object o) {
        //if (this == o) return true;
        if (o == null) return false;
        else {
            boolean result = false;
            if (o instanceof Book) {
                Book book = (Book) o;
                //当两个Book对象的名称(不关心大小写,无空格)、作者(不关心大小写,无空格)、版本号相同时,认为两者表示同一本书。注意此处无关价格
                if (book.name.equalsIgnoreCase(this.name)&&book.author.equalsIgnoreCase(this.author)&&book.edition==this.edition)
                    result = true;
            }
            return result;
        }
    }
}


手机类 
 class CellPhone {
    String model;
    int memory;
    int storage;
    int price;

    public CellPhone(String model, int memory, int storage, int price) {
        this.model = model;
        this.memory = memory;
        this.storage = storage;
        this.price = price;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getMemory() {
        return memory;
    }

    public void setMemory(int memory) {
        this.memory = memory;
    }

    public int getStorage() {
        return storage;
    }

    public void setStorage(int storage) {
        this.storage = storage;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String toString()
    {
        return "CellPhone [model:"+model+", memory:"+memory+", storage:"+storage+", price:"+price+"]";
    }

    public boolean equals(Object o)
    {
        boolean result=false;
        if(o==null) return false;
        else
        {
            CellPhone cell=(CellPhone) o;
            if(cell.model.equals(this.model)&&cell.memory==this.memory&&cell.storage==this.storage)
            result=true;
        }
        return result;
    }
}


可定制排序的矩形
class RectangleComparator implements Comparator{
    @Override
    public int compare(Object o1, Object o2) {
        Rectangle r1=(Rectangle)o1;
        Rectangle r2=(Rectangle)o2;
        if (r1.getArea() < r2.getArea()) return 1;
        else if (r1.getArea() > r2.getArea()) return -1;
        else return 0;
    }
}

教师类 
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        Teacher t1=new Teacher(cin.nextInt(),
                cin.next(),
                cin.nextInt(),
                cin.next());
        Teacher t2=new Teacher(cin.nextInt(),
                cin.next(),
                cin.nextInt(),
                cin.next());

        System.out.println(t1);
        System.out.println(t2);
        System.out.println(t1.equals(t2));
    }
}
class Teacher
{
    int no;
    String name;
    int age;
    String seminary;

    public Teacher(int no, String name, int age, String seminary) {
        this.no = no;
        this.name = name;
        this.age = age;
        this.seminary = seminary;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSeminary() {
        return seminary;
    }

    public void setSeminary(String seminary) {
        this.seminary = seminary;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Teacher teacher = (Teacher) o;
        boolean result=false;
        if(this.no==teacher.no&&this.age==teacher.age&&this.name.equals(teacher.name)&&this.seminary.equals(teacher.seminary))
            result=true;
        return result;
    }

    @Override
    public String toString() {
     return   "no: "+no+", name:"+name+", age: "+age+", seminary: "+seminary;
    }
}


家电类
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Truck truck=new Truck();
        truck.get();
        int sum=truck.getSum();
        System.out.println(sum);
    }
}

interface Appliance
{
    int getWeight();
}

//该货车上的所有家电,用一个集合(数组或集合类)表示
//数组的话考虑用二维数组,同时储存家电的种类和数量
//采用集合类的方法,创建一个App类

class App implements Appliance
{
    int weight;
    public App(int weight)
    {
        this.weight=weight;
    }

    @Override
    public int getWeight()
    {
        return 0;
    }
}
class TV extends App implements Appliance
{
    public TV(int w)
    {
        super(w);
    }
    @Override
    public int getWeight(){
        return weight;
    }
}

class WashMachine extends App implements Appliance
{
    public WashMachine(int w)
    {
        super(w);
    }
    @Override
    public int getWeight(){
        return weight;
    }
}

class AirConditioner extends App implements Appliance
{
    public AirConditioner(int w)
    {
        super(w);
    }
    @Override
    public int getWeight(){
        return weight;
    }
}

class Truck
{
    Scanner sc=new Scanner(System.in);
    int sum;
    int num=sc.nextInt();
    App a[]=new App[num];
    //获取所有家电
    public void get()
    {
        for (int i = 0; i < num ; i++) {
           int n=sc.nextInt();
           int w=sc.nextInt();
           switch (n){
               case 1:{
                   a[i]=new TV(w);break;
               }
               case 2: {
                   a[i]=new WashMachine(w);break;
               }
               case 3:{
                   a[i]=new AirConditioner(w);break;
               }
               default:break;
           }
        }
    }
    public int getSum()
    {
        sum=0;
        for (int i = 0; i < num; i++) {
            sum += a[i].getWeight();
        }
        return sum;
    }

}


成绩管理系统
class CourseManagementSystem{
    int A[][]=new int[100][2];//用来存放学生的成绩数据,学号就是数组下标
    int a,b,n=0;
    int q=0,w=0,e=0,r=0,t=0;//用来统计五个分数段的人数
    public void add(int no,int grade){
        if(A[no][1]!=0)//说明之前已经存入了数据
            System.out.println("the student already exists");
        else 
        { A[no][1]=grade;
          n++;
        if(grade<=59)
         q++;
        else if(grade<=69)
            w++;
        else if(grade<=79)
            e++;
        else if(grade<=89)
            r++;
        else if(grade<=100)
            t++;}
    }
    public void delete(int no){
        if(A[no][1]==0)System.out.println("no such student");//说明还没有存入数据
        else 
        {int grade1=A[no][1];
         A[no][1]=0;
         n--;
        if(grade1<=59)
         q--;
        else if(grade1<=69)
            w--;
        else if(grade1<=79)
            e--;
        else if(grade1<=89)
            r--;
        else if(grade1<=100)
            t--;}
    }
    public int query(int no){
        return A[no][1];
    }
    public void statistic( ){
        System.out.println("[0-59] : "+q+"n[60-69] : "+w+"n[70-79] : "+e+"n[80-89] : "+r+"n[90-100] : "+t);
    }
}


学生信息管理
class Student implements Comparable{
    int no;
    String name;
    Student(int a,String b){
        this.no=a;
        this.name=b;
    }
    public String toString() {
        return "no="+this.no+"&name="+this.name;
    }
    @Override
    public int compareTo(Object o) {
        // TODO Auto-generated method stub
        Student a=(Student) o;
        
        return this.name.compareTo(a.name);
    }
    public int hashCode() {
        return this.no;
    }
    public boolean equals(Object o) {
        Student s=(Student) o;
        if(this.no==s.no)
            return true;
        return false;
    }
}


 

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号