Java (examples in classes and objects)

Source: Internet
Author: User

First, the parameter in the method is a numeric type (int)
ImportJava.util.Scanner; Public classScorecalc {Public   void calc (int num1,int num2,int  num3) {        intsum = num1 + num2 +num3; DoubleAVG = SUM/3; System.out.println ("Total:" +sum); System.out.println ("Average score:" +avg); }     Public Static voidMain (string[] args) {Scanner input=NewScanner (system.in); System.out.println ("Please enter the results of Java:"); intNUM1 =Input.nextint (); System.out.println ("Please enter C's score:"); intnum2 =Input.nextint (); System.out.println ("Please enter the score for DB:"); intNUM3 =Input.nextint (); Scorecalc Calc = new  Scorecalc (); Calc.calc (NUM1, num2, num3);         
Input.close (); }}
Operation diagram:

The parameter in the method is a string array (string[])
Import java.util.Arrays;  Public class Sortname {    public   string[] Sortname (string[] unorderarr){        Arrays.sort ( Unorderarr);         return Unorderarr;}    }
Importjava.util.Arrays;ImportJava.util.Scanner; Public classSortnametest { Public Static voidMain (string[] args) {String name[]=NewString[5]; Scanner Scanner=NewScanner (system.in); System.out.println ("Please enter the Participant list:");  for(inti = 0; I < 5; i++) {Name[i]=Scanner.next (); } sortname Sortname = new  sortname (); string[] Orderarr         = sortname.sortname (name);        System.out.println (arrays.tostring (Orderarr));    Scanner.close (); }}       
Operation diagram:

The parameters in the method are objects (Student Student)
 public  class   Student { double      Chinese;     double   math;     double   中文版;  public  Student (double  chinese,< Span style= "COLOR: #0000ff" >double  math,double   中文版)        { this . Chinese = Chinese;         this . Math = math;     this . 中文版 = 中文版; }}
 Public class Studentcalc {    publicdouble    Calc (Student Zhangsan){         Double avg = (Zhangsan.chinese + zhangsan.math + zhangsan.english)/3;         return avg;    }}
ImportJava.util.Scanner; Public classStudenttest { Public Static voidMain (string[] args) {Scanner Scanner=NewScanner (system.in); System.out.println ("Please input language score:"); DoubleChinese =scanner.nextdouble (); System.out.println ("Please enter Math score:"); DoubleMath =scanner.nextdouble (); System.out.println ("Please input English score:"); Double中文版 =scanner.nextdouble (); Student Zhangsan = new  Student (Chinese, math,english); Studentcalc scoreavg         = new  Studentcalc ();  double avg = Scoreavg.calc (Zhangsan); System.out.println ("The average score for each class is:" +avg);    Scanner.close (); }}
Operation diagram:

The parameter in the method is an array of objects (student[] s)
 public  class      Student {String name;     int   age;     double   height;  public  Student (String name,int  Age , double   height) { this . Name = name;         this . Age = age;     this . Height = height; }}
 Public class Studentmanager {    publicdouble    heightavg (student[] s){         double sum = 0;          for (int i = 0; i < s.length; i++) {            + = s[i].height        ;        } return sum/s.length;    }}
ImportJava.util.Scanner; Public classStudentmanagertest { Public Static voidMain (string[] args) {Scanner Scanner=NewScanner (system.in); Student students[]=NewStudent[2];  for(inti = 0; i < students.length; i++) {System.out.println ("Please enter the" + (i+1) + "Participant's name:"); String name=Scanner.next (); System.out.println ("Please enter the" + (i+1) + "age of each learner:"); intAge =Scanner.nextint (); System.out.println ("Please enter the" + (i+1) + "Height of the trainees:"); DoubleHeight =scanner.nextdouble (); Student Student=NewStudent (name,age,height); Students[i]=student; } Studentmanager Manager = new  Studentmanager ();  double avg = Manager.heightavg (students); System.out.println ("Average height:" +avg);    Scanner.close (); }}
Operation diagram:

Java (examples in classes and objects)

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.