Java lab test questions in June 17

Source: Internet
Author: User

/Files/Wangs/June 17 test. Doc

 

1

ClassFushu

{

 IntA; // Real-Time

 IntB; // virtual part

PublicFushu (IntX,IntY)

{

This. A = X;

This. B = y;

}

PublicFushu add (fushu Add1)

{

IntX = add1.a +This.;

IntY = add1. B +This. B;

Return NewFushu (x, y );

}

PublicFushu plus (fushu sub1)

{

IntX =This. A-sub1.a;

IntY =This. B-sub1. B;

Return NewFushu (x, y );

 

}

 

PublicString tostring (){

String RT = "";

 If(B> 0)

RT = "(" + A + "+" + B + "I" + ")";

 If(B = 0)

RT = "(" + A + ")";

 If(B <0)

RT = "(" + A + B + "I" + ")";

 ReturnRT;

}

 

Public Static VoidMain (string [] ARGs)

{

Fushu fushu1 =NewFushu (3, 4 );

Fushu fushu2 =NewFushu (2,-13 );

Fushu result = fushu1.add (fushu2 );

System.Out. Println (result );

// System. Out.Println("Result =" + result. A + "+" + result. B + "I ");

}

}

 

2. // interfacetest. Java

// Main class interfacetest

Public class interfacetest {

Public static void main (string ARGs []) {

Myline line = new myline (15 );

Mycircle Circle = new mycircle (10 );

Myrect rect = new myrect (10, 20 );

Shape shapes [] = new shape [3];

Shapes [0] = line;

Shapes [1] = circle;

Shapes [2] = rect;

String output =

Line. tostring () + "\ n"

+ Circle. tostring () + "\ n" + rect. tostring ();

For (INT I = 0; I <shapes. length; I ++ ){

Output + = "\ n" +

"" + Shapes [I]. tostring () +

"\ Narea =" +

Shapes [I]. Length ();

}

System. Out. println ();

System. Out. println (output );

}

}

// Define the Interface Shape

Interface Shape {

// Calculate the length

Public abstract double length ();

 

}

// Define a Class Point

Class myline

Implements shape {

Private int length;

Public myline (INT length ){

This. Length = length;

}

 

Public String tostring (){

Return "this is myline ";

}

Public double length (){

Return length;

}

 

}

Class mycircle

Implements shape {

Protected double radius;

 

Public mycircle (double circleradius ){

This. radius = circleradius;

}

 

Public double length (){

Return 2 * Math. Pi * radius;

}

Public String tostring (){

Return "this is a circle ";

}

 

}

Class myrect

Implements shape {

Protected int height;

Protected int width;

 

Public myrect (INT height, int width ){

This. Height = height;

This. width = width;

 

}

Public double length (){

Return 2 * (height + width );

}

 

Public String tostring (){

Return "this is a rectangle ";

}

}

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.