主要想请教Java里是如何创建对象的。
题目翻译成中文如下:
编写一个程序,定义一个表示员工的类Staff,类的属性包括编号(num)、姓名(name)、出勤率(rateOfAttend)、基本工资(basicSal)、奖金(prize),员工实发工资(wage)其中员工实发工资=基本工资*出勤率+奖金。定义一个类StaffTest,创建Staff类的对象,通过键盘输入员工基本信息,计算员工实发工资,并输出对象所有属性信息。
16 个解决方案
#1
。。。忙到2点半啊 太命苦了。 但是帮你做作业还是不好的 把你遇到的问题说一下吧 这个题目很简单的,说说你遇到的问题大家也好帮你。
#2
这个题应该是简单的。。。
#3
目前来说写到了这里(必然有错……初学者是吧……):
package ch3;
import java.util.Scanner;
public class CH3_3
{
private int num;
private String name;
private double rateOfAttend;
private int basicSal;
private int prize;
double wage,
wage = basicSal*rateOfAttend+prize;
public static void main(String[] args)
{
public CH3_3(){};
public CH3_3(String num,String name,String rateOfAttend,String basicSal,String prize)
{
this.num=num;
this.name=name;
this.rateOfAttend=rateOfAttend;
this.basicSal=basicSal;
this.prize=prize
}
public String getNum(){
return num;
}
public void setNum(String num){
this.num=num;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getRateOfAttend(){
return rateOfAttend;
}
public void setRateOfAttend(String rateOfAttend){
this.rateOfAttend=rateOfAttend;
}
public String getBasicSal(){
return basicSal;
}
public void setBasicSal(String basicSal){
this.basicSal=basicSal;
}
public String getPrize(){
return prize;
}
public void setPrize(String prize){
this.prize=prize;
}
Scanner input = new Scanner(System.in);
System.out.printf("%s\n","Please input the num of the staff:");
num=in.nextInt();
System.out.printf("%s\n","Please input the name of the staff:");
name=in.nextString();
System.out.printf("%s\n","Please input the rateOfAttend of the staff:");
rateOfAttend=in.nextDouble();
System.out.printf("%s\n","Please input the baiscSal of the staff:");
baiscSal=in.nextInt();
System.out.printf("%s\n","Please input the prize of the staff:");
prize=in.nextInt();
}
}
package ch3;
import java.util.Scanner;
public class CH3_3
{
private int num;
private String name;
private double rateOfAttend;
private int basicSal;
private int prize;
double wage,
wage = basicSal*rateOfAttend+prize;
public static void main(String[] args)
{
public CH3_3(){};
public CH3_3(String num,String name,String rateOfAttend,String basicSal,String prize)
{
this.num=num;
this.name=name;
this.rateOfAttend=rateOfAttend;
this.basicSal=basicSal;
this.prize=prize
}
public String getNum(){
return num;
}
public void setNum(String num){
this.num=num;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getRateOfAttend(){
return rateOfAttend;
}
public void setRateOfAttend(String rateOfAttend){
this.rateOfAttend=rateOfAttend;
}
public String getBasicSal(){
return basicSal;
}
public void setBasicSal(String basicSal){
this.basicSal=basicSal;
}
public String getPrize(){
return prize;
}
public void setPrize(String prize){
this.prize=prize;
}
Scanner input = new Scanner(System.in);
System.out.printf("%s\n","Please input the num of the staff:");
num=in.nextInt();
System.out.printf("%s\n","Please input the name of the staff:");
name=in.nextString();
System.out.printf("%s\n","Please input the rateOfAttend of the staff:");
rateOfAttend=in.nextDouble();
System.out.printf("%s\n","Please input the baiscSal of the staff:");
baiscSal=in.nextInt();
System.out.printf("%s\n","Please input the prize of the staff:");
prize=in.nextInt();
}
}
#4
我已经写的代码在上一楼,求指点……
#5
你也错的太没谱了吧?
public CH3_3(String num,String name,String rateOfAttend,String basicSal,String prize)
{
this.num=num;
this.name=name;
this.rateOfAttend=rateOfAttend;
this.basicSal=basicSal;
this.prize=prize
}
这个方法不能写在main函数下,应该写在构造函数(构造函数和类同名)中,如
CH3_3(int num,String name,double rateOfAttend,int basicSal,int prize,double wage)
{
this.num=num;
this.name=name;
this.rateOfAttend=rateOfAttend;
this.basicSal=basicSal;
this.prize=prize;
this.wage=wage;
}
还有你的参数名和上面定义的也不一样啊,rateOfAttend定义成double你写成了string
另外System.out.printf("%s\n","Please input the prize of the staff:");
prize=in.nextInt();这个神马东西?你是先学的c语言吧?
我觉得吧,你可能是不细心,很多错误不该发生
public CH3_3(String num,String name,String rateOfAttend,String basicSal,String prize)
{
this.num=num;
this.name=name;
this.rateOfAttend=rateOfAttend;
this.basicSal=basicSal;
this.prize=prize
}
这个方法不能写在main函数下,应该写在构造函数(构造函数和类同名)中,如
CH3_3(int num,String name,double rateOfAttend,int basicSal,int prize,double wage)
{
this.num=num;
this.name=name;
this.rateOfAttend=rateOfAttend;
this.basicSal=basicSal;
this.prize=prize;
this.wage=wage;
}
还有你的参数名和上面定义的也不一样啊,rateOfAttend定义成double你写成了string
另外System.out.printf("%s\n","Please input the prize of the staff:");
prize=in.nextInt();这个神马东西?你是先学的c语言吧?
我觉得吧,你可能是不细心,很多错误不该发生
#6
lz,再看看书上的内容吧,你这思路很混乱的感觉呀
#7
问题确实很多……仔细修改了一下,目前只存在一个问题:输出结果时“Please input the name of the staff:”后不让输入name就跳过了……郁闷,怎么弄都弄不好……
#8
package ch3_3;
public class Staff {
private static int num;
private static String name;
private static double rateOfAttend;
private static int basicSal;
private static int prize;
private static double wage;
public Staff() {
};
public Staff(int num,String name,double rateOfAttend,int basicSal,int prize,double wage)
{
Staff.num=num;
Staff.name=name;
Staff.rateOfAttend=rateOfAttend;
Staff.basicSal=basicSal;
Staff.prize=prize;
Staff.wage=wage;
}
public int getNum() {
return num;
}
public void setNum(int num) {
Staff.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
Staff.name = name;
}
public double getRateOfAttend() {
return rateOfAttend;
}
public void setRateOfAttend(double rateOfAttend) {
Staff.rateOfAttend = rateOfAttend;
}
public int getBasicSal() {
return basicSal;
}
public void setBasicSal(int basicSal) {
Staff.basicSal = basicSal;
}
public int getPrize() {
return prize;
}
public void setPrize(int prize) {
Staff.prize = prize;
}
public double getWage(){
return wage;
}
public void setWage(double wage){
Staff.wage=wage;
}
public void displayMessage(int num,String name,double rateOfAttend,int basicSal,int prize,double wage)
{
System.out.printf(" %s%d\n %s%s\n %s%.2f%s\n %s%d\n %s%d\n %s%.2f\n",
"The number of the staff:",num,
"The name of the staff:",name,
"The rate of attend of the staff:",rateOfAttend*100,"%",
"The basicSal of the Staff:", basicSal,
"The prize of the staff:",prize,
"The wage of the staff:",wage);
}
}
#9
刚才那个是class的代码,后面的是将其实例化的对象的代码。求大神指点,为啥会出现name不让输入就直接跳过name直接让输rateOfAttend了……求指教……
package ch3_3;
import java.util.Scanner;
public class StaffTest {
@SuppressWarnings("resource")
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
Staff myStaff = new Staff();
System.out.println("Please input the num of the staff:");
int num= input.nextInt();
System.out.println("Please input the name of the staff:");
String name=input.nextLine();
System.out.println();
System.out.println("Please input the rateOfAttend of the staff:");
double rateOfAttend=input.nextDouble();
System.out.println("Please input the baiscSal of the staff:");
int basicSal=input.nextInt();
System.out.println("Please input the prize of the staff:");
int prize=input.nextInt();
double wage = rateOfAttend * basicSal + prize;
myStaff.displayMessage(num,name,rateOfAttend,basicSal,prize,wage);
}
}
#11
你学完类这节就应该可以写了
#12
nextline读取到回车
你输入num后的回车也会保留在System.in中
会被next.line读到
你输入num后的回车也会保留在System.in中
会被next.line读到
#13
大二上java也是双语课《java语言程序设计》,幸好对java有兴趣,一路坚强走过
#14
好 。 说的好
#15
现在想想我那会也跟着差不多,什么都不会!...
#16
public class StudentHandle {
public static void main(String[] args) {
Student stu1 = new Student("斯蒂芬",110);
stu1.setMathScore(90.0);
stu1.setEnglishScore(80.5);
stu1.setComputerScore(99.5);
System.out.println("斯蒂芬的数学成绩是: "+stu1.getMathScore());
System.out.println("斯蒂芬的英语成绩是: "+stu1.getEnglishScore());
System.out.println("斯蒂芬的计算机成绩是: "+stu1.getComputerScore());
System.out.println("斯蒂芬的成绩总分是: "+stu1.sumScore());
System.out.println("斯蒂芬的成绩平均分是: "+stu1.avgScore());
System.out.println("斯蒂芬的成绩最高分是: "+stu1.maxScore());
System.out.println("斯蒂芬的成绩最低分是: "+stu1.getMathScore());
}
}
/*创建一个学生类*/
class Student{
private String name;//学生的姓名
private int id;//学生的学号
private double mathScore;//数学成绩
private double englishScore;//英语成绩
private double computerScore;//计算机成绩
/*构造方法*/
public Student(String name,int id){
this.name = name;
this.id = id;
}
/*学生姓名的get方法*/
public String getName(){return name;}
/*学生学号的get方法*/
public int getId(){return id;}
/*学生数学成绩的set和get方法*/
public void setMathScore(double mathScore){this.mathScore = mathScore;}
public double getMathScore(){return mathScore;}
/*学生英语成绩的set和get方法*/
public void setEnglishScore(double englishScore){this.englishScore = englishScore;}
public double getEnglishScore(){return englishScore;}
/*学生计算机成绩的set和get方法*/
public void setComputerScore(double computerScore){this.computerScore = computerScore;}
public double getComputerScore(){return computerScore;}
/*该学生3门课程的成绩总和*/
public double sumScore(){return (mathScore+englishScore+computerScore);}
/*该学生3门课程的成绩平均分*/
public double avgScore(){return (mathScore+englishScore+computerScore)/3;}
/*该学生3门课程的成绩最高分*/
public double maxScore(){
double maxScore = mathScore;
if(englishScore>maxScore)
maxScore = englishScore;
if(computerScore>maxScore)
maxScore = computerScore;
return maxScore;
}
/*该学生3门课程的成绩最低分*/
public double minScore(){
double minScore = mathScore;
if(minScore> englishScore)
minScore = englishScore;
if(minScore> computerScore)
minScore = computerScore;
return minScore;
}
}
#1
。。。忙到2点半啊 太命苦了。 但是帮你做作业还是不好的 把你遇到的问题说一下吧 这个题目很简单的,说说你遇到的问题大家也好帮你。
#2
这个题应该是简单的。。。
#3
目前来说写到了这里(必然有错……初学者是吧……):
package ch3;
import java.util.Scanner;
public class CH3_3
{
private int num;
private String name;
private double rateOfAttend;
private int basicSal;
private int prize;
double wage,
wage = basicSal*rateOfAttend+prize;
public static void main(String[] args)
{
public CH3_3(){};
public CH3_3(String num,String name,String rateOfAttend,String basicSal,String prize)
{
this.num=num;
this.name=name;
this.rateOfAttend=rateOfAttend;
this.basicSal=basicSal;
this.prize=prize
}
public String getNum(){
return num;
}
public void setNum(String num){
this.num=num;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getRateOfAttend(){
return rateOfAttend;
}
public void setRateOfAttend(String rateOfAttend){
this.rateOfAttend=rateOfAttend;
}
public String getBasicSal(){
return basicSal;
}
public void setBasicSal(String basicSal){
this.basicSal=basicSal;
}
public String getPrize(){
return prize;
}
public void setPrize(String prize){
this.prize=prize;
}
Scanner input = new Scanner(System.in);
System.out.printf("%s\n","Please input the num of the staff:");
num=in.nextInt();
System.out.printf("%s\n","Please input the name of the staff:");
name=in.nextString();
System.out.printf("%s\n","Please input the rateOfAttend of the staff:");
rateOfAttend=in.nextDouble();
System.out.printf("%s\n","Please input the baiscSal of the staff:");
baiscSal=in.nextInt();
System.out.printf("%s\n","Please input the prize of the staff:");
prize=in.nextInt();
}
}
package ch3;
import java.util.Scanner;
public class CH3_3
{
private int num;
private String name;
private double rateOfAttend;
private int basicSal;
private int prize;
double wage,
wage = basicSal*rateOfAttend+prize;
public static void main(String[] args)
{
public CH3_3(){};
public CH3_3(String num,String name,String rateOfAttend,String basicSal,String prize)
{
this.num=num;
this.name=name;
this.rateOfAttend=rateOfAttend;
this.basicSal=basicSal;
this.prize=prize
}
public String getNum(){
return num;
}
public void setNum(String num){
this.num=num;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getRateOfAttend(){
return rateOfAttend;
}
public void setRateOfAttend(String rateOfAttend){
this.rateOfAttend=rateOfAttend;
}
public String getBasicSal(){
return basicSal;
}
public void setBasicSal(String basicSal){
this.basicSal=basicSal;
}
public String getPrize(){
return prize;
}
public void setPrize(String prize){
this.prize=prize;
}
Scanner input = new Scanner(System.in);
System.out.printf("%s\n","Please input the num of the staff:");
num=in.nextInt();
System.out.printf("%s\n","Please input the name of the staff:");
name=in.nextString();
System.out.printf("%s\n","Please input the rateOfAttend of the staff:");
rateOfAttend=in.nextDouble();
System.out.printf("%s\n","Please input the baiscSal of the staff:");
baiscSal=in.nextInt();
System.out.printf("%s\n","Please input the prize of the staff:");
prize=in.nextInt();
}
}
#4
我已经写的代码在上一楼,求指点……
#5
你也错的太没谱了吧?
public CH3_3(String num,String name,String rateOfAttend,String basicSal,String prize)
{
this.num=num;
this.name=name;
this.rateOfAttend=rateOfAttend;
this.basicSal=basicSal;
this.prize=prize
}
这个方法不能写在main函数下,应该写在构造函数(构造函数和类同名)中,如
CH3_3(int num,String name,double rateOfAttend,int basicSal,int prize,double wage)
{
this.num=num;
this.name=name;
this.rateOfAttend=rateOfAttend;
this.basicSal=basicSal;
this.prize=prize;
this.wage=wage;
}
还有你的参数名和上面定义的也不一样啊,rateOfAttend定义成double你写成了string
另外System.out.printf("%s\n","Please input the prize of the staff:");
prize=in.nextInt();这个神马东西?你是先学的c语言吧?
我觉得吧,你可能是不细心,很多错误不该发生
public CH3_3(String num,String name,String rateOfAttend,String basicSal,String prize)
{
this.num=num;
this.name=name;
this.rateOfAttend=rateOfAttend;
this.basicSal=basicSal;
this.prize=prize
}
这个方法不能写在main函数下,应该写在构造函数(构造函数和类同名)中,如
CH3_3(int num,String name,double rateOfAttend,int basicSal,int prize,double wage)
{
this.num=num;
this.name=name;
this.rateOfAttend=rateOfAttend;
this.basicSal=basicSal;
this.prize=prize;
this.wage=wage;
}
还有你的参数名和上面定义的也不一样啊,rateOfAttend定义成double你写成了string
另外System.out.printf("%s\n","Please input the prize of the staff:");
prize=in.nextInt();这个神马东西?你是先学的c语言吧?
我觉得吧,你可能是不细心,很多错误不该发生
#6
lz,再看看书上的内容吧,你这思路很混乱的感觉呀
#7
问题确实很多……仔细修改了一下,目前只存在一个问题:输出结果时“Please input the name of the staff:”后不让输入name就跳过了……郁闷,怎么弄都弄不好……
#8
package ch3_3;
public class Staff {
private static int num;
private static String name;
private static double rateOfAttend;
private static int basicSal;
private static int prize;
private static double wage;
public Staff() {
};
public Staff(int num,String name,double rateOfAttend,int basicSal,int prize,double wage)
{
Staff.num=num;
Staff.name=name;
Staff.rateOfAttend=rateOfAttend;
Staff.basicSal=basicSal;
Staff.prize=prize;
Staff.wage=wage;
}
public int getNum() {
return num;
}
public void setNum(int num) {
Staff.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
Staff.name = name;
}
public double getRateOfAttend() {
return rateOfAttend;
}
public void setRateOfAttend(double rateOfAttend) {
Staff.rateOfAttend = rateOfAttend;
}
public int getBasicSal() {
return basicSal;
}
public void setBasicSal(int basicSal) {
Staff.basicSal = basicSal;
}
public int getPrize() {
return prize;
}
public void setPrize(int prize) {
Staff.prize = prize;
}
public double getWage(){
return wage;
}
public void setWage(double wage){
Staff.wage=wage;
}
public void displayMessage(int num,String name,double rateOfAttend,int basicSal,int prize,double wage)
{
System.out.printf(" %s%d\n %s%s\n %s%.2f%s\n %s%d\n %s%d\n %s%.2f\n",
"The number of the staff:",num,
"The name of the staff:",name,
"The rate of attend of the staff:",rateOfAttend*100,"%",
"The basicSal of the Staff:", basicSal,
"The prize of the staff:",prize,
"The wage of the staff:",wage);
}
}
#9
刚才那个是class的代码,后面的是将其实例化的对象的代码。求大神指点,为啥会出现name不让输入就直接跳过name直接让输rateOfAttend了……求指教……
package ch3_3;
import java.util.Scanner;
public class StaffTest {
@SuppressWarnings("resource")
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
Staff myStaff = new Staff();
System.out.println("Please input the num of the staff:");
int num= input.nextInt();
System.out.println("Please input the name of the staff:");
String name=input.nextLine();
System.out.println();
System.out.println("Please input the rateOfAttend of the staff:");
double rateOfAttend=input.nextDouble();
System.out.println("Please input the baiscSal of the staff:");
int basicSal=input.nextInt();
System.out.println("Please input the prize of the staff:");
int prize=input.nextInt();
double wage = rateOfAttend * basicSal + prize;
myStaff.displayMessage(num,name,rateOfAttend,basicSal,prize,wage);
}
}
#10
首先,忙到两点半对身体不好
第二,这样的例子实在太多,你要学会谷歌 http://lmgtfy.com/?q=java+simple+class,自己模仿的写一个
第二,这样的例子实在太多,你要学会谷歌 http://lmgtfy.com/?q=java+simple+class,自己模仿的写一个
#11
你学完类这节就应该可以写了
#12
nextline读取到回车
你输入num后的回车也会保留在System.in中
会被next.line读到
你输入num后的回车也会保留在System.in中
会被next.line读到
#13
大二上java也是双语课《java语言程序设计》,幸好对java有兴趣,一路坚强走过
#14
好 。 说的好
#15
现在想想我那会也跟着差不多,什么都不会!...
#16
public class StudentHandle {
public static void main(String[] args) {
Student stu1 = new Student("斯蒂芬",110);
stu1.setMathScore(90.0);
stu1.setEnglishScore(80.5);
stu1.setComputerScore(99.5);
System.out.println("斯蒂芬的数学成绩是: "+stu1.getMathScore());
System.out.println("斯蒂芬的英语成绩是: "+stu1.getEnglishScore());
System.out.println("斯蒂芬的计算机成绩是: "+stu1.getComputerScore());
System.out.println("斯蒂芬的成绩总分是: "+stu1.sumScore());
System.out.println("斯蒂芬的成绩平均分是: "+stu1.avgScore());
System.out.println("斯蒂芬的成绩最高分是: "+stu1.maxScore());
System.out.println("斯蒂芬的成绩最低分是: "+stu1.getMathScore());
}
}
/*创建一个学生类*/
class Student{
private String name;//学生的姓名
private int id;//学生的学号
private double mathScore;//数学成绩
private double englishScore;//英语成绩
private double computerScore;//计算机成绩
/*构造方法*/
public Student(String name,int id){
this.name = name;
this.id = id;
}
/*学生姓名的get方法*/
public String getName(){return name;}
/*学生学号的get方法*/
public int getId(){return id;}
/*学生数学成绩的set和get方法*/
public void setMathScore(double mathScore){this.mathScore = mathScore;}
public double getMathScore(){return mathScore;}
/*学生英语成绩的set和get方法*/
public void setEnglishScore(double englishScore){this.englishScore = englishScore;}
public double getEnglishScore(){return englishScore;}
/*学生计算机成绩的set和get方法*/
public void setComputerScore(double computerScore){this.computerScore = computerScore;}
public double getComputerScore(){return computerScore;}
/*该学生3门课程的成绩总和*/
public double sumScore(){return (mathScore+englishScore+computerScore);}
/*该学生3门课程的成绩平均分*/
public double avgScore(){return (mathScore+englishScore+computerScore)/3;}
/*该学生3门课程的成绩最高分*/
public double maxScore(){
double maxScore = mathScore;
if(englishScore>maxScore)
maxScore = englishScore;
if(computerScore>maxScore)
maxScore = computerScore;
return maxScore;
}
/*该学生3门课程的成绩最低分*/
public double minScore(){
double minScore = mathScore;
if(minScore> englishScore)
minScore = englishScore;
if(minScore> computerScore)
minScore = computerScore;
return minScore;
}
}