"Java Technology" first time job

Source: Internet
Author: User

(a), learning summary
1. In Java, through the scanner class to complete console input, consult the JDK Help document, scanner class implementation of basic data input method? Can not only use text description, must write code, through specific examples to explain.
Text Scan class Scanner (Java.util.Scanner), you can use a different next method to convert the input text to a different type of value.
The methods for obtaining basic type data are:
Nextint (): Reading integer data
Nextdouble (): Read double-precision data
Next (): reads the next word of the input (space as a delimiter)
Nextline (): reads the next line of input

import java.util.Scanner;public class Test{    public static void main(String[]args){        Scanner input=new Scanner(System.in);        ...    }}

The random () method of the 2.Random class and the math class can produce a stochastic number, what is the difference between these two ways, and what are the characteristics of each? Consult the JDK Help documentation for an example.

util的random是一个类:包含nextInt(),nextDouble ()等类似的方法。math的random只是一个方法:返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。

For example, compare the random numbers produced by two methods:

import java.util.Random;public class Demo{    public static void main(String args[]){        Random a=new Random();        int a1=a.nextInt();        double b=Math.random();         System.out.println("这是调用Random()类产生的随机数");        System.out.println(a1);        System.out.println("这是调用Math.random()方法产生的随机数");        System.out.println(b);    }}

Use the Math.random () method to produce a random number of type int, for example to produce a random number between 0~100:

import java.util.Random;public class Demo{    public static void main(String args[]){        Random a=new Random();        int b=a.nextInt();        int c=(int)(Math.random()*100);         System.out.println("这是调用Random()类产生的随机数");        System.out.println(b);        System.out.println("这是调用Math.random()方法产生的随机数");        System.out.println(c);    }}

Both methods can play the same role, but if many threads need to generate pseudo-random numbers at very high rates, then math.random () may reduce the contention that each thread has its own pseudo-random number generator. So many applications will find the Math.random () method easier to use.

3. What is the result of running the following procedure? Check the information and analyze why.

public class Test {    public static void main(String args[]) {         double a = 0.1;        double b = 0.1;        double c = 0.1;        if((a + b + c) == 0.3){            System.out.println("等于0.3");        }else {            System.out.println("不等于0.3");        }    }     }

The program runs with a result of: not equal to 0.3

Cause: Floating-point data has a very small error when it is being operated.

In order to deal with the loss of precision, you can use the Java.math.BigDecimal class, consult the JDK help document or the textbook p378 to modify the above procedure.

import java.math.BigDecimal;import java.math.MathContext;public class Test {       public static void main(String args[]) {            BigDecimal a = new BigDecimal(0.1);           BigDecimal b = new BigDecimal(0.1);           BigDecimal c = new BigDecimal(0.1);            if(a.add(b).add(c).round(new MathContext(1)).equals(new BigDecimal("0.3"))){                System.out.println("等于0.3");            }else {                System.out.println("不等于0.3");            }        }    }

The above program output is: equal to 0.3

4. In the environment configuration, the role of Path and Classpath, Java_home:
-1). Path environment variable. The function is to specify the command search path, execute commands under the I command line as Javac compiles the Java program, it looks in the path specified by the path variable to see if the appropriate command program can be found. We need to add the bin directory in the JDK installation directory to the existing path variable, and the bin directory contains the frequently used executables such as Javac/java/javadoc wait, and after setting the path variable, you can execute the Javac/java tool in any directory.
-2). CLASSPATH environment variables. The role is to specify the class search path, to use the already written classes, the premise is to be able to find them, the JVM is through the classpth to find the class. We need to set the Dt.jar and Tools.jar in the Lib subdirectory of the JDK installation directory to classpath, of course, the current directory "." must also be added to the variable.
-3). Java_home environment variables. It points to the JDK's installation directory, and software such as Eclipse/netbeans/tomcat finds and uses the installed JDK by searching for java_home variables.

(b), experimental summary

  • Experimental content:
    • 1. See commodity guessing price and topic extension
      -Design Ideas
      -First use the random number to get the price of the commodity, using the cycle to control the number of times the user guesses the price, define a variable to save the user to guess the price of the score, and then define a new variable to save the user to guess the price accumulated scores, in a round guess the price of the final output of the price of the commodity, The number of times the user's total score and guess price are output at the end of the game.
      -Note
      -because user input is defined using a string type to continue the game instruction, do not use = = when comparing commodity price sizes, if using the Equals method of the String class. Str.equals (STR1);
        • 2. Perpetual calendar
          • Design ideas
            • 1) Write a method to determine the leap year.
            • 2) According to a year is a leap years or common year to write a method to determine how many days a certain month.
            • 3) Write a method to calculate the number of days before a certain month before the distance of January 1900. Because the number of days in each month will be different, determine the number of days in a month before calculating the total number of days from January 1900.
            • 4) Write a method to output a calendar for the month of the year. A method known to determine the day of the week: The value of 1+totaldays%7 is 1-7, from Monday to Sunday, respectively.
          • Problem
            • 1) The output calendar determines whether the line is swapped, using the relationship between January 1900 and 7. namely 1+totaldays%7=?. A value of 7 o'clock wraps the line.
        • 3. Scoring System
          • Design ideas
            • 1) Use a two-dimensional array to hold each jury's score for each player.
            • 2) Use one-dimensional arrays to represent the highest score, the lowest score, and the average score of the highest score and the lowest score for each player.
            • 3) Using bubble Sort method to sort the rows of a two-dimensional array from high to low, the maximum minimum value of each row is stored in a one-dimensional array representing the highest and lowest points.
            • 4) Each player's final score is calculated using a cycle, which is stored in the average score group.
            • 5) output each player's score and the average score of each player according to the order from highest to lowest.
          • Problem
            • 1) using the bubble sorting method to sort the two-dimensional array is to pay attention to the problem of array crossing.
            • 2) using a three-layer loop to sort the two-dimensional array, the first loop changes the row, and each row of the two-dimensional array is treated as a one-dimensional array in the second and third loops by using the bubble sort method to sort each row of the two-dimensional array.
              (c), code hosting
    • Code Cloud Commit History

"Java Technology" first time job

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.