Programming exercises for the 3rd and 4 chapters of Java language programming

Source: Internet
Author: User

3.1

 Public classTest { Public Static voidMain (string[] args) {System.out.println ("Enter A, B, C:"); Scanner input=NewScanner (system.in); DoubleA =input.nextdouble (); Doubleb =input.nextdouble (); Doublec =input.nextdouble (); DoubleDelta = b * b-4 * A *C; Doublet = Math.pow (Delta, 0.5); if(Delta > 0) {            DoubleX1 = (-B + t)/2; Doublex2 = (-b-t)/2; System.out.println ("The Roots is" + x1 + "+" +x2); } Else if(Delta = = 0) {System.out.println ("The root is" +-b/(2 *a)); } Else{System.out.println ("The equation has no real roots"); }    }}

3.2

 Public classTest { Public Static voidMain (string[] args) {System.out.println ("Enter An integer:"); Scanner input=NewScanner (system.in); intn =Input.nextint (); System.out.print ("is" + N + "an even number?"); if(n% 2 = = 0) System.out.println ("True"); ElseSystem.out.println ("False"); }}

3.3

 Public classTest { Public Static voidMain (string[] args) {System.out.print ("Enter A, B, C, D, E, F:"); Scanner input=NewScanner (system.in); DoubleA =input.nextdouble (); Doubleb =input.nextdouble (); Doublec =input.nextdouble (); DoubleD =input.nextdouble (); DoubleE =input.nextdouble (); Doublef =input.nextdouble (); DoubleFM = A * D-b *C; if(fm = = 0) {System.out.println ("The equation has no solution"); } Else{System.out.println ("A Is" + ((E * d-b * f)/FM) + "and Y are" + ((A * F-E * c)/FM)); }    }}

3.4

 Public classTest { Public Static voidMain (string[] args) {Scanner input=NewScanner (system.in); intA = (int) (Math.random () * 100); intB = (int) (Math.random () * 100); System.out.print ("Enter the sum of the 0~100:" + A + "and" + B + ":"); intc =Input.nextint (); if(c = = A +b) System.out.println ("True"); ElseSystem.out.println ("False"); }}

3.5

 Public classTest { Public Static intJudgeintYearintmonth) {        BooleanLeap; Leap= (year% of 4 ==0 && year% 100! = 0) | | (Year% 400 = = 0); if(Month = = 2) {            if(LEAP)return29; Else return28; } Else if(Month = = 1 | | month = = 3 | | month = = 5 | | month = = 7 | | month = 8 | | month = = | | month = = 12) {            return31; } Else {            return30; }    }     Public Static voidMain (string[] args) {string[] months= {"", "January", "February", "March", "April",                "May", "June", "July", "August", "September",                "October", "November", "December"}; System.out.print ("Inpit Month and Year:"); Scanner input=NewScanner (system.in); intmonth =Input.nextint (); intYear =Input.nextint (); System.out.println (Months[month]+ "" + year + "have" + judge (year, month) + "Days"); }}

4.7

 Public classTest { Public Static voidMain (string[] args) {Doublen = 10000; Doubles1, S2, t; S1= S2 = 0; T= 1; Final DoubleRate = 0.05;  for(inti = 1; I < 11; i++) {T*= (1 +Rate ); } S1= n *T; System.out.println ("S1 =" +S1); }}

4.16

 Public classTest { Public Static voidMain (string[] args) {System.out.print ("Enter A number:"); Scanner input=NewScanner (system.in); intn =Input.nextint (); inti = 2;  while(true) {             while(n% i = = 0 && n! =i) {System.out.print (i+ ", "); N/=i; } I++; if(n = =i) {System.out.println (i);  Break; }        }    }}

4.25

 Public classTest { Public Static DoubleCOUNTPI (intN) {DoublePI = 0; DoubleT; intM=1;  for(inti = 1; I < n; i++) {T=1.0/(2*i-1); T*=m; Pi+=T; M*=-1; } Pi*= 4; returnPi; }         Public Static voidMain (string[] args) {System.out.print ("Enter A number:"); Scanner input=NewScanner (system.in);  for(inti = 10000; I <= 100000; i++) {System.out.println ("Pi (" + i + ") =" +COUNTPI (i)); }    }}

4.27

 Public classTest { Public Static BooleanIsleapyear (intN) {return((n% 4 = = 0 && n% = 0) | | n 400 = = 0); }         Public Static voidMain (string[] args) {intn = 0;  for(inti = 2001; I < 2100; i++) {            if(Isleapyear (i)) {n++; if(n% 11 = = 0) {System.out.println ("\ n"); } Else{System.out.print (i+ " "); }                            }        }    }}

4.33

 Public classTest { Public Static BooleanTestintN) {inti, sum; intm = N/2; Sum= 0;  for(i = 1; I <= m; i++) {            if(n% i = = 0) Sum+=i; }        if(Sum = =N)return true; Else            return false; }     Public Static voidMain (string[] args) { for(inti = 2; I < 10000; i++) {            if(Test (i)) System.out.print (i+ "\ n"); }    }}

4.41

 Public classTest { Public Static voidMain (string[] args) {intN, Count, Max, T; Scanner input=NewScanner (system.in); System.out.println ("Enter A number:"); N=Input.nextint (); T= max =N; Count= 0;  while(T! = 0) {            if(T >max) {Count= 1; Max=T; } Else{Count++; } System.out.println ("Enter A number:"); T=Input.nextint (); } System.out.println ("max=" + Max + ", count=" +count); }}

Programming exercises for the 3rd and 4 chapters of Java language programming

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.