「java写一个类并测试」java怎么定义测试类

博主:adminadmin 2022-12-20 19:30:10 81

今天给各位分享java写一个类并测试的知识,其中也会对java怎么定义测试类进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

用JAVA编写一个程序,要求如下:

实现代码如下:

Student类:

public class Student {

private String name;

private String sex;

private int age;

private double chinese;

private double math;

private double english;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public double getChinese() {

return chinese;

}

public void setChinese(double chinese) {

this.chinese = chinese;

}

public double getMath() {

return math;

}

public void setMath(double math) {

this.math = math;

}

public double getEnglish() {

return english;

}

public void setEnglish(double english) {

this.english = english;

}

}

-----------------------------------------------------------------

StudentTest类:(测试类)

import java.util.Scanner;

public class StudentTest {

public static void main(String[] args) {

Student student = new Student();

Scanner sc = new Scanner(System.in);

System.out.println("请输入姓名:");

student.setName(sc.next());

System.out.println("请输入性别:");

student.setSex(sc.next());

System.out.println("请输入年龄:");

student.setAge(sc.nextInt());

System.out.println("请输入语文成绩、数学成绩、英语成绩:");

student.setChinese(sc.nextDouble());

student.setMath(sc.nextDouble());

student.setEnglish(sc.nextDouble());

Double count = student.getChinese()+ student.getMath()+student.getEnglish();

System.out.println("姓名:"+student.getName()+" 性别:"+student.getSex()+" 年龄:"+student.getAge());

System.out.println("总分:"+count+" 平均分:"+count/3);

}

}

运行结果为:

Java设计一个圆类Circle,并测试,要求: Circle类中有两个成员方法,一个求圆的周长,

public class CircleTest {

public static void main(String[] args) {

double radius = 10;

Circle circle = new Circle();

circle.setRadius(radius);

System.out.println("半径" + radius + "的圆,周长: " + circle.perimeter() + ", 面积: " + circle.area());

double radius2 = 11;

Circle circle2 = new Circle(radius2);

System.out.println("半径" + radius2 + "的圆,周长: " + circle2.perimeter() + ", 面积: " + circle2.area());

}

}

class Circle {

private double radius;

public Circle() {

}

public Circle(double radius) {

this.radius = radius;

}

public double area() {

return Math.PI * Math.pow(this.radius, 2);

}

public double perimeter() {

return Math.PI * this.radius * 2;

}

public double getRadius() {

return radius;

}

public void setRadius(double radius) {

this.radius = radius;

}

}

编写Java程序,定义一个学生类,并编写主类测试该类的功能。基本要求如下:

代码已经编译通过

import java.util.*;

public class MainClass_StudentInfor{

public static void main(String args[]){

boolean e=true;

Student stu=new Student();

Scanner sc=new Scanner(System.in);

while(e){

System.out.println("\t\t\t\t\t*********欢迎进入学生信息系统*********\t\t\t\t\t");

System.out.println("\t\t\t\t\t********** 1:录入学生的信息********\t\t\t\t\t");

System.out.println("\t\t\t\t\t***********2:输出信息**************\t\t\t\t\t");

System.out.println("\t\t\t\t\t***********3:求总分****************\t\t\t\t\t");

System.out.println("\t\t\t\t\t***********4求均分*****************\t\t\t\t\t");

System.out.println("\t\t\t\t\t***********5:成绩排序**************\t\t\t\t\t");

System.out.println("\t\t\t\t\t***********6:检索成绩**************\t\t\t\t\t");

System.out.println("\t\t\t\t\t***********7:求最大值和最小值**************\t\t\t\t\t");

System.out.println("\t\t\t\t\t***********8:结束程序 ***************\t\t\t\t\t");

try{

byte b=sc.nextByte();

switch(b){

case 1:

stu.inputInfo();break;

case 3:

stu.getSum();break;

case 4:

stu.getAver();break;

case 2:

stu.printInfo();break;

case 5:

stu.sort();break;

case 6:

stu.index();break;

case 7:

stu.indexBL();break;

case 8:

e=false;

}

}

catch(NullPointerException f){

f.printStackTrace();

System.out.println("请正确操作");

}

}

}

}

enum Sex{

男,女

}

class Student{

int number;

Sex a;

String name;

float sum=0;

float average;

float score[];

String subject[]={"高数","英语","C语言"};

void inputInfo(){//录入学生基本信息

Scanner sc=new Scanner(System.in);

System.out.println("请输入学号 :");

number=sc.nextInt();

System.out.println("请输入姓名 :");

name=sc.next();

System.out.println(" 请输入性别 1:男,2:女");

int x=sc.nextInt();

if(x==1)

{

a=Sex.男;

}

else

{

a=Sex.女;

}

System.out.println("请输入高数,英语,c语言的成绩:");

score=new float[3];

for(int i=0;i=2;i++)

{

score[i]=sc.nextFloat();

System.out.println(subject[i]+":"+score[i]);

}

}

void printInfo(){//输出学生基本信息

System.out.println("学号:"+number);

System.out.println("姓名:"+name);

System.out.println("性别:"+a);

for(int i=0;i3;i++){

System.out.println(subject[i]+":"+score[i]);

}

}

void getSum(){

for(int i=0;i=2;i++)

{

sum=sum+score[i];

}

System.out.println(" 学生的总分:"+sum);

}

void getAver(){

for(int i=0;i=2;i++){

sum=sum+score[i];

}

average=sum/3;

System.out.println(" 学生的平均成绩:"+average);

}

void sort(){//排序

Arrays.sort(score);

System.out.println("学生的成绩排序为:"+Arrays.toString(score));

}

void index(){//查询成绩

boolean n=true;

while(n){

Scanner sc=new Scanner(System.in);

System.out.println("请输入,1检索开始,2检索结束");

int m=sc.nextInt();

switch(m){

case 1:

System.out.println("请输入您要查找那门课的成绩0:高数;1:英语:2:C语言");

Scanner sc1=new Scanner(System.in);

int c=sc1.nextInt();

System.out.println(subject[c]+score[c]);

System.out.println("您是否还要查找其他科目的成绩");

break;

case 2:

n=false;

}

}

}

void indexBL(){

float biggest=0;

float lowest=100;

for(int i=0;i3;i++){

if(biggest=score[i]){

biggest=score[i];

}

if(lowest=score[i]){

lowest=score[i];

}

}

System.out.println("该生的最高成绩是"+biggest);

System.out.println("该生的最低成绩是"+lowest);

}

}

(求助)JAVA编写类与类的测试程序

--------------------------DateUtil类

/**

* @author 王炳焱

* 创建日期:2007-5-21

* 项目名称:te

* 文件名称:DateUtil

*/

package test0521;

/**

* @author Administrator

*

*/

public class DateUtil {

public int year;// 用于表示年份

public int month;// 用于表示月份

public int day;// 用于表示天数

private boolean isLeapYear = false;// 用于表示是否为闰年.初始值为false

DateUtil() {

}

DateUtil(int year, int month) {// 用于初始化year变量和month变量

this.year = year;

this.month = month;

}

public boolean isLeapYear() {// 用于判断年份是否为闰年

this.isLeapYear = (this.year % 4 == 0 this.year % 100 != 0)

|| (this.year % 400 == 0);//闰年条件

return this.isLeapYear;

}

public int getMonthDays() {// 用于计算并取得该月的天数

int[] bigMonth = { 1, 3, 5, 7, 8, 10, 12 };//31天的月份

for (int i = 0; i bigMonth.length; i++) {

if (this.month == bigMonth[i]) {

this.day = 31;

return this.day;

}

}

if (this.month == 2 this.isLeapYear()) {//闰年2月

this.day = 29;

return this.day;

}

if (this.month == 2) {//平年2月

this.day = 28;

return this.day;

}

this.day = 30;//除去31天的月份和2月,其他月份都为30天

return this.day;

}

}

---------------Test类

/**

* @author 王炳焱

* 创建日期:2007-5-21

* 项目名称:te

* 文件名称:Test

*/

package test0521;

/**

* @author Administrator

*

*/

public class Test {

/**

* @param args

*/

public static void main(String[] args) {

// TODO 自动生成方法存根

DateUtil dateUtil = new DateUtil(2008, 1);//输入年份与月份

String str = "平年";

if (dateUtil.isLeapYear()) {//根据判断闰年返回布尔值生成对应字符串

str = "闰年";

}

System.out.println(dateUtil.year + "年为"+str);

System.out.println(dateUtil.month + "月有" + dateUtil.getMonthDays()+ "天");

}

}

用java编写平面坐标类Point并测试

运行结果:

public class Pointtest {

public static void main(String[] args) {

Point point1 = new Point(0,0);

Point point2 = new Point(5,5);

Point point3 = new Point(5,5);

System.out.println(point1.equals(point2));

System.out.println(point2.equals(point3));

System.out.println(Point.distance(point1,point2));

System.out.println(Point.distance(point1,5,5));

System.out.println(Point.distance(0,0,5,5));

}

}

class Point{

private  int x;

private  int y;

public Point() {

super();

// TODO Auto-generated constructor stub

}

public Point(int x, int y) {

super();

this.x = x;

this.y = y;

}

public int getX() {

return x;

}

public void setX(int x) {

this.x = x;

}

public int getY() {

return y;

}

public void setY(int y) {

this.y = y;

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + x;

result = prime * result + y;

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Point other = (Point) obj;

if (x != other.x)

return false;

if (y != other.y)

return false;

return true;

}

public String toString() {

return "Point [x=" + x + ", y=" + y + "]";

}

public static double distance(int x, int y,int a,int b) {

return Math.sqrt((Math.pow((x-a), 2)+Math.pow((y-b), 2)));

}

public static double distance(Point p,int a,int b) {

return Math.sqrt((Math.pow((p.getX()-a), 2)+Math.pow((p.getY()-b), 2)));

}

public static double distance(Point a,Point b) {

return Math.sqrt((Math.pow((a.getX()-b.getX()), 2)+Math.pow((a.getY()-b.getY()), 2)));

}

}

java写一个类并测试的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java怎么定义测试类、java写一个类并测试的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-20,除非注明,否则均为首码项目网原创文章,转载请注明出处。