Java language Programming Midterm exam questions

Source: Internet
Author: User
Tags define abstract

Java language Program Design midterm exam question One, programming question 1. Abstract class

Create a closed Graph abstract class (closedfigure), define an abstract method to calculate the area, create a two subclass ellipse (Ellipse) and Rectangle (Rectangle) that inherits the plane graph abstract class, and implement the method of calculating the plot area in the subclass. The design test class Test1 carries on the corresponding test, according to the different kind of plane graph to carry on the corresponding area computation. (30 points)
Tip: According to the first definition of ellipse, a is used to denote the length of the ellipse's long half axis, B represents the length of the elliptic short half axis, and a>b>0, the ellipse area formula: L=πab

2. Interface

Create interfaces Speakable and Runer, and then create two classes sheep and person implementations of the methods inside the two interfaces. Define the characteristics of the sheep in the class sheep (Baa bark, run with limbs), in the class person define the characteristics of people (meet all say hello, run with legs), design test class Test2 to carry out the corresponding test. (30 points)
Tip: Print directly on the feature.

3. Exceptions

Add a constructor method to the plural class that has a parameter of type string (in the form a+bi, such as "3.2+4.5i"). The string is processed, the real and imaginary substrings are extracted, and then converted to floating-point numbers, and assigned to the corresponding member variables. When the contents of the real and imaginary sub-strings cannot be converted to floating-point numbers, the numeric format exception NumberFormatException is thrown. In the main method, get the complex string (such as 3.2+4.5i) that the user entered from the console, call the constructor above to build the complex number object, and perform the necessary exception handling. (20 points)
Tip: The code to get user input from the console is referenced below:
Scanner input = new Scanner (system.in);
String str = Input.next ();
Note: Using the scanner class requires importing the corresponding class in the class library with the following code:
Import Java.util.Scanner;

4. String

Known string "I am a student in my university"
Perform the following actions as required (20 points):
(1) Copy this string to a character array char[] Str
(2) Count the number of occurrences of the letter I in the string;
(3) Remove the substring "my";
(4) Converting this string to an array of strings requires that each array element be a meaningful English word and output to the console.

Ii. Results of operation (1) Procedure 1 operation result

(2) Program 2 operation result

(3) Program 3 operation result

(4) Program 4 operation result

Third, source code (1) Program 1 source code
Package Task1;import Java.util.scanner;import Java.text.DecimalFormat;//Closed Graphic abstract class (Closedfigure)AbstractClass Closedfigure {//define abstract methods to calculate area    Abstract DoubleGetarea ();}//define ellipse (Ellipse) class inheritance inheritance plane graphics abstract class (Closedfigure)Class Ellipse extends Closedfigure {//define member variables    DoubleA, B;//define Construction methods     Public Ellipse(DoubleADoubleb) { This. A = A; This. B = b; }//Implement abstract methods to calculate area    //According to the first definition of ellipse, a is used to denote the length of the ellipse's long half axis, B represents the length of the elliptic short half axis, and a>b>0, the ellipse area formula: L=πab     Public Double Getarea() {DoubleL = Math.PI * A * b;returnL }}//define Rectangle (Rectangle) class inherits inheritance plane graphics abstract class (Closedfigure)Class Rectangle extends Closedfigure {//define member variables    DoubleA, B;//define Construction methods     Public Rectangle(DoubleADoubleb) { This. A = A; This. B = b; }//Implement abstract methods to calculate area     Public Double Getarea() {Doubles = A * b;returnS }}//Design test class Test1 to carry out the corresponding test, according to different types of plane graphics for the corresponding area calculation.  Public classTest1 { Public Static void Main(string[] args) {System. out. println ("Welcome to Sunny's Shop ~ O (≧v≦) o~~"); System. out. println ("Here you can calculate the area of the ellipse and the rectangle."); Scanner sc =NewScanner (System.inch); DecimalFormat DF =NewDecimalFormat ("0.00"); System. out. println ("To start, press 1, exit please press 0:");//SET flag variable, judge calculation start and end of calculation        inttemp = Sc.nextint ();if(temp = =0) {System. out. println ("Welcome to (≧▽≦)."); } while(temp = =1) {//Set the flag variable to determine if the ellipse's long and short half-axis inputs meet the requirements            intTemp1 =1; while(Temp1 = =1) {System. out. println ("Please enter the long and short half axes of the ellipse:");DoubleAell = Sc.nextdouble ();DoubleBEll = Sc.nextdouble ();if(Aell <=0|| BEll <=0) {System. out. println ("Long half shaft, short half axle must be greater than 0!" "); System. out. println ("Please re-enter:"); Temp1 =1; }Else if(Aell < BELL) {System. out. println ("The long half axle must be greater than the short half shaft!" "); System. out. println ("Please re-enter:"); Temp1 =1; }Else{Ellipse E =NewEllipse (Aell, BELL); System. out. println ("The area of the ellipse is:"+ Df.format ((E.getarea ())); Temp1 =0; }            }//Set the flag variable to determine if the length and width of the rectangle's input meet the requirements            intTemp2 =1; while(Temp2 = =1) {System. out. println ("Please enter the length and width of the rectangle:");DoubleAREC = Sc.nextdouble ();DoubleBrec = Sc.nextdouble ();if(Arec <=0|| Brec <=0) {System. out. println ("The length and width of the rectangle must be greater than 0!" "); System. out. println ("Please re-enter:"); Temp2 =1; }Else{Rectangle R =NewRectangle (Arec, Brec); System. out. println ("The area of the ellipse is:"+ Df.format ((R.getarea ())); Temp2 =0; }} System. out. println ("continue, press 1, exit please press 0:"); temp = Sc.nextint ();if(temp = =0) {System. out. println ("Welcome to (≧▽≦)."); Break; }        }    }}
(2) Program 2 source code
Package Task1;//Create interface speakable interface speakable {    //Define abstract method speak ()    voidSpeak ();}//Create interface Runer interface runer {    //Define abstract method run ()    voidRun ();}//Then create two classes sheep and person to implement the methods inside these two interfaces.  class Sheep implements speakable, runer {String name ="Sheep";//Implement abstract method speak ()     Public voidSpeak () {System.out.println (" a very cozy and cozy"); }//Implement abstract method run ()     Public voidRun () {System.out.println ("Run with your limbs."); }}//Then create two classes sheep and person to implement the methods inside these two interfaces class  person implements speakable, runer {String name ="People";//Implement abstract method speak ()     Public voidSpeak () {System.out.println ("Hello to all."); }//Implement abstract method run ()     Public voidRun () {System.out.println ("Running with two legs"); }} Public  class Test2 {     Public Static voidMain (string[] args) {Sheep Sheep =NewSheep ();        System.out.println (Sheep.name);        Sheep.speak ();        Sheep.run ();        System.out.println (); Person person =NewPerson ();        System.out.println (Person.name);        Person.speak ();    Person.run (); }}
(3) Program 3 source code
Package Cn.itcast.test;import Java.util.Scanner;//Import Scanner class//define plural classesClass Complex { Public DoubleReal//plural real part     Public DoubleIm//plural imaginary part    //Adds a constructor method that has a parameter of type string and throws an exception using the throws keyword declaration     Public Complex(String complex) throws NumberFormatException {string real, im;//extraction of real and imaginary sub-strings        if(Complex.indexof ("+") != -1) {real = complex.substring (0, Complex.indexof ("+")); im = Complex.substring (Complex.indexof ("+") +1, Complex.indexof ("I")); This. im = Double.parsedouble (IM); }Else{real = complex.substring (0, Complex.indexof ("-")); im = Complex.substring (Complex.indexof ("-") +1, Complex.indexof ("I")); This. im =-(Double.parsedouble (IM)); }//Real part converted to floating point number         This. Real = double.parsedouble (real); }//define Getreal () method to get the real part     Public Double Getreal() {returnReal }//define Getreal () method to get imaginary part     Public Double Getim() {returnIm }}//define TEST3 test Class 3 Public classTEST3 { Public Static void Main(string[] args) {Scanner input =NewScanner (System.inch); System. out. println ("Please enter the plural in a+bi format:"); while(true) {String str = input.next ();///The following code defines a Try...catch statement for catching exceptions            Try{//Instantiate object A of plural classComplex A =NewComplex (str);//outputs the real and imaginary parts of the complex number, respectivelySystem. out. println ("The real part of the complex number is:"+ a.getreal ()); System. out. println ("The imaginary part of the complex number is:"+ A.getim ()); Break; }Catch(NumberFormatException e) {System. out. println ("Numeric format exception!" Please re-enter: ");//Print the captured exception information}Catch(Exception e)            {E.printstacktrace (); }        }    }}
(4) Program 4 source code
Package Task1; Public classTest4 { Public Static void Main(string[] args) {String S1 ="I am a student in my university";//(1) Copy this string to a character array char[] str and output        Char[] str = S1.tochararray (); System. out. println ("(1) copy this string into a character array char[] str and output:"); for(intA =0; A < Str.length; a++) {System. out. Print (Str[a]); } System. out. println ("\ n");//(2) count the number of occurrences of the letter I in the string;        intn =0; for(intA =0; A < Str.length; a++) {if(' I '= = Str[a]) {n++; }} System. out. println ("(2) The number of occurrences of the letter I in the string:"+ N); System. out. println ("\ n");//(3) Remove substring "my";System. out. println ("(3) Remove substring" my "); System. out. println (S1.substring (S1.indexof ("My"), S1.indexof ("My")                +"My". Length ())); System. out. println ("\ n");//(4) converts this string into an array of strings, requiring each array element to be a meaningful English word and outputting it to the console. string[] S3 = S1.split (" "); System. out. println ("(4) after converting this string into a string array, the array element is:"); for(intA =0; A < S3.length; a++) {if(a==s3.length-1) {System. out. Print (S3[a]); }ElseSystem. out. print (S3[a] +","); }    }}

Java language Programming Midterm exam questions

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.