Java hands-on brain after class study questions

Source: Internet
Author: User


Public classSquareint { Public Static voidMain (string[] args) {intresult; for(intx =1; X <=Ten; X + +) {result=Square (x); //The Math Library also provides a way to calculate the square number//result= (int) Math.pow (x,2);System. out. println ("The square of"+ x +" is"+ result +"\ n"); } } //a static method for calculating the square number of a custom Public Static intSquareinty) {returnY *y; }}

1. This code uses static to call the custom method, if not static you can use the class name. member name or object name. The member name is called.

2, write a method, using the pure random number generator algorithm to generate a specified number (such as 1000) of random integers.

Import Javax.swing.joptionpane;public class Testseed {public   static void Main (String args[])   {      int value;< c3/>string output = "";      for (int i = 1; i <=; i++) {         value = 1 + (int) (Math.random () *);         Output + = value + "  ";                  if (i% 10== 0)            output + = "\ n";      }      Joptionpane.showmessagedialog (null, Output,         "Random Numbers from 1 to 6",         Joptionpane.information_ MESSAGE);      System.exit (0);}   }

This program outputs 100 random numbers, but does not write with a random number generator, and there is a problem with this program.

3. Take a look at the following code, do you find anything special?

methodoverload.java//Using overloaded Methodspublic class Methodoverload {public static void main (string[] args) {Syst Em.out.println ("The square of the integer 7 is" + Square (7)); System.out.println ("\nthe square of double 7.5 is" + square (7.5));} public static int square (int x) {return x * x;} public static double square (double y) {return y * y;}}

This code customizes two methods, showing the Java method overloads, one for the type int, one for the double type, and the int type automatically when the output is typed, the double type automatically calls the method of the double type, and Changing the order of custom methods does not affect the results, proving that the order of the calls is not sequential, but that Java automatically recognizes the parameter types and then finds the corresponding custom method to perform the calculations.

4. Use computer to calculate the number of combinations:

(1) Using the combination number formula to calculate with n!

Import Java.util.scanner;public class Zuheshu1 {public static void main (String[]args) {System.out.println ("Enter the number of combinations N and K: "); Scanner in1=new Scanner (system.in); int n=in1.nextint (); Scanner in2=new Scanner (system.in); int k=in2.nextint (); int result=jiechen (n)/(Jiechen (k) *jiechen (n-k)); SYSTEM.OUT.PRINTLN ("Combined number result is:" +result); In1.close (); In2.close ();} public static int Jiechen (int n) {int jieguo=1;if (n<0) {System.out.println ("Input is illegal! ");} else if (n==0| | n==1) {jieguo=1;} else {jieguo=jiechen (n-1) *n;} return Jieguo;}}

Experimental results

(2) Using recursive method to calculate with Yang Hui triangle

Package Zuheshu2;import Java.util.scanner;public class Zuheshu2 {public static void main (String[]args) { System.out.println ("Please enter the number of combinations N and K:"); Scanner in1=new Scanner (system.in); int n=in1.nextint (); Scanner in2=new Scanner (system.in); int k=in2.nextint (); SYSTEM.OUT.PRINTLN ("Combined number result is:" +jieguo (N,k)); In1.close (); In2.close ();} public static int Jieguo (int n,int m) {if (m==0| | N==M) return 1;int s=math.min (M, n-m); int f=1,f1=0;for (int i=1;i<=s;i++) {f1=f* (n-i+1)/(i); f=f1;} return F1;}}

Experimental results: (This algorithm for reference Baidu)

(3) Using recursive method to calculate the recursive formula of combination number

Import Java.util.scanner;public class Zuheshu2 {public static void main (String[]args) {System.out.println ("Enter the number of combinations N and K: "); Scanner in1=new Scanner (system.in); int n=in1.nextint (); Scanner in2=new Scanner (system.in); int k=in2.nextint (); SYSTEM.OUT.PRINTLN ("Combined number result is:" +jieguo (N,k)); In1.close (); In2.close ();} public static int Jieguo (int m,int n) {if (m<0| | n<0| | M<n) return 0;if (m==n) return 1;if (n==1) return M;return Jieguo (m-1,n) +jieguo (m-1,n-1);}}

Experimental results:

5. Recursive programming solves the problem of the Nottingham Tower. Implemented in Java

public class towersofhanoi{public   static void solvetowers (int disks, int sourcepeg,       int destinationpeg, int temp Peg)   {      if (disks = = 1)      {         System.out.printf ("\n%d-to-%d", sourcepeg, destinationpeg);         return;      }      Solvetowers (Disks-1, Sourcepeg, Temppeg, destinationpeg);      System.out.printf ("\n%d-to-%d", sourcepeg, destinationpeg);      Solvetowers (Disks-1, Temppeg, Destinationpeg, sourcepeg);   }    public static void Main (string[] args)   {      int startpeg = 1;       int endpeg = 3;       int temppeg = 2;       int totaldisks = 3;      Solvetowers (Totaldisks, Startpeg, Endpeg, temppeg);   

Experimental results:

6. Palindrome number

Import java.util.*;p Ublic class Palindrome {public static void main (String[]args) {     //Enter a string from the keyboard str  string str ="";  System.out.println ("Please enter a string:");  Scanner in=new Scanner (system.in);  Str=in.nextline (); Creates a character cache class object from a string sb  stringbuffer sb=new stringbuffer (str);//The contents of the character cache are inverted  sb.reverse (); The same number n int n=0 is calculated for the corresponding position character of STR and SB  ;  for (int i=0;i<str.length (); i++) {   if (Str.charat (i) ==sb.charat (i))    n++;  }//If all characters are equal, That is, the value of n equals the length of STR, then STR is a palindrome.     if (N==str.length ())      System.out.println (str+ "is a palindrome!");     else      System.out.println (str+ "Not a palindrome!");}}

Experimental results:

Java hands-on brain after class study 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.