求java大神帮忙写下java程序设计的实验报告中的程序 一些语句 谢了

求java大神帮忙写下java程序设计的实验报告中的程序 一些语句 谢了,第1张

import javaioBufferedReader;

import javaioIOException;

import javaioInputStreamReader;

public class TestString {

public static void main(String arg[]) throws IOException {

int a,n;

Systemoutprintln("请输入a,n:");

BufferedReader buf=new BufferedReader(new InputStreamReader(Systemin));

String readStr=bufreadLine();

String argStr[]=readStrsplit(",");

a=IntegerparseInt(argStr[0]);

n=IntegerparseInt(argStr[1]);

long sum=0,aaa=0;

for (int i=1;i<=n;i++){

aaa=(long)getaaa(a,i);

sum+=aaa;

Systemoutprint(aaa);

if (i==n) Systemoutprint("=");

else Systemoutprint("+");

}

Systemoutprintln(""+sum);

}

static double getaaa(int a,int n){

double aaa=a;

for(double i=1;i<n;i++){

aaa+=aMathpow(10, i);

}

return aaa;

}

}

需要注意的是这里面引入了几个java包。

第2个:http://zhidaobaiducom/question/198168341html&__bd_tkn__=6ef56e5d3a2499274a6df875f5b773e682519fb8a07960d151bbad4928e085c42f309a67e9e08f6c24eb7e11a1fbf820dff421b52a

package testbaiduKnowjava实验报告;

//AreaAndLengthjava

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;

Systemoutprintln("不是一个三角形,不能计算周长");

}

return length;

}

public double getArea() {

if (boo) {

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

area = Mathsqrt(p (p - sideA) (p - sideB) (p - sideC));

return area;

} else {

Systemoutprintln("不是一个三角形,不能计算面积");

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 = 05 (above + bottom) height;

return area;

}

}

class Circle {

double radius, area;

Circle(double r) {

// 代码12 // 方法体

radius = r;

}

double getArea() {

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

return MathPI radius radius;

}

double getLength() {

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

return 2 MathPI 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 = circlegetLength();

Systemoutprintln("圆的周长:" + length);

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

area = circlegetArea();

Systemoutprintln("圆的面积:" + area);

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

length = tranglegetLength();

Systemoutprintln("三角形的周长:" + length);

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

area = tranglegetArea();

Systemoutprintln("三角形的面积:" + area);

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

area = ladergetArea();

Systemoutprintln("梯形的面积:" + area);

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

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

area = tranglegetArea();

Systemoutprintln("三角形的面积:" + area);

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

area = tranglegetLength();

Systemoutprintln("三角形的周长:" + length);

}

}

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 javaioBufferedReader;

import javaioInputStream;

import javaioInputStreamReader;

import javautilHashMap;

import javautilMap;

public class Auth1 implements Auth {

Map<String, String> data = new HashMap<String, String>();

@Override

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

try {

// 加载用户名和密码

InputStream in = getClass()getResourceAsStream("Auth1txt");

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

String temp = null;

while((temp = brreadLine()) != null){

dataput(tempsplit(":")[0], tempsplit(":")[1]);

}

if(dataget(name) == null){

resultsuccess = false;

resultmsg = "用户名错误";

return;

}

if(!pwdequals(dataget(name))){

resultsuccess = false;

resultmsg = "密码错误";

return;

}

resultsuccess = true;

resultmsg = "auth success!";

} catch (Exception e) {

eprintStackTrace();

}

}

}

public class Auth2 implements Auth {

Map<String, String> data = new HashMap<String, String>();

@Override

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

try {

// 加载用户名和密码

InputStream in = getClass()getResourceAsStream("Auth2txt");

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

String temp = null;

int i = 1;

String temp1 = null;

while((temp = brreadLine()) != null){

if(i % 2 == 0){

dataput(temp1, temp);

} else {

temp1 = temp;

}

i++;

}

if(dataget(name) == null){

resultsuccess = false;

resultmsg = "用户名错误";

return;

}

if(!pwdequals(dataget(name))){

resultsuccess = false;

resultmsg = "密码错误";

return;

}

resultsuccess = true;

resultmsg = "auth success!";

} catch (Exception e) {

eprintStackTrace();

}

}

}

import javautilScanner;

public class Test {

public static void main(String[] args) {

Auth1 a1 = new Auth1();

Auth2 a2 = new Auth2();

Scanner sc = new Scanner(Systemin);

String name = null;

String pwd = null;

Systemoutprintln("enter name:");

name = scnextLine();

Systemoutprintln("enter pwd:");

pwd = scnextLine();

AuthResult result = new AuthResult();

Systemoutprintln("---Auth1---check");

a1check(name,pwd,result);

if(resultisSuccess()){

Systemoutprintln("auth success!");

} else {

Systemoutprintln("auth fail! because " + resultgetMsg());

}

Systemoutprintln("---Auth2---check");

a2check(name,pwd,result = new AuthResult());

if(resultisSuccess()){

Systemoutprintln("auth success!");

} else {

Systemoutprintln("auth fail! because " + resultgetMsg());

}

}

}

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

Auth1txt

name1:pwd1

name2:pwd2

Auth2txt

name3

pwd3

name4

pwd4

public class Vehicle {

private int speed;

private String brand;

private String no;

public Vehicle() {

}

public Vehicle(int speed, String brand, String no) {

thisspeed = speed;

thisbrand = brand;

thisno = no;

}

public int getSpeed() {

return speed;

}

public void setSpeed(int speed) {

thisspeed = speed;

}

public String getBrand() {

return brand;

}

public void setBrand(String brand) {

thisbrand = brand;

}

public String getNo() {

return no;

}

public void setNo(String no) {

thisno = no;

}

public String showInfo() {

return "speed:" + speed + ",brand=" + brand + ",no=" + no;

}

public static void main(String[] args) {

Vehicle v1 = new Vehicle(150, "brand1", "123");

Vehicle v2 = new Vehicle(100, "brand2", "976");

Vehicle v3 = new Vehicle(200, "brand3", "231");

int averSpeed = (v1speed + v2speed + v3speed) / 3;

Systemoutprintln("三辆车的平均速度为:" + averSpeed);

}

}

1。Peoplejava

public class People {

//性别(sex)及出生日期(date);方法成员有:获取人的性别和出生日期及构造方法。要求构造方法可以设置性别和出生日期的初始值。

private int sex;

private Date birth;

public People (int sex, Date birth) {

thissex = sex;

thisbirth = birth;

}

public int getSex() {

return thissex;

}

public Date getBirth() {

return thisbirth;

}

}

2。Studentjava

public class Student extends People{

private int sex;

private Date birth;

private String name;

private int stuno;

private double grate;

private String studentNative;

public Student(int sex, Date birth, String name, int stuno, double grate, String studentNative) {

super(sex, birth);

thisname = name;

thisstuno = stuno;

thisgrate = grate;

thisstudentNative = studentNative;

}

public Date getBirth() {

return birth;

}

public double getGrate() {

return grate;

}

public String getName() {

return name;

}

public int getSex() {

return sex;

}

public String getStudentNative() {

return studentNative;

}

public int getStuno() {

return stuno;

}

}

3。测试类自己编写就好了,创建一个People和Student的对象,然后设置值,取值就可以了。

五。

1构造方法没有返回值,方法名和类名一样

2继承是指子类可以拥有父类的方法和属性;多态是指父类的引用可以指向子类对象的实例

3重写

4重载

欢迎分享,转载请注明来源:表白网

原文地址:https://h5.hunlipic.com/biaobai/2698538.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2024-01-05
下一篇2024-01-05

发表评论

登录后才能评论

评论列表(0条)

    保存