java实验报告附代码,java实验报告格式

java实验报告,请高手帮忙!

package test.baiduKnow.java实验报告;

创新互联专注于茫崖网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供茫崖营销型网站建设,茫崖网站制作、茫崖网页设计、茫崖网站官网定制、微信平台小程序开发服务,打造茫崖网络公司原创品牌,更为您提供茫崖网站排名全网营销落地服务。

//AreaAndLength.java

class Trangle {

double sideA, sideB, sideC, area, length;

boolean boo;

public Trangle(double a, double b, double c) {

// 【代码1】 // 参数a,b,c分别赋值给sideA,sideB,sideC

sideA = a;

sideB = b;

sideC = c;

if (a + b c)

// 【代码2】) // a,b,c构成三角形的条件表达式

{

// 【代码3】 // 给boo赋值。}

boo = true;

} else {

// 【代码4】 // 给boo赋值。}

boo = false;

}

}

double getLength() {

/*

* 【代码5】 方法体,要求进行判断,如果能构成三角形,则计算出length的值并返回

* 如果不能构成三角形,则提示“不是一个三角形,不能计算周长”

*/

if (boo == true) {

length = sideA + sideB + sideC;

} else {

length = 0;

System.out.println("不是一个三角形,不能计算周长");

}

return length;

}

public double getArea() {

if (boo) {

double p = (sideA + sideB + sideC) / 2.0;

area = Math.sqrt(p * (p - sideA) * (p - sideB) * (p - sideC));

return area;

} else {

System.out.println("不是一个三角形,不能计算面积");

return 0;

}

}

public void setABC(double a, double b, double c) {

// 【代码6】 // 参数a,b,c分别赋值给sideA,sideB,sideC

sideA = a;

sideB = b;

sideC = c;

if (a + b c)

// 【代码2】) // a,b,c构成三角形的条件表达式

{

// 【代码3】 // 给boo赋值。}

boo = true;

} else {

// 【代码4】 // 给boo赋值。}

boo = false;

}

}

}

class Lader {

double above, bottom, height, area;

Lader(double a, double b, double h) {

// 【代码10】 // 方法体,将参数a,b,c分别赋值给above,bottom,height

above = a;

bottom = b;

height = h;

}

double getArea() {

// 【代码11】 // 方法体,,要求计算出area返回

area = 0.5 * (above + bottom) * height;

return area;

}

}

class Circle {

double radius, area;

Circle(double r) {

// 【代码12】 // 方法体

radius = r;

}

double getArea() {

// 【代码13】 // 方法体,要求计算出area返回

return Math.PI * radius * radius;

}

double getLength() {

// 【代码14】 // getArea方法体的代码,要求计算出length返回

return 2 * Math.PI * radius;

}

void setRadius(double newRadius) {

radius = newRadius;

}

double getRadius() {

return radius;

}

}

public class AreaAndLength {

public static void main(String args[]) {

double length, area;

Circle circle = null;

Trangle trangle;

Lader lader;

circle = new Circle(1);

trangle = new Trangle(3, 4, 5);

lader = new Lader(4, 5, 3);

// 【代码15】 // 创建对象circle

// 【代码16】 // 创建对象trangle。

// 【代码17】 // 创建对象lader

// 【代码18】 // circle调用方法返回周长并赋值给length

length = circle.getLength();

System.out.println("圆的周长:" + length);

// 【代码19】 // circle调用方法返回面积并赋值给area

area = circle.getArea();

System.out.println("圆的面积:" + area);

// 【代码20】 // trangle调用方法返回周长并赋值给length

length = trangle.getLength();

System.out.println("三角形的周长:" + length);

// 【代码21】 // trangle调用方法返回面积并赋值给area

area = trangle.getArea();

System.out.println("三角形的面积:" + area);

// 【代码22】 // lader调用方法返回面积并赋值给area

area = lader.getArea();

System.out.println("梯形的面积:" + area);

// 【代码23】 // trangle调用方法设置三个边,要求将三个边修改为12,34,1。

// 【代码24】 // trangle调用方法返回面积并赋值给area

area = trangle.getArea();

System.out.println("三角形的面积:" + area);

// 【代码25】 // trangle调用方法返回周长并赋值给length

area = trangle.getLength();

System.out.println("三角形的周长:" + length);

}

}

Java实验,代码怎么写?

Shape.java接口代码

public interface Shape {

public static final double PI = 3.14d;

public double area();

}

Circle.java圆类代码

public class Circle implements Shape {

private double radius;

public Circle(double radius) {

  this.radius = radius;

}

@Override

public double area() {

  return PI * this.radius * this.radius;

}

public double perimeter() {

  return 2 * PI * this.radius;

}

}

Cylinder.java圆柱体类代码

public class Cylinder extends Circle {

private double height;

public Cylinder(double radius, double height) {

  super(radius);

  this.height = height;

}

public double area() {

  return 2 * super.area() + super.perimeter() * this.height;

}

public double volume() {

  return super.area() * this.height;

}

}

X5_3_6.java主类代码

public class X5_3_6 {

public static void main(String[] args) {

  Circle cir1 = new Circle(5);

  System.out.println("圆的面积为:" + cir1.area());

  System.out.println("圆的周长为:" + cir1.perimeter());

  Cylinder cy1 = new Cylinder(10, 15);

  System.out.println("圆柱体的表面积为:" + cy1.area());

  System.out.println("圆柱体的体积为:" + cy1.volume());

}

}

上面是我写的代码,下图是执行结果,麻烦看一下,是否可以。

java实验报告实验过程和步骤怎么写

public interface Auth {

public void check(String name,String pwd,AuthResult result);

}

class AuthResult{

public String msg;// 结果信息

public boolean success;// 验证结果

public boolean isSuccess(){

return success;

}

public String getMsg(){

return msg;

}

}

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.util.HashMap;

import java.util.Map;

public class Auth1 implements Auth {

MapString, String data = new HashMapString, String();

@Override

public void check(String name,String pwd,AuthResult result) {

try {

// 加载用户名和密码

InputStream in = getClass().getResourceAsStream("Auth1.txt");

BufferedReader br = new BufferedReader(new InputStreamReader(in));

String temp = null;

while((temp = br.readLine()) != null){

data.put(temp.split(":")[0], temp.split(":")[1]);

}

if(data.get(name) == null){

result.success = false;

result.msg = "用户名错误";

return;

}

if(!pwd.equals(data.get(name))){

result.success = false;

result.msg = "密码错误";

return;

}

result.success = true;

result.msg = "auth success!";

} catch (Exception e) {

e.printStackTrace();

}

}

}

public class Auth2 implements Auth {

MapString, String data = new HashMapString, String();

@Override

public void check(String name,String pwd,AuthResult result) {

try {

// 加载用户名和密码

InputStream in = getClass().getResourceAsStream("Auth2.txt");

BufferedReader br = new BufferedReader(new InputStreamReader(in));

String temp = null;

int i = 1;

String temp1 = null;

while((temp = br.readLine()) != null){

if(i % 2 == 0){

data.put(temp1, temp);

} else {

temp1 = temp;

}

i++;

}

if(data.get(name) == null){

result.success = false;

result.msg = "用户名错误";

return;

}

if(!pwd.equals(data.get(name))){

result.success = false;

result.msg = "密码错误";

return;

}

result.success = true;

result.msg = "auth success!";

} catch (Exception e) {

e.printStackTrace();

}

}

}

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

Auth1 a1 = new Auth1();

Auth2 a2 = new Auth2();

Scanner sc = new Scanner(System.in);

String name = null;

String pwd = null;

System.out.println("enter name:");

name = sc.nextLine();

System.out.println("enter pwd:");

pwd = sc.nextLine();

AuthResult result = new AuthResult();

System.out.println("---Auth1---check");

a1.check(name,pwd,result);

if(result.isSuccess()){

System.out.println("auth success!");

} else {

System.out.println("auth fail! because " + result.getMsg());

}

System.out.println("---Auth2---check");

a2.check(name,pwd,result = new AuthResult());

if(result.isSuccess()){

System.out.println("auth success!");

} else {

System.out.println("auth fail! because " + result.getMsg());

}

}

}

文件与这些类同包下应该就能读到

Auth1.txt

name1:pwd1

name2:pwd2

Auth2.txt

name3

pwd3

name4

pwd4


文章名称:java实验报告附代码,java实验报告格式
网站URL:http://scjbc.cn/article/hcdphj.html

其他资讯