Java job 05 (Chapter 1 Common runtime Class 2)

Source: Internet
Author: User

Package homework05; </P> <p> Import Java. math. biginteger; </P> <p> public class ch07_3 {<br/> Public static double factorial (int n) // calculate the factorial function <br/>{< br/> If (n = 0) return 1; <br/> else return N * factorial (n-1 ); <br/>}< br/> Public static biginteger factorial (int n) {<br/> biginteger bigint = NULL; <br/> If (n = 0) {<br/> bigint = new biginteger ("1"); <br/> return bigint; <br/>}</P> <p> else {<br/> bigint = ne W biginteger (integer. tostring (n); <br/> return bigint. multiply (factorial (n-1); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/>/* 4. Calculate 1 + 2! + 3! + 4! + · Sum of items from 100th to 200 */<br/> double f = 0; <br/> for (int K = 100; k <= 200; k ++) {<br/> F + = factorial (k); <br/>}< br/> system. out. println ("1 + 2! + 3! + 4! + · Sum of items from 100th to 200: "+ F); // the output value is infinity <br/> // infinity indicates positive infinity, that is, the value is greater than the maximum value of the double type. Negative infinity outputs-infinity. <br/> // In fact, any calculation result that exceeds the maximum value that can be expressed by the double type produces this result. <Br/> // use a large integer for processing. <Br/> biginteger result = new biginteger ("0"); <br/> for (int K = 100; k <= 200; k ++) {<br/> result = result. add (factorial (k); <br/>}< br/> system. out. println ("1 + 2! + 3! + 4! + · The sum of items from 100th to 200: "+ result); <br/>}</P> <p>

Result:

1 + 2! + 3! + 4! + · Sum of items from 100th to 200: infinity
1 + 2! + 3! + 4! + · Sum of items from 100th to 200: 792621072814943158937574954417696054502273470568077747007887743862285047941581535541498718312275086275679893343076013862361579680670972527976009279036348551929550827607601145450876014530359530008733947699274904382825444692112993342058966668017369617374101405221613980910401559347844227172278387932925351155828685624342954436057587785914951951445917696000000000000000000000000

Whether the question is correct or not remains to be verified.

 

Package homework05; </P> <p> Import Java. util. hashset; <br/> Import Java. util. iterator; </P> <p>/* <br/> * Subject: Set A = {1, 2, 4} and B = {1, 3, 7, 9, 11 }, write an application to output the intersection of A and B, and set and difference set. <Br/> */<br/> public class ch07_4 {</P> <p> Public static void displayset (hashset <integer> temp) {<br/> iterator <integer> iter = temp. iterator (); <br/> while (ITER. hasnext () {<br/> integer TE = (integer) ITER. next (); <br/> system. out. print (Te. intvalue () + "/t"); <br/>}< br/> system. out. println (); // line feed. <Br/>}</P> <p> Public static void main (string [] ARGs) {<br/> integer [] A = {1, 2, 4 }; <br/> integer [] B = {1, 3, 7, 9, 11}; <br/> hashset <integer> A = new hashset <integer> (); <br/> hashset <integer> B = new hashset <integer> (); <br/> for (INT I = 0; I <. length; I ++) {// initialize set a <br/>. add (A [I]); <br/>}< br/> for (INT I = 0; I <B. length; I ++) {// initialize Set B <br/> B. add (B [I]); <br/>}</P> <p> hashset <integer> tempset = (hashset <integer>). CLON E (); // saves the original value of set a as an intermediate variable. </P> <p>. addall (B); // Union <br/> system. out. print ("output result of the union of A and B:"); <br/> displayset (a); // output result. </P> <p> A = (hashset <integer>) tempset. clone (); <br/>. retainall (B); // intersection <br/> system. out. print ("output result of intersection A and B:"); <br/> displayset (a); // output result. </P> <p> A = (hashset <integer>) tempset. clone (); <br/>. remove (B); // intersection <br/> system. out. print ("output result of difference set A and B:"); <br/> displayset (a); // output result. </P> <p >}< br/>

Running result:

Output the result of the union of A and B: 1 2 3 4 7 9 11
Output the result of intersection A and B: 1 3
Output the result of A and B difference sets: 1 2 3 4

Package homework05; </P> <p> Import Java. util. iterator; <br/> Import Java. util. treeset; </P> <p>/* <br/> * Title: 10 hard disks, two important attributes: price and capacity. Write an application and use the <br/> * treemap <K, V> class to sort and output the detailed information of 10 hard disks by price and capacity. <Br/> */<br/> // hard disk class <br/> class harddisk implements comparable {<br/> private int price; <br/> private int content; </P> <p> Public static int flag = 1; // flag is arranged by price or by capacity. 1: price 2: Capacity </P> <p> Public harddisk (INT price, int content) {<br/> This. price = price; <br/> This. content = content; <br/>}< br/> Public int compareto (object B) {<br/> harddisk Hd = (harddisk) B; <br/> If (flag = 1) {<br/> return (this. price-hd.price); <br/>}< br/> If (flag = 2) {<br/> return (this. content-hd.content); <br/>}< br/> return 0; <br/>}</P> <p> Public int getcontent () {<br/> return content; <br/>}< br/> Public void Setcontent (INT content) {<br/> This. content = content; <br/>}< br/> Public int getprice () {<br/> return price; <br/>}< br/> Public void setprice (INT price) {<br/> This. price = price; <br/>}</P> <p >}< br/> public class ch07_5 {</P> <p> Public static void main (string [] ARGs) {<br/> harddisk [] HDS = {New harddisk (), new harddisk ), <br/> New harddisk (150,30), n EW harddisk (500,200), new harddisk (), new harddisk (), <br/> New harddisk (), new harddisk )}; </P> <p> treeset <parddisk> treeset = new treeset <parddisk> (); <br/> for (INT I = 0; I <HDS. length; I ++) {// sort by price by default. <Br/> treeset. add (HDS [I]); <br/>}< br/> iterator <parddisk> iter = treeset. iterator (); <br/> system. out. println ("=== details ="); <br/> system. out. println ("price/T capacity"); <br/> while (ITER. hasnext () {<br/> harddisk Hd = ITER. next (); <br/> system. out. println (HD. getprice () + "/t" + HD. getcontent (); <br/>}</P> <p> // <br/> treeset. clear (); // clear the content in the treeset. <Br/> for (INT I = 0; I <HDS. length; I ++) {// sort by capacity. <Br/> harddisk. flag = 2; <br/> treeset. add (HDS [I]); <br/>}< br/> iterator <parddisk> iter2 = treeset. iterator (); <br/> system. out. println ("=== details ="); <br/> system. out. println ("capacity/t price"); <br/> while (iter2.hasnext () {<br/> harddisk Hd = iter2.next (); <br/> system. out. println (HD. getcontent () + "/t" + HD. getprice (); <br/>}</P> <p >}< br/>

Running result:

=== Details ===
Price capacity
25 1
40 2
80 25
100 35
150 30
200 40
250 38
270 60
300 50
500 200
=== Details ===
Capacity price
1 25
2 40
25 80
30 150
35 100
38 250
40 200
50 300
60 270
200 500

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.