Java hands-on labs and after-school programs

Source: Internet
Author: User

Homework after class

First, write the program, the message box displays the calculation result

Design idea: Import the Scanner package, use the joptionpane class to implement the input of the message box and display the result.

Program code:

Package com;

Import Java.util.Scanner;

import Scanner Package

Import Javax.swing.JOptionPane;

public class Manner {

public static void Main (string[] args) {

Scanner in=new Scanner (system.in);

defining The Scanner class

int A, B;

String s1,s2;

S1 =joptionpane.showinputdialog ("Enter first integer");

S2=joptionpane.showinputdialog ("Enter second integer");

Enter a two integer

A= Integer.parseint (S1);

b= integer.parseint (S2);

The conversion of String type two data into integer two data for Operation Joptionpane.showmessagedialog (

NULL, "Two integers and is:" + (a+b));

Joptionpane.showmessagedialog (

NULL, "The difference of two integers is:" + (A-B);

Joptionpane.showmessagedialog (

NULL, "the product of two integers is:" + (a*b));

Joptionpane.showmessagedialog (

NULL, "Quotient of two integers:" + (A/b));

In.close ();

}

}

Results:

Second, the hands of the brain

One, (1) Read the example carefully : Enumtest.java, run it, analyze the running results?

A: The result of the operation is:true SMALL MEDIUM LARGE, after judging whether the same object is referenced, output the data of the array using an enumeration.

(2) What conclusions can you get? Have you mastered the basic usage of enum types?

A: The enumeration type input is a string type, the output is a string type, it is not part of the original data type, is a reference type, so you can use the "= =" and the equals () method compared to the value of the variable.

Binary representation of numerical values

1, the original code: The notation is the absolute value of the symbolic bit plus true value, that is, with the first symbol, the remaining bits represent values.

2, anti-code: The expression is a positive inverse code is its own, negative number of the anti-code is in its original code on the basis of the symbol bit unchanged, the remaining bits are reversed. So if an inverse code represents a negative number, the human brain can not visually see its value, usually to convert it to the original code and then calculate.

3, Complement: the expression is a positive complement is its own, the complement of the negative is in its original code on the basis of the symbol bit unchanged, the rest of you take the counter, the last +1(that is, on the basis of anti-code +1). For negative numbers, the complement representation is also the human brain can not intuitively see its value, usually also need to convert to the original code and then calculate its value.

4. The number in Java is represented by the complement.

Three

Look at this diagram, and look at the number of bits in Java for each data type, and the range of values that you can draw.

For:Char 8bits binary number range is-2of the7the second party to2of the7Sub-party-1,Byte 8bit signed integer-128to the127any integer between the Shortbit unsigned integer-32768to the32767any integer between theintbit signed integer-2of the tothe second party to2of the toSub-party-1any integer between theLongbit signed integer-2of the thethe second party to2of the theSub-party-1any integer between thefloatbit single-precision floating-point number,Doublebit double-precision floating-point number.

The conclusion is thatJava will automatically convert from a low-level type to an advanced type, that is, a data type with a relatively small range of values converted to a data type with a relatively large range of values.

Four, please run the following code (Testdouble.java), you see what kind of output, unexpected?

Answer: 0.05 + 0.01 = 0.060000000000000005

1.0-0.42 = 0.5800000000000001

4.015 * 100 = 401.49999999999994

123.3/100 = 1.2329999999999999

The results are imprecise and are not accurate in the mathematical range, because the double type is a floating-point type and has a precision problem.

Why is the value of a double type not "mathematically accurate" as a result of the operation?

For:DoubleThe type is a double-precision floating-point number, which consists of the sign bit, the order code and the mantissa, the sign bit determines the positive and negative of the floating-point number, the exponent and the mantissa are obtained from the binary scientific counting form of the floating-point number, according to the storage standard of floating-point numberIEEEdeveloped,DoubleThe starting number of the type exponent is1023, on the basis of the index, is the memory of the exponential representation, the mantissa is directly filled in, the space is redundant to0if space is not enough,0Shed1in. Because the binary cannot be precisely represented, soDoublenot very precise. In a computer, a floating-point number is saved with a limited number of contiguous bytes, and many decimals are not exactly represented in the binary computer, because the number of bits in the mantissa field of the floating-point count is limited, and for this reason, the float is persisted until the resulting mantissa is sufficient to fill the Mantissa field, after which the extra bits are rounded. In other words, the decimal-to-binary transformation is not guaranteed to be accurate, but only approximate, in addition to the accuracy issues previously mentioned. In fact, only a very small number of decimal decimals have accurate binary floating-point representation. Coupled with the accumulation of errors in floating-point arithmetic, the result will be the inaccurate problem of floating-point arithmetic.

Six, (1) What is the output of the following code?

int x=100;

int y=200;

System.out.println ("x+y=" +x+y);

System.out.println (x+y+ "=x+y");

The result: x+y=100200 300=x+y

(2) Why are there such output results?

A: Because a variable of type string plus any other basic type of variable will be treated as a string. And the output of the first statement inside x+y is not enclosed in parentheses, it becomes a string, the function of the Plus is a concatenation of strings, so the output is 100200,and the output of the second statement X+y in front of the top, no parentheses are required, so the plus sign is the SUM function.

Java hands-on labs and after-school programs

Related Article

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.