Java classes and Object Foundation questions

Source: Internet
Author: User

Write Java applications. First, define a clock class--clock, which consists of three int types

Member variables represent hours, minutes, seconds, and a construction method for three member variables (hours, minutes, seconds)

For initialization, there is also a member method show () to display the time of the clock object. Second, redefine

A main class,TestClass, creates objects of multiple clock classes in the main method of themaster Class , using this

Some objects call the method show () to display the time of the clock.

 Public classClock {inthour; intmin; intsec; Clock (intHintMints) { This. hour=h;  This. min=m;  This. sec=s; }     voidShou () {if(hour>=0&&hour<24) && (min>=0&&min<60) && (sec>=0&&sec< 60)) System.out.println (Hour+ ":" +min+ ":" +sec); ElseSystem.out.println ("Time entered is incorrect"); }     Public Static voidMain (string[] args) {Clock Shijian=NewClock (12,30,45); Clock Shijian1=NewClock (25,20,61); Clock shijian2=NewClock (8,15,59);        Shijian.shou ();        Shijian1.shou ();    Shijian2.shou (); }}

Write Java applications. First, define an account class that describes the bank accounts , including the member changes

Amount "Account" and "Deposit balance", the Member method has "deposit", "withdrawal" and "balance inquiry". Secondly

Write a main class that tests the function of the account class in the main class.

 Public classAccount {String Zhanghao; DoubleYuE; Account (String Zhanghao,DoubleYuE) {         This. zhanghao=Zhanghao;  This. Yue=YuE; }    DoubleCunkuan (DoubleQianshu) {                if(qianshu<0) System.out.println ("Money Error"); Else           This. yue+=Qianshu; return  This.    YuE; }    voidQukuan (DoubleQianshu) {        if(qianshu> This. YuE) {System.out.println ("Insufficient balance"); return; }         This. yue-=Qianshu; }        DoubleGetyue () {return  This.    YuE; }     Public Static voidMain (string[] args) {Account AC=NewAccount ("123456", 1000); Ac.cunkuan (500); System.out.println ("Balance" +Ac.getyue ()); Ac.qukuan (300); System.out.println ("Balance" +Ac.getyue ()); Ac.cunkuan (-200); System.out.println ("Balance" +Ac.getyue ()); Ac.qukuan (2000); System.out.println ("Balance" +Ac.getyue ()); }}

Java classes and Object Foundation questions

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.