Java test report (basic training, selection cycle, arrays, strings, and classes and objects)

table of Contents

A, Java basic training

Two, Java and cycle selection

Three, Java arrays practice

Four, Java classes and objects practice

Five, Java string practice 


A, Java basic training

1, a program written in Java, a double type Fahrenheit input from the keyboard, then convert it to an output Celsius.

package package1;
import java.util.*;
public class class1 {
public static void main(String[] args) {
// TODO Auto- generated method stub
Scanner in=new Scanner(System.in);
System.out.println("请输入华氏度: ");
double g=in.nextFloat();
double w=(5.0/9)*(g-32);
System.out.println(+g+"华氏度"+"对应的摄氏度为: "+w);
}
}

2, programming from the keyboard input values a, b, c, and calculates the following expression (square root with the Math.sqrt () method). 

package package1;
import java.util.*;
public class class2 {
public static void main(String[] args) {
// TODO Auto- generated method stub
Scanner.gsq=new Scanner(System.in) ;
System.out.println("Please input the number of a,b,c : ");
int a=gsq.nextInt();
int b=gsq.nextInt();
int c=gsq.nextInt();
double result=(((-1)*b)+Math.sqrt(b*b-4*a*c))/(2*a);
if(b*b-4*a*c<0){
System.out.println("the number doesn't exit");
}e1se{
System.out.println("The result is : "+result);
}
}
}

3. Write a program to read an integer 0-1000 calculate the sum of each digit. For example, the input 923, the output of each digit of the sum is 14.

package package1;
import java.util.*;
public class class3 {
public static void main(String[] args) {
// T0DO Auto- generated method stub
Scanner gg=new Scanner(System.in);
System.out.println("Please input the number between 0 and 1000: ");
int t=gg.nextInt();
while(t < 0 || t > 1000){
System.out.println("The number you input is invalid,please input again : ");
t=gg.nextInt();
int sum=0,i,a=t;
while(a!=0){
i=a%10;
a/=10;
sum+=i;
System.out.println(" the sum of”+t+”is "+sum);
}
}|

Two, Java and cycle selection

1, programming, user input from the keyboard requires a number of double type, an output of the integer part and a fractional part of the number.

package package2;
import java.util.*;
public class class1 {
public static void main(String[] args) {
System.out.println( "请输入一-个double数值: ");
Scanner gg =new Scanner(System.in);    
String a=gg.nextLine();
int tmp=a.index0f('.');
System.out.println( "整数部分为: "+a. substring(0, tmp));
System.out.println("小数部分为: "+a. substring(tmp+1));
}|

2, programming, a positive integer from keyboard input, judgment is odd or even.

package package2;
import java.util.*;    
public class class2 {
publ1ic static void main(String[] args) {
// TODO Auto- generated method stub
System.out.print1n("请输入一个整数: ");
System.out.println();
Scanner gg = new Scanner(System.in);
int number = gg.nextInt();
if( (number&1) == 1){
System.out.println("您输入的是奇数!");
}else{
System.out.println("您输入的是偶数!");
}
}|
    

3, programming, user 4 requires input from the keyboard positive integer, to find the maximum and minimum values, and wherein the printed output.

package package2; .
import java.util.*;
public class class3 {
public static void main(String[] args) {
int a[]=new int [4];
Scanner input = new Scanner(System.in);
System.out.println("Please input four numbers: ");
for (int i=0;i<4;i++)
a[i]=input.nextInt();
int min=a[0];
int max=a[0] ;
for int i=0; i<4;i++)
if (a[i]<min){
min=a[i];
}
if (a[i]>max){
max=a[i];
}
System.out.println("The max number is " +max);
System.out.println("The min number is " +min);
}
}

4, write a Java application, enter a number between 1-9999, determine whether the palindrome.

package package2;
import java.util.*;
public class class4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner gg=new Scanner(System.in);
System.out.println("请输入1~9999之间的数字: ");
int a=gg.nextInt();
int b=0,c=0,d=a;
while(a!=0){
c=a%10; .
b=b*10+c;
a=a/10;
}
if(b==d){
System.out.println("该数为回文数! ");
}
else{
System.out.println("该数不是回文数! ");
}

5, write a program to achieve the greatest common divisor program

package package2;
import java.util.*;
public class class5{
public static void main(String[] args) {
Scanner gg= new Scanner(System.in);
System.out.println("请输入两个整数: ");
int a=gg.nextInt();
int b=gg.nextInt();
for(int c=Math.max(a,b);c>=1;c--){
if(a%c==0 && b%c==0){
System.out.println(" 最大公约数为: "+c);
break ;
}

 6. Write a program that requires users to enter a month from the keyboard, enter the year is a leap year. Year in line with one of the following two conditions is the leap year; (divisible by four, but can not be divisible by 400 and divided by 100. )

package package2;
import java.util.*;
public class class6 {
public static void main(String[] args) {
Scanner gg = new Scanner(System.in);
System.out.println("请输入今夕是何年: ");
int y=gg.nextInt();
if((y%4==0 && y%100!=0) || y%400==0) {
System.out.println(y+"是闰年");
else
System.out.println(y+"不是闰年");
}
}

 

 7, programming, calculates and outputs between 1-1000 having 7 or an integer multiple of the number and sum of 7.

package package2;
import java.util.*;
public class class7 {
public static void main(String[ ] args) {
int s=0,c=0;
for(int i=1;i<=1000;i++){
if(i%7==0||i%10==7||(i/100)%10==7||(i/10)%10==7){
S+=i;  
C++;
System.out.println("1-1000之间含有7或者是7的倍数的整数之和及个数分别是"+s+","+c+"个");
}
}

 

Three, Java arrays practice

1, generating a random integer between 10 1-100. Deposited into an array. The maximum value of the output of the array, minimum, average, and all array elements.

package package4;
public class class1;
public static void main(String[] args) {
int [] a=new int [10];
int sum=0, max=0, min=101;
System.out.println("随机生成的十个数字为: ");
for(int i=0;i<10;i++) {
a[i]=(int)(Math.random()*100)+1;//随机生成10个1-100的数
System.out.print(+a[i]+"、");
for(int i=0;i<10;i++) {
sum+=a[i];
if(a[i]<min) //比较最小值
min=a[i];
else if(a[i]>max)
max=a[i]; //比较最大值
System.out.println(“该数组中最大值为" +max+" ,最小值为“+min+" ,数值总和为"+sum+" ,平均值为"+(sum)/10.0);
}
}

2, programming, define an array, and a randomly generated number between 0-100. Assigned to it, then the item is removed 0, the output of the new array.

package package4;
public class class3 {
public static void main(String[] args) {
// TODO Auto- generated method stub
int [] a=new int [7]; //定 义随机生成7个数
int k=0;
System.out.println("原来的数组为: ");
for(int i=0;i<a.length;i++) {
a[i]=(int) (Math. random()*100);
for(int i=0;i<a.length-k;i++) {
System.out.print(a[i]+" ");
}
for(int j=0;j<a.length;j++) {
if(a[j]==0) {
k++;
for(int t=j;t<a.length-1;t++)
a[t]=a[t+1];
}
System.out.println("\n重新生成的数组为: ");
for(int i=0;i<a.length-k;i++) {
System.out.print(a[i]+"“);
System.out.println("\n共去掉"+k+"个0值");
}
}

3, programming, perform the following functions:

1) Input to the array 20 integers:

Number 20 in ascending order, outputting the sorted array 2) method uses bubble:

3) Enter an integer X, the output binary search to find X. X index in an array with the array, the output can not find -1.

package package4;
import java.util.Scanner;
public class class2 {
public static void main(String[] args) {
int [] a=new int[20];//定 义数组存放20个数
int gg;
System.out.print1n("生成的数组为: ");
for(int i=0; i<a.length;i++) {
a[i]=(int)(Math.random()*100);//随机生成20个数
System.out.print(a[i]+" ");
//冒泡排序
for(int i=0; i<a.1ength-1;i++) {
for(int j=0;j<a.length-1-i;j++) {
if(a[j]>a[j+1])团
gg=a[j];
a[j]=a[j+1]; 
a[j+1]=gg;
}|
}
//输出排序后的数组
System.out.println("\n排序后的顺序: ");
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");

//二分法查找X
int p=0,1=a.length-1, k=0;
Scanner in=new Scanner(System. in);
System.out.println("\n请输入一个0~100的整数: ");
int t=in.nextInt();
while(1>=0) {
k=(p+1)/2;
if(t<a[k])
1=k-1;
else if(t==a[k]) {
System.out.println("该数所处的位置为: a["+k+"]");
break;
}else
p=k+1;
if(t!=a[k])
System.out.println( -1);
}
}

And detecting when running the program, and enter the number belonging to the array when the array does not belong:

Four, Java classes and objects practice

1, the definition of a class called Person, which contains a member variable of type String and a member variable of type int, methods are defined to access and modify the methods for these two variables. Additional methods defined for the class and then speak a name of the output value of its member variables. Draw UML class diagram, write a program to test all methods of the class.

 (1), UML is shown below:

(2), the preparation procedure is as follows:

package package3;
import java.util.*;
public class person {
String name;
int height;
public String getName() {
return name ;
}
public void setName(String name) {
this.name = name; .
}
public int getheight() {
return height;
}
public void setheight(int height) {
this.height = height;
public void set(String name, int height){
this.name=name;
this.height=height;
}
public void speak(){
System.out.print(name+" "+height);

public static void main(String[] args) {
person a=new person() ;
a.set("王帅",178);
a.speak();
}

 

2, the definition of a class named Circle represents circle defined area of ​​circular circumference and methods, define and modify access (using the constructor) for its member variables. Draw UML class diagram, write a program to test all methods of the class.

(1) shown in FIG UML as shown below:

(2) creation method first creates Circle class, code is shown below:

package package3;
public class Circle {
private double r;
public double getR() {
return r;
public void setR(double r) {
this.r = r;
}
public void setArea() {
System.out.println("面积为"+3.14*r*r);
public void setLength() {
System.out.println("周长为"+2*3.14*r);

(3) create a test output class to call the code as shown below:

package package3;
public class test {
public static void main(String[] args) {
Circle c=new Circle();
c.setR(2);
System.out.println("半径为"+c.getR());
c.setArea();
C.setLength();|
}
}

 (4) Run the program and Detection:   

3, showing the definition of a circle Circle class, method and variable construct a reasonable; Cylinder define a named class represents a cylinder, it inherits Circle class, a variable defined in claim height represents the height of the cylinder. Covering getArea () method Cylinder surface area defined getVolume () method Cylinder volume. Construction methods and constructors defined radius and height with two default parameters. Write test program prompts the user for the round base of the cylinder radius and height, the program creates a cylindrical object and outputs the calculated cylinder volume and surface area. UML class and its relationship with the picture.

     (1) shown in FIG UML:

      (2) preparation of parent Circle class, as follows:

package day1;

import java.util.Scanner;

public class Circle {
	double radius;
	public double area() {
		return radius*radius*3.14;	
	}
	public double getRadius() {
		return radius;
	}
	public void setRadius() {
		System.out.println("请输入底面面的半径:");
		Scanner sc =new Scanner(System.in);
		radius=sc.nextDouble();
		
	}

}

   (3) preparation of parent class subclasses inherit Cylinder Circle class, the specific code as shown below:

package day1;

import java.util.Scanner;

public class Cylinder extends Circle {
	double height;
	public double getLength() {
		return 2*3.14*radius;
		
	}
	public double getVolume() {
		return height*area();
		
	}
	public double getArea() {
		return 2*area()+height*getLength();
		
	}
	
	public double getHeight() {
		return height;
	}
	public void setHeight() {
		System.out.println("请输入圆柱的高:");
		Scanner sc =new Scanner(System.in);
		height=sc.nextDouble();
	}
	public static void main(String[] args) {
		Cylinder a=new Cylinder();
		a.setRadius();
		a.setHeight();
		System.out.println("圆柱的表面积为:"+a.getArea());
		System.out.println("圆柱的体积为:"+a.getVolume());
			}
}

    (4) Run the code and detection:

 

4, the use of inheritance and derived class students and teachers to manage files. Suppose you want to manage some of the data follows the following categories of personnel.

    teacher (teacher) class: name, sex, age, job title, as the curriculum;

    student (student) categories: name, sex, age, school, academic departments;

    gradstudent (graduate student) categories: name, sex, age, school, academic departments, tutor.

Each class is only required to set up the display constructors and member functions of the class object data. Write the main function, a description of the class object, and its simple test class member functions. UML class and its relationship with the picture.

(1) shown in FIG UML:

 (2) the preparation of the parent class Person class code as follows:

package day1;
public class Person {
	public String name;
	public String sex;
	public int age;
	public void showInfo() {
		System.out.println("姓名:"+name);
		System.out.println("性别:"+sex);
		System.out.println("年龄:"+age);
	}
}

(3) Preparation Teacher class inheritance class person, the code as shown below:

package day1;
public class Teacher extends Person{
	String name="王刚";
	String sex="男";
	int age=23;
	String job="班主任";
	String project="软件工程";
	public void showInfo() {		
		System.out.println("姓名:"+name);
		System.out.println("性别:"+sex);
		System.out.println("年龄:"+age);
		System.out.println("职称:"+job);
		System.out.println("担任课程:"+project);
	}	
public static void main(String[] args) {
	Teacher teach =new Teacher();
	teach.showInfo();	
}
}

(4) preparation of class inheritance Student Person class code is shown below:

package day1;
public class Student extends Person{
	String name="小明";
	String sex="男";
	int age=19;
	String number="20173344134";
	String department="物联网";
	public void showInfo() {
		System.out.println("姓名:"+name);
		System.out.println("性别:"+sex);
		System.out.println("年龄:"+age);
		System.out.println("学号:"+number);
		System.out.println("系别:"+department);
	}
		public static void main(String[] args) {
			Student stu =new Student();
			stu.showInfo();			
}
	}

(5) prepare Gradstudent Student class inheritance code as shown below:

package day1;
public class Gradstudent extends Student{
	String name="老王";
	String sex="男";
	int age=19;
	String number="20173344134";
	String department="物联网";
	String teacher="王刚";
	public void showInfo() {		
		System.out.println("姓名:"+name);
		System.out.println("性别:"+sex);
		System.out.println("年龄:"+age);
		System.out.println("学号:"+number);
		System.out.println("系别:"+department);
		System.out.println("导师:"+teacher);

}
	public static void main(String[] args) {	
		Gradstudent stu =new Gradstudent();
		stu.showInfo();			
}
}

(6) Detection and run the program:

5, design a car class that contains a representation of the member variable rate, indicating that the startup, acceleration, braking techniques, and then design a bus class inherits from automobiles, passengers expressed the need for variable number of members, in addition to the definition of passengers on the train and Get off method, preparation of test these two classes of use. (This procedure is relatively simple, mainly used to understand sub-classes and inheritance, methods, you can define your own!)

(1) preparation of Car parent class, method and variable definitions, as shown below:

package day1;
public class Car {
	public double speed;
	public void start() {
		System.out.println("车辆开始启动!");		
	}
    public void speedUp() {
    	System.out.println("车辆开始加速!");
	}
     public void stop() {
    	 System.out.println("车辆开始刹车!");
}	
}

(2) preparation of class inheritance publicCar Car, code is shown below:

package day1;
public class publicCar extends Car {
	int number;
	void getIn() {
		System.out.println("乘客上车!");
		
	}
	void getOff() {
		System.out.println("乘客下车!");
	}
	public static void main(String[] args) {
		publicCar c=new publicCar();
		c.start();
		c.speedUp();
		c.stop();
		c.getIn();
		c.getOff();}
}

(3) Run the program and Detection:

 

Five, Java string practice 

 1, programming, input word, each word input from small to large after output by the sequential ordering string

package test56;
import java.util.Scanner;
public class Wordsorts {
	 public static void main(String[] args) {
	      	char[] str=new char[100];
	        char temp;
	        String string= null;
	        Scanner sc=new Scanner(System.in);
	        System.out.println("输入一个字符串:");
	        string=sc.next();
	        str=string.toCharArray();
	            for(int i=0;i<string.length();i++)//排序
	            for(int j=string.length()-1;j>i;j--)
	            {
	                if(str[j]<=str[j-1])
	                {
	                    temp=str[j];
	                    str[j]=str[j-1];
	                    str[j-1]=temp;
	                }
	            }
	            System.out.println(str);
	            sc.close();
	    }
}

2, an encryption program write request from the keyboard to enter a character string, and then outputs the encrypted string. Encryption rule for each character string into the next letter represents original is converted into a b. C. original is converted to lower case B is converted to a lowercase, uppercase converted to uppercase A. Z

package test56;
import java.util.Scanner;
public class Password {
	public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String s = sc.nextLine();
        char[] ch = s.toCharArray();
        System.out.println("加密后的字符串为:");
        for (int i = 0; i < ch.length; i++) {
            if (ch[i] >= 'A' && ch[i] <= 'Y') {
                ch[i] = (char) (ch[i] + 1);
                System.out.print(ch[i]);
            } else if (ch[i] == 'Z') {
                System.out.print('A');
            }
            else if (ch[i] >= 'a' && ch[i] <= 'y') {
                ch[i] = (char) (ch[i] + 1);
                System.out.print(ch[i]);
            } else {
                System.out.print('a');
            }

        }
    }
}
Published 16 original articles · won praise 6 · views 8202

Guess you like

Origin blog.csdn.net/Until_U/article/details/104737632