Java language Programming Basic article 13th Chapter programming exercises __ Programming

Source: Internet
Author: User
1 2 3 4/* I see a sample code in the Java core technology that fits the problem, but I want to spit it in the good trouble, full of methods of call, feel better with C write to concise it? * * * * 5 GeometricObject.java:package yo

Ngheng;
    Public abstract class Geometricobject implements comparable<geometricobject>{private String color = "white";
    private Boolean filled;

    Private Java.util.Date datecreated;
            public static Geometricobject Max (Geometricobject o1,geometricobject O2) {if (O1.getarea () > O2.getarea ())
        return O1;
    else return O2;
    } protected Geometricobject () {datecreated = new java.util.Date ();
        } protected Geometricobject (String color, Boolean filled) {datecreated = new java.util.Date ();
        This.color = color;
    this.filled = filled;
    Public String GetColor () {return color;
    public void SetColor (String color) {this.color = color;
    public Boolean isfilled () {return filled;
 } public void Setfilled (Boolean filled) {       this.filled = filled;
    Public Java.util.Date getdatecreated () {return datecreated; @Override public String toString () {return ' created on ' + datecreated + \ncolor: ' + color + ' \nfille
    D: "+ filled;

    public abstract double Getarea ();

    public abstract double getprimeter (); 
        @Override public int compareTo (Geometricobject go) {if (This.getarea () > Go.getarea ()) return 1;
        else if (This.getarea () < Go.getarea ()) return-1;
    else return 0;

}} Rectangle.java:package Yongheng;
    public class Rectangle extends geometricobject{private double width;

    private double height;
        Public Rectangle () {super ();
        width = 0;
    Height = 0;
        Public Rectangle (double width, double height) {super ();
        This.width = width;
    This.height = height; Public Rectangle (double width, double height, String cOlor, Boolean filled) {super (color,filled);
        This.width = width;
    This.height = height;
    @Override public Double Getarea () {return width*height;
    @Override public Double Getprimeter () {return (width+height) *2;

}} Main.java:package Yongheng;
Import Java.util.Scanner;
Import java.util.ArrayList;
Import Java.util.Arrays;

Import java.util.Collections; public class Main {public static void main (string[] args) {arraylist<rectangle> list = new arraylist&
        Lt;> ();
        List.add (New Rectangle (1,1));
        List.add (new Rectangle (3,4));
        List.add (New Rectangle (9,10));
        List.add (new Rectangle (3,5));
        List.add (New Rectangle (4,2));
        List.add (New Rectangle (7,8));
        List.add (New Rectangle (5,6));

        List.add (New Rectangle (2,2));

        Collections.sort (list);
        for (Rectangle obj:list) {System.out.println (Obj.getarea ());
}
    }

} 
6. Class Circle:package Yongheng;

    public class Circle {private double radius = 1.0D;
    Public Circle () {radius = 1.0D;
    Public Circle (double radius) {This.radius = radius;
    Public double Getarea () {return Math.PI * radius * RADIUS;

} class Comparable:package Yongheng; public class Comparablecircle extends Circle implements comparable<comparablecircle>{public comparablecircle ()
    {super ();
    Public comparablecircle (double radius) {super (RADIUS);
            @Override public int compareTo (Comparablecircle obj) {if (This.getarea () > Obj.getarea ()) {
        return 1;
        else if (This.getarea () < Obj.getarea ()) {return-1;
    else return 0;

}} main:package Yongheng;
        public class Main {public static void main (string[] args) {comparablecircle C1 = new Comparablecircle (2.0); ComparablecircLe C2 = new comparablecircle (3.0);
    System.out.println (C1.compareto (C2));

} 7.

Geometricobject similar to the fifth problem of the same interface colorable:package Yongheng;


public interface colorable {void Howtocolor ();}

Class Square:package Yongheng;

    public class Square extends Geometricobject implements colorable{private double side = 1.0D;
        Public Square () {super ();
    side = 1.0D;
        Public Square (double side, String color, Boolean filled) {super (color,filled);
    This.side = side;
    @Override public void Howtocolor () {System.out.println (GetColor ());
    @Override public Double Getarea () {return side*side;
    @Override public Double Getprimeter () {return side*4.0;

}} main:package Yongheng; The public class Main {//I write and ask for a bit of a do not conform to public static void Main (string[] args) {geometricobject arr[] = new GeoMet
        RICOBJECT[5];
Arr[0] = new Square (1, "white", false);        ARR[1] = new Square (2, "Blue", true);
        ARR[2] = new Square (3, "Black", true);
        ARR[3] = new Square (1, "Red", true);
        ARR[4] = new Square (1, "yellow", false);
        Square temp = null;
                for (Geometricobject Obj:arr) {if (obj instanceof square) {temp = (Square) obj;
            if (temp.isfilled ()) Temp.howtocolor ();
{8}}}}.

Class Mystack:package Yongheng;

Import java.util.ArrayList;

    public class Mystack implements cloneable{private arraylist<object> list = new arraylist<object> ();
    public Boolean IsEmpty () {return list.isempty ();
    public int GetSize () {return list.size ();
    Public Object Peek () {return list.get (List.size ()-1);
        Public Object Pop () {Object o = List.get (List.size ()-1);
        List.remove (GetSize ()-1);
    return o;
   public void push (Object o) {list.add (o); @Override public String toString () {return "stack:" + list.tostring (); @SuppressWarnings ("unchecked") @Override public Object clone () throws clonenotsupportedexception{M
        Ystack tstack = (mystack) super.clone ();
        Tstack.list = (arraylist<object>) list.clone ();
    return tstack;

}} main:package Yongheng;

Import Java.util.Date;  public class Main {public static void main (string[] args) throws clonenotsupportedexception{mystack stack =
        New Mystack ();
        for (int i = 0; i < ++i) Stack.push (i);
        Mystack Nstack = (mystack) stack.clone ();
        for (int i = 0; i < ++i) Nstack.pop ();
        System.out.println (Stack.getsize ());

    System.out.println (Nstack.getsize ());

9 class Course:package Yongheng;
    public class Course implements cloneable{private String coursename;
    Private string[] Students = new STRING[100]; Private int numberofstudents;
    Public Course (String coursename) {this.coursename = Coursename;
    public void Addstudent (String student) {students[numberofstudents++] = student;
    String[] Getstudents () {return students;
    public int getnumberofstudents () {return numberofstudents;
    Public String Getcoursename () {return coursename;
        @Override public Object Clone () {Course Course = null;
            try{course = (course) super.clone ();
            Course.coursename = Coursename;
            Course.numberofstudents = numberofstudents;
        Course.students = Students.clone ();
    The catch (Clonenotsupportedexception ex) {} return course;

}} main:package Yongheng;

Import Java.util.Date;

        public class Main {public static void main (string[] args) {Course Course = new Course ("Java");
   for (int i = 0; i < ++i)         Course.addstudent (i + "t");
        Course ct = (Course) course.clone ();
        Ct.addstudent ("Zhijun");
        System.out.println (Course.getnumberofstudents ());
    System.out.println (Ct.getnumberofstudents ());
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.