
正文
java计算学费代码 java课程设计题目及代码简约计算器
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
写几个简单的Java程序,计算学费之类的
你好!很高兴为你解答。
首先,为你提供两个类,一个是StuFee对象,用于模拟学费对象
二是TestStuFee对象,用于运行测试学费对象,解决问题
其次,以下是运行结果:
**********************************
run:
第十年学费:15513.282159785162
第11到第14年70207.39453239123
请输入10个整数
1:1
2:2
3:3
4:4
5:5
6:6
7:7
8:8
9:9
10:10
5
请输入10个浮点数
1:1.1
2:2.2
3:3.3
4:4.4
5:5.5
6:6.6
7:7.7
8:8.8
9:9.9
10:10.10
5.96
成功生成(总时间:28 秒)
最后,给出源码。请建2个.java文件,分别放置这两个类。
*******************第一个***************************
package szu.ykl.calcStuFee;
/**
*
* @author 叶科良
*/
public class StuFee {
private double addSpeed;
private double baseFee;
private double fee;
public double getAddSpeed() {
return addSpeed;
}
public void setAddSpeed(double addSpeed) {
this.addSpeed = addSpeed;
}
public double calcFee(double year) {
return (Math.pow((1 + this.getAddSpeed()), year - 1)) * this.getBaseFee();
}
public double calcFee(int year1, int year2) {
if (year1 = year2) {
double result = 0;
for (int i = 0; i (year2 - year1+1); i++) {
result +=this.calcFee(year1+i);
}
return result;
}
return -1;
}
public StuFee(double addSpeed, double baseFee) {
this.setAddSpeed(addSpeed);
this.setBaseFee(baseFee);
}
public double getBaseFee() {
return baseFee;
}
public void setBaseFee(double baseFee) {
this.baseFee = baseFee;
}
public static int average(int[] array) {
if (null != array) {
if (array.length != 0) {
int resultInt = 0;
for (int i = 0; i array.length; i++) {
resultInt += array[i];
}
return (int)(resultInt/array.length);
} else {
return -1;
}
} else {
return -1;
}
}
;
public static double average(double[] array) {
if (null != array) {
if (array.length != 0) {
double resultDouble = 0;
for (int i = 0; i array.length; i++) {
resultDouble += array[i];
}
return (resultDouble/array.length);
} else {
return -1;
}
} else {
return -1;
}
};
}
********************第二个***************************************
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package szu.ykl.calcStuFee;
import java.util.Scanner;
/**
*
* @author 叶科良
*/
public class TestStuFee {
public static void main(String[] args) {
StuFee test = new StuFee(0.05, 10000);
System.out.println("第十年学费:" + test.calcFee(10));
System.out.println("第11到第14年" + test.calcFee(11, 14));
TestStuFee testStu = new TestStuFee();
try {
System.out.println("请输入10个整数");
System.out.println(test.average(testStu.getAcceptAsIntArray(10)));
} catch (java.util.InputMismatchException e) {
System.out.println("请输入整数!");
}
try {
System.out.println("请输入10个浮点数");
System.out.println(test.average(testStu.getAcceptAsDoubleArray(10)));
} catch (java.util.InputMismatchException e) {
System.out.println("请输入浮点数!");
}
}
/**
* 指定数组大小,从命令行接收整数
* @param arraySize 数组大小
* @return 原始整数数组
*/
public int[] getAcceptAsIntArray(int arraySize) {
int[] acceptArray = new int[arraySize];
Scanner reader = new Scanner(System.in);
for (int i = 0; i arraySize; i++) {
System.out.print((i + 1) + ":");
acceptArray[i] = reader.nextInt();
}
return acceptArray;
}
/**
* 指定数组大小,从命令行接收浮点数
* @param arraySize 数组大小
* @return 原始浮点数数组
*/
public double[] getAcceptAsDoubleArray(int arraySize) {
double[] acceptArray = new double[arraySize];
Scanner reader = new Scanner(System.in);
for (int i = 0; i arraySize; i++) {
System.out.print((i + 1) + ":");
acceptArray[i] = reader.nextDouble();
}
return acceptArray;
}
}
最后,有问题可以追问!
相关问答
Q1: java,学生的学费程序1这段程序有什么错误,如何修改? 2请高手写出每一步都发生了什么,越详细越好/
//定义包含main方法类t
//ps:java中建议类名都大写
//由于错误太多,而且都是写小错,我帮您都直接修改了。
public class t
{
//这是main方法,main方法是程序java计算学费代码的入口点
public static void main(String[] args)
{
Stu stu1 = new Stu(29, "aa", 340); //实例化一个Stu类java计算学费代码的对象stu1,用的是Stu唯一的一个构造方法
Stu stu2 = new Stu(39, "aa", 240); //实例化一个Stu类的对象stu2,用的是Stu唯一的一个构造方法
System.out.println(Stu.getTotalFee()); //输出Stu类的一个静态方法getTotalFee()的返回值,也就是总钱数
}
}
//下面定义了一个名为Stu的类
class Stu
{
//以下四句是声明了Stu类的4个成员变量,整型的age,字符串型的name,整型的free
//以及静态的整型totalFee
int age;
String name;
int free;
static int totalFee;
//这个方法是Stu类的构造方法,在实例化Stu这个类的时候必须
//使用下面指定的参数(即main方法中new Stu(29, "aa", 340)
//每实例化一个对象,就把第三个参数代表的钱数加到totalFee这个
//变量上。
public Stu(int age, String name, int fee)
{
this.age = age;
this.name = name;
totalFee += fee;
}
//这个静态方法返回了上面定义的一个静态成员变量
//返回总钱数
//PS:静态的方法可以不用实例化对象直接通过类名.方法名这种形式去调用
// 静态的方法和成员变量不属于任何一个实例(Instance),而属于这个类的
// class对象
public static int getTotalFee()
{
return totalFee;
}
}
Q2: 用java编写今年某大学生的学费为400元,学费的增长率为5%。使用循环语句编写程序计算10年后的
public static void main(String[] args) {
// TODO Auto-generated method stub
double fee = 400;
System.out.println("10年后学费:"+fees(fee, 10, 0.05));
double sum=fees(fee, 0, 0.05)+fees(fee, 1, 0.05)+fees(fee, 2, 0.05)+fees(fee, 3, 0.05);
System.out.println("从现在开始4年总学费:"+sum);
}
public static double fees(double fee,int years,double rate) {
for(int i=1;i=years;i++)
fee += fee*rate;
return fee;
}
关于java计算学费代码和java课程设计题目及代码简约计算器的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






