Java Programming Basics Review Notes Unit III

Source: Internet
Author: User

1 Unidirectional If statement bidirectional if statement dangling Elseswitch:char,byte,short,int2javax.swing.joptionpane.showconfirmdialog (null,text); return value: Joptionpane.yes_option:0joptionpane.no_option:1joptionpane.cancel_option:23cannot cast int from Booleancannot cast Boolean from int4%b5printf ("%f", 2), Java.util.IllegalFormatConversionException6java Basic is the value of the parameter, the basic type can not be changed, need an integer, Double packing, Field f = r1.getclass (). Getdeclaredfield ("value"); F.setaccessible (true); f.setdouble (r1.r1+1); You cannot also use the copy constructor public static Boolean Calequroot (double a,double b,double c,double r1,double R2) throws Nosuchfieldexception , SecurityException, illegalargumentexception, illegalaccessexception{double delta = a * a-4 * b * C;IF (Delta < 0) ret Urn false;delta=math.sqrt (delta); Field F=r1.getclass (). Getdeclaredfield ("value");//r1=double.valueof (1.0); r2=double.valueof (2.0); f.setaccessible (true); F.setdouble (R1,double.valueof ((-b+delta)/2/a); F.setdouble (r2,double.valueof (-b-delta)/2/a); return true;} The string type is an immutable string, and the pass-through does not change its value Keyword:boolean Expressionboolean ValuebooLean Typebreak statementconditional operatordangling-else ambiguityfall-through behavior: The statement starts at the match, Until the break statement is encountered or the end of the switch statement is reached. operator Associativityoperator precedenceselection statementshort-circuit evaluation3.1< <= >=! = = > 3.2cannot cast 3.330 is Even30 are odd________________30 are even3.4z is 5________________z are 7________________x is 23.5a== C==d indent correctly: b,c3.6 ..... ..... 3.7 equivalent 3.80.5,0.0,0.2343.9math.random () *20; Math.random () *10+10; Math.random () *40+10;3.10if (y > 0) x=1;3.11if (Score >) pay+=1.03;if (Score >) pay+=1.03;else pay*=1.01; 3.12if (score >= 90.0) grade = ' A ', else if (score >= 80.0) grade = ' B ', else if (score >= 70.0) grade = ' C '; else if ( Score >= 60.0) grade = ' D ', else grade = ' F '; 3.13boolean fl = count% = = 0;if (fl) NewLine = True;else NewLine = false; 3.14false,false,true,true,true,true,3.15x >= 1 && x <= 1003.16 (x >= 1 && x <= 100) | | x<03.17x = y && yx/= y (x! = 0) | | (x = 0) 3.18223.19true,false,true,false3.20 (x < y && y < Z) is true (x < y | | y < z) is true! (x < Y) is false (x + y < Z) is false (x + y < Z) are false3.21age > && age < 183.22weight > 50 || Height > 1603.23weight > && height > 1603.24weight > ^ height > 1603.25char,byte,int,long Next label The statement inside the check may not work. Compact format, good readability 3.2623.27switch (a) {case 1:x+=5;break;case 2:x+=10;break;case 3:x+=16;break;case 4:x+=34;} 3.28switch (day) {Case 0:dayname = "Sunday"; break;case 1:dayname = "Monday"; break;case 2:dayname = "Tuesday"; Break;case 3: Dayname = "Wednesday"; break;case 4:dayname = "Thursday"; break;case 5:dayname = "Friday"; break;case 6:dayname = "Saturday" ; break;} 3.29system.out.print (count + count% 10 ==0? ") \ n ":"); 3.30pay*= (temperature > 90?1.5:1.1); 3.31%b%c%d%f% s3.32 number of variable parameter number java.util.IllegalFormatConversionException 3.33amount is 32.32 3.232000e+01amount is 32.3200 3.2320e+ 01false Java falsejavafalse Java 3.35truetrue3.36 false, assignment operator (simple operator/= ...) 3.37falsefalse3.38yes,yes,yes3.39javax.swing.joptionpane.showconfirmdialog (Null,confirmmessage); Joptionpane.yes_option:0joptionpane.no_option:1joptionpane.cancel_option:23.1import Java.lang.reflect.Field; Import Java.util.scanner;import java.text.*;p ublic class Exercise {public static int calequroot (double a,double b,double C,double r1,double R2) throws Nosuchfieldexception, SecurityException, IllegalArgumentException, illegalaccessexception{double Delta = b * b-4 * A * c;if (Delta < 0) return-1;delta=math.sqrt (delta); Field F=r1.getclass (). Getdeclaredfield ("value"); F.setaccessible (true); F.setdouble (r1,double.valueof (-b+delta)/ 2/a) f.setdouble (r2,double.valueof (-b-delta)/2/a); if (Delta < 1e-10) return 0;return 1;} public static void Main (string[] args) throws Nosuchfieldexception, SecurityException, IllegalArgumentException, illegalaccessexception{double a,b,c;double r1 = double.valueof (0), r2 = double.valueof (0); Scanner Scanner = new Scanner (system.in); System.out.println ("Input A:"); a = SCANNER.NExtdouble (); System.out.println ("Input B:"); b = scanner.nextdouble (); System.out.println ("Input c:"); c = scanner.nextdouble (); int fl=calequroot (A,B,C,R1,R2); if (fl = = 1) { SYSTEM.OUT.PRINTLN (R1); SYSTEM.OUT.PRINTLN (R2);} else if (fl = = 0) {System.out.println (r1);} else {System.out.println ("NO ROOT");}}} 3.9import Java.util.scanner;public class Exercise {public static void main (string[] args) throws Exception{scanner Scanne R = new Scanner (system.in); int[] A = new Int[9];int ans=0; String Tmp=scanner.next (); for (int i=0;i<9;i++) {A[i]=tmp.charat (i)-' 0 '; ans+= (a[i] * (i + 1))% 11;} ans%=11; System.out.println (Tmp+ans);}} 3.17import Java.util.scanner;public class Exercise {public static void main (string[] args) throws exception{@ Suppresswarnings ("resource") Scanner Scanner = new Scanner (system.in); int r = (int) math.random () * 3; String[] srp={"Scissor", "Rock", "paper"}; String[] result={' You won ', ' You lose ', ' It is a draw '}; System.out.print ("scissor (0), Rock (1), paper (2):"); int A;while ((A=scanner.nextint ())<=2 && a>=0) {System.out.print ("The computer is" +srp[r]+ ". You are "+srp[a]+ (a==r?" too. ":". ") + (a==r?result[2]:(a-r==1| | a-r==-2) result[0]:result[1]));}}} 3.20import Java.util.scanner;public class Exercise {public static void main (string[] args) throws exception{@ Suppresswarnings ("resource") Scanner Scanner = new Scanner (system.in); System.out.print ("Enter the temperature in Fahrenheit:");d ouble Ta;while ((ta=scanner.nextdouble ()) < -58| | Ta > +) {System.out.print ("Enter the temperature in Fahrenheit bewteen-58 and 41:");} Double V; System.out.print ("Enter the wind speed miles per hour:"), while ((V=scanner.nextdouble ()) < 2) {System.out.print (" Enter the wind speed miles per hour above 2 miles/h: ");} Double ans=35.74 + 0.6215 * ta-35.75 * MATH.POW (V, 0.16) + 0.4275 * ta * Math.pow (V, 0.16); System.out.println ("The Wind Chill Index is" +ans);} 3.21import Java.util.scanner;public class Exercise {/** * lux consistency * */public static void Main (string[] args) throws Exceptio n{@SuppressWarnings ("resource") Scanner Scanner = new Scanner (system.in); int year,month,day,week; System.out.print ("Enter Year:"), Year = Scanner.nextint (); System.out.print ("Enter Month:"); month = Scanner.nextint (); if (Month < 3) month+=12; System.out.print ("Enter the Day of the Month:");d ay = scanner.nextint (); string[] Date = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};week = (day + 26* (month + 1)/ Ten + year% + year% 100/4 + year/400 + year/100*5)%7; System.out.println ("Day of the Week is" + Date[week]);}} 3.22import Java.util.scanner;public class Exercise {public static void main (string[] args) throws exception{@ Suppresswarnings ("resource") Scanner Scanner = new Scanner (system.in);d ouble x, y; System.out.print ("Enter a point with coordinates:"); x = Scanner.nextdouble (); y = scanner.nextdouble (); if (x * x + y * Y <=) {System.out.println ("point (" +x+ "," +y+ ") are in the Circle");} else {System.out.println ("point (" +x+ "," +y+ ") are not in the circle");}}} 3.27import Java.util.scanner;public class Exercise {final static double eps=1e-16;public static Boolean judge (double A,do uble b,double x,double y) {if (Math.Abs (A-B) < EPS && a < EPS) {if (Math.Abs (x-y) < EPS && x &lt ; EPS) {return true;} else return false;} Else{boolean FL = A * b < 0;boolean FL2 = (b * x + A * y-a * b) > 0;if (FL^FL2) {return false;} else return true;}} public static void Main (string[] args) throws exception{@SuppressWarnings ("resource") Scanner Scanner = new Scanner ( system.in);d ouble x, y; System.out.print ("Enter a point with the coordinates:"); x = Scanner.nextdouble (); y = scanner.nextdouble (); if (Judge ( 200,100,x,y) {System.out.println ("point (" +x+ "," +y+ ") are in the Circle");} else {System.out.println ("point (" +x+ "," +y+ ") was not in the Circle");}}} 3.28import Java.util.scanner;public class Exercise {final static double eps=1e-16;public static int judge (double[] W,doub Le[] h,double[] x,double[] y) {w[0]/=2;w[1]/=2;h[0]/=2;h[1]/=2;double dx= Math.Abs (X[0]-x[1]);d ouble dy = Math.Abs (y[0]-y[1]); if (dx > w[0] + w[1]| | Dy > H[0] + h[1]) return 0;if (dx > w[0]| | Dy > H[0]) return 2;return 1;} public static void Main (string[] args) throws exception{@SuppressWarnings ("resource") Scanner Scanner = new Scanner ( system.in);d ouble[] x = new double [2],y = new double [2],w = new double [2],h = new double [2]; System.out.println ("Enter R1 ' s center x, y-coordinates, width, and height:"); x[0] = Scanner.nextdouble (); y[0] = scanner. Nextdouble (); w[0] = Scanner.nextdouble (); h[0] = scanner.nextdouble (); System.out.println ("Enter R2 ' s center x, y-coordinates, width, and height:"); x[1] = Scanner.nextdouble (); y[1] = scanner. Nextdouble (); w[1] = Scanner.nextdouble (); h[1] = scanner.nextdouble (); int fl;if ((fl = judge (w,h,x,y)) = = 0) { SYSTEM.OUT.PRINTLN ("R2 does not overlap R1");} else if (fl = = 1) {System.out.println ("R2 is inside R1");} else {System.out.println ("R2 overlaps R1");}}}

  

Java Programming Basics Review Notes Unit III

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.