Learn Java-8 from scratch. Create the first object and the first object in java-8

Source: Internet
Author: User

Learn Java-8 from scratch. Create the first object and the first object in java-8

1. Create an object;
2. Use attributes to describe objects;
3. Determine the behavior of the object;
4. merge objects;
5. inherit from other objects;
6. Convert the object and other types of information.

 

Program NewRoot2: calculates the square root of the input number and outputs

 1 package com.jsample; 2  3 public class NewRoot2 { 4     public static void main(String[] args){ 5         int number = 100; 6         if (args.length > 0){ 7             number = Integer.parseInt(args[0]); 8         } 9         System.out.println("The square root of "10                 + number11                 + " is "12                 + Math.sqrt(number)13         );14     }
View Code

 

Output:

The square root of 169 is 13.0

 

Program ModemTester: test the functions of the following four classes

 1 package com.jsample; 2  3 public class ModemTester { 4     public static void main(String[] args){ 5         CableModem surfBoard = new CableModem(); 6         DslModem gateway = new DslModem(); 7         AcousticModem acoustic = new AcousticModem(); 8         surfBoard.speed = 500000; 9         gateway.speed = 400000;10         acoustic.speed = 300;11         System.out.println("Trying the cable modem:");12         surfBoard.displaySpeed();13         surfBoard.connect();14         System.out.println("Trying the DSL modem");15         gateway.displaySpeed();16         gateway.connect();17         System.out.println("Trying the acoustic modem");18         acoustic.displaySpeed();19         acoustic.connect();20         System.out.println("Closing all modems");21         surfBoard.disconnect();22         gateway.disconnect();23         acoustic.disconnect();24     }25 }
View Code

Subordinate class: Modem

 1 package com.jsample; 2  3 public class Modem { 4     int speed; 5  6     public void displaySpeed(){ 7         System.out.println("Speed: " + speed); 8     } 9 10     public void disconnect(){11         System.out.println("Disconnecting to the Internet");12     }13 }
View Code

 

Subordinate class: CableModem

 1 package com.jsample; 2  3 public class CableModem extends Modem{ 4     String method = "cable connecttion"; 5  6     public void connect(){ 7         System.out.println("Connecting to the Internet"); 8         System.out.println("Using a " + method); 9     }10 }
View Code

 

Subordinate class: DslModem

 1 package com.jsample; 2  3 public class DslModem extends Modem{ 4     String method = "DSL phone connection"; 5  6     public void connect(){ 7         System.out.println("Connecting to the Internet"); 8         System.out.println("Using a " + method); 9     }10 }
View Code

 

Subordinate class: AcousticModem

 1 package com.jsample; 2  3 public class AcousticModem extends Modem{ 4     String method = "acoustic connection"; 5  6     public void connect(){ 7         System.out.println("Connecting to the Internet"); 8         System.out.println("Using a " + method); 9     }10 }
View Code

 

Output:
Trying the cable modem:
Speed: 500000
Connecting to the Internet
Using a cable connecttion
Trying the DSL modem
Speed: 400000
Connecting to the Internet
Using a DSL phone connection
Trying the acoustic modem
Speed: 300
Connecting to the Internet
Using a acoustic connection
Closing all modems
Disconnecting to the Internet
Disconnecting to the Internet
Disconnecting to the Internet

 

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.