Assignment 1:
-
What are the requirements of Java language for identifier definition? Which of the following identifiers are legal?
(1) int char ; (2) char ; (3) float fLut ; (4) byte Cy%ty=12345;
(5) double Dou_St; (6) String (key); (7) long $123=123456L; (8) boolean aa=123.45; -
If x = 5, y = 3 and z = 0 are known, calculate the following values of Z.
(1) z=z+y*x++ (2) z=y+++x (3) z=y+x++ (4) z=~x (5) z=x^y (6) z=x<<y -
Try to write a program to convert Celsius temperature into Fahrenheit temperature. The conversion formula is as follows:
Fahrenheit = (9 / 5) * Celsius + 32 -
Given that the volume of the sphere is 4 / 3 π r3, try to write a program, input the radius of the sphere, and calculate the volume of the sphere.
-
Use the do... while loop to calculate 1+ 2!+…+ 100! The sum of.
-
Write a Java program and output all Greek letters. (hint: from ') α’ To ' ω’)
1.
Identifiers are symbols used to name variables, classes, methods, etc. in Java programs. There is a convention for the method of identifier definition, that is, a string consisting of a letter or a string of letters, numbers or symbols starting with a letter. Specific requirements are as follows:
①. Only underscores are allowed for symbols_ And the dollar sign;
②. Name length is unlimited;
③. Note that the case of English letters represents different meanings;
④. Naming should follow the principle of easy to understand and easy to remember;
⑤. You can't use reserved words as names.
The identifiers (3), (4), (5) and (7) in the question are correct, and (1), (2), (6) and (8) are wrong.
(1) char in is a reserved word and cannot be used as an identifier, which violates requirement 5;
(2) There is only the type declaration of identifier in the, and there is no identifier;
(6) Symbols other than underline and USD cannot appear in the bid identifier, which violates requirement 1;
(8) The variable declared by the identifier Boolean is of boolean type, which can only be assigned to true or false.
2.
(1)18 (2) 8 (3) 8
(4) Binary original code: 0000 0000 0000 0101
After reverse operation: 1111 1111 1111 1111 1111 1111 1111 1010
In Java, signed integers are represented by complement, and complement = inverse code + 1
1. Reverse code first: 1000 million 0101
2. Additional complement: 1000 0000 0110
The highest bit represents the sign bit. 1 represents a negative number and 0 represents a positive number
So the result is - 6
(5)6
(6)40
3. / / try to write a program to convert Celsius temperature to Fahrenheit temperature. The conversion formula is as follows:
//Fahrenheit = (9 / 5) Celsius + 32`*
**`import java.util.*; public class A { public static void main(String[] args) { int x; float F; System.out.print("Enter Celsius temperature:"); Scanner s=new Scanner(System.in); x=s.nextInt(); F=((float)9/5)*x+32; System.out.println("Fahrenheit temperature is:"+F); } }
4. / / given that the volume of the sphere is 4 / 3 π r3, try to write a program, input the radius of the sphere, and calculate the volume of the sphere.
import java.util.*; public class A { public static void main(String[] args) { float r; System.out.print("Enter radius:"); Scanner s=new Scanner(System.in); r=s.nextInt(); System.out.println("Sphere volume:"+(double)(4*Math.PI*Math.pow(r,3)/3)); } }
5. / / use the do... while loop to calculate 1+ 2!+…+ 100! The sum of.
```java import java.math.BigInteger;//Used to solve the problem of data overflow public class A{ public static void main(String[] args) { BigInteger bi=new BigInteger("1"); BigInteger sum=new BigInteger("0"); for(int i=1;i<=100;i++){ for(int j=1;j<=i;j++){ bi=bi.multiply(new BigInteger(j+"")); } sum=sum.add(bi); bi=bi.valueOf(1) ;//Reset bi } System.out.println(sum); } } 6.//Write a Java program and output all Greek letters. (hint: from ') α’ To ' ω’) ```java public class A { //Reference and print English alphabet public static void main(String[] args) { int firstEnglish, lastEnglish; char firstE = 'A', lastE = 'Z'; //Gets the value of the first and last letters firstEnglish = (int) firstE;//Forced type conversion lastEnglish = (int) lastE;//ditto System.out.println("English alphabet: ");//Print wrap for (int i = firstEnglish; i <= lastEnglish; ++i) { char uppercase, lowercase; uppercase = (char) i; lowercase = (char) (i + 32);//ASCII codes of upper and lower case letters differ by 32 System.out.print(" " + uppercase + lowercase); } System.out.println();//Line feed int firstGreek, lastGreek; char firstG = 'α', lastG = 'ω'; firstGreek = (int) firstG; lastGreek = (int) lastG; System.out.println("Greek alphabet: "); for (int i = firstGreek; i <= lastGreek; ++i) { char greekLetter; greekLetter = (char) i; System.out.print(" " + greekLetter); } } }