定义学生类student有三个属性成绩
Ⅰ 求一道编程题。我创建了学生类。定义了3个字段。名称,成绩。和总成绩。但是编写的求和方法为什么只能。
Sum函数不要这样写嘛,给它传个参数,传1就计算第1次的返回,传2就计算第2次的返回
Ⅱ 定义一个学生类,有姓名,学号,成绩(int类型)三个属性 求大神帮助啊!谢谢各位
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.List;
publicclassSorter{
publicstaticvoidmain(String[]args){
<Student>students=newArrayList<Student>();
Studentstudent1=newStudent();
student1.id="1";
student1.name="张三";
student1.score=60;
students.add(student1);
Studentstudent2=newStudent();
student2.id="2";
student2.name="李四";
student2.score=70;
students.add(student2);
Studentstudent3=newStudent();
student3.id="3";
student3.name="王二";
student3.score=80;
students.add(student3);
Collections.sort(students);
for(Studentstudent:students){
System.out.println("编号:"+student.id+",姓名:"+student.name+",成绩:"+student.score);
}
}
}
<Student>{
publicStringid;
publicStringname;
publicintscore;
@Override
publicintcompareTo(Studento){
if(score>o.score){
return-1;
}elseif(score<o.score){
return1;
}
return0;
}
}
Ⅲ 定义一个学生类,包含学号、姓名和总分三个属性,要求创建5个学生对象,并查找总分大于200分学生的信息
import java.util.Scanner;
class Students{
protected int sid; //学号
protected String name; //姓名
protected double score; //总分
public Students(){
}
public Students(int sid, String name, double score){
this.sid = sid;
this.name = name;
this.score = score;
}
public double getScore(){
return this.score;
}
public void display(){
System.out.println("学号:" + sid);
System.out.println("姓名:" + name);
System.out.println("总分:" + score);
}
}
public class Test26 {
public static void main(String[] args) {
Students[] studs = new Students[5];
int i;
int sid;
String name;
double score;
Scanner scan = new Scanner(System.in);
System.out.println("添加学生信息");
for(i=0; i<studs.length; i++){
System.out.print("学号:");
sid = scan.nextInt();
System.out.print("姓名:");
name = scan.next();
System.out.print("总分:");
score = scan.nextDouble();
studs[i] = new Students(sid, name, score);
}
System.out.println("总分大于200分学生的信息");
for(i=0; i<studs.length; i++){
if(studs[i].getScore() > 200){
studs[i].display();
}
}
}
}
Ⅳ 定义一个学生类, 需要有姓名, 年龄, 考试成绩三个成员属性. 属性(成员变量)需要私有并提供get, set方法.
public class Student {
内private String name ;
private int age;
private double Exam;
public String getName() {
容return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getExam() {
return Exam;
}
public void setExam(double exam) {
Exam = exam;
}
}
Ⅳ java:定义学生类Student, 其中属性有 name, id, score ,分别表示姓名,学好,成绩.....
定义一个表示学生信息的类Student,要求如下:
(1)类Student的成员变量:
sNO表示学号;sName表示姓名;sSex表示性别;表示年龄;sJava:表示Java课程成绩。
(2)类Student带参数的构造方法:
在构造方法中通过形参完成对成员变量的赋值操作。
(3)类Student的方法成员:
getNo():获得学号;
getName():获得姓名;
getSex():获得性别;
getAge()获得年龄;
getJava():获得Java课程成绩
(4)根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。*/
Ⅵ 定义一个学生类,需要有姓名,年龄,考试成绩三个成员属性,创建5个对象,属性可以任意值。OC
.h文件
#import <Foundation/Foundation.h>
@interface Student : NSObject
{
NSString *name;
int age;
float score;
}
-(void)setName:(NSString *)newname;
-(NSString *)name;
-(void)setAge:(int)newage;
-(int)age;
-(void)setScore:(float)newscore;
-(float)score;
-(Student *)initWithName:(NSString *)newname andAge:(int)newage andScore:(float)newscore;
+(Student *)studentWithName:(NSString *)newname andAge:(int)newage andScore:(float)newscore;
-(NSComparisonResult)compareWithScore:(Student *)stu;
-(NSComparisonResult)compareWithAge:(Student *)stu;
-(NSComparisonResult)compareWithName:(Student *)stu;
@end
Student.m文件
#import "Student.h"
@implementation Student
-(void)setName:(NSString *)newname{
name=newname;
}
-(NSString *)name{
return name;
}
-(void)setAge:(int)newage{
age=newage;
}
-(int)age{
return age;
}
-(void)setScore:(float)newscore{
score=newscore;
}
-(float)score{
return score;
}
-(Student *)initWithName:(NSString *)newname andAge:(int)newage andScore:(float)newscore{
self=[super init];
if (self) {
name=newname;
age=newage;
score=newscore;
}
return self;
}
+(Student *)studentWithName:(NSString *)newname andAge:(int)newage andScore:(float)newscore{
Student *stu=[[Student alloc]initWithName:newname andAge:newage andScore:newscore];
return stu;
}
-(NSComparisonResult)compareWithScore:(Student *)stu{
NSComparisonResult result1;
if (self.score>stu.score) {
return NSOrderedDescending;
}
else if (self.score<stu.score){
return NSOrderedAscending;
}
else{
return NSOrderedSame;
}
return result1;
}
-(NSComparisonResult)compareWithAge:(Student *)stu{
NSComparisonResult result2;
if (self.age>stu.age) {
return NSOrderedDescending;
} else if(self.age<stu.age){
return NSOrderedAscending;
}
else{
return NSOrderedSame;
}
return result2;
}
-(NSComparisonResult)compareWithName:(Student *)stu{
return [self.name compare:stu.name];
}
-(NSString *)description{
NSString *str=[NSString stringWithFormat:@"%f,%d,%@",score,age,name];
return str;
}
@end
测试代码
Student *stu=[Student new];
[stu setScore:65.5];
[stu setAge:21];
[stu setName:@"zhangsan"];
Student *stu1=[Student new];
[stu1 setScore:70.3];
[stu1 setAge:19];
[stu1 setName:@"lisi"];
Student *stu2=[Student new];
[stu2 setScore:56.5];
[stu2 setAge:18];
[stu2 setName:@"wangwu"];
Student *stu3=[Student new];
[stu3 setScore:85.6];
[stu3 setAge:25];
[stu3 setName:@"mingming"];
Student *stu4=[Student new];
[stu4 setScore:95.4];
[stu4 setAge:20];
[stu4 setName:@"xionghong"];
NSArray *stuArr=[NSArray arrayWithObjects:stu,stu1,stu2,stu3,stu4, nil];
NSArray *sortArr=[stuArr sortedArrayUsingSelector:@selector(compareWithScore:)];
NSLog(@"按成绩排序后:");
for (int i=0; i<sortArr.count; i++) {
Student *a=[sortArr objectAtIndex:i];
NSLog(@"Myname is %@,age is %d,score is %f",a.name,a.age,a.score);
}
NSArray *sortArr1=[stuArr sortedArrayUsingSelector:@selector(compareWithAge:)];
NSLog(@"按年龄排序后:");{
for (int i=0; i<sortArr1.count; i++) {
Student *a=[sortArr1 objectAtIndex:i];
NSLog(@"Myname is %@,age is %d,score is %f",a.name,a.age,a.score);
}
}
NSArray *sortArr2=[stuArr sortedArrayUsingSelector:@selector(compareWithName:)];
NSLog(@"按姓名排序后:");{
for (int i=0; i<sortArr2.count; i++) {
Student *a=[sortArr2 objectAtIndex:i];
NSLog(@"Myname is %@,age is %d,score is %f",a.name,a.age,a.score);
}
}
Ⅶ 定义一个学生类,需要有姓名,年龄,考试成绩三个成员属性,创建5个对象,属性可以任意值。(Objective-C
创建一个Obj,设置obj的三个属性(string类型):姓名、年龄、成绩;
给obj每个属性赋值的时候,重写每个属性的set、get、方法;
赋值完成后,存入数组;
查询的时候写个排序算法,输出打印
Ⅷ 【java】定义学生类,包含三个属性:学号,成绩,及入学日期(日期也为类【急求!!】
最简单的就是重写学生类的 equals() 和 hashCode() 以 入学日期为关键字段, 然后调用Arrays.sort() 方法, 默认就是升序排序
Ⅸ 定义一个学生类, 需要有姓名, 年龄, 考试成绩三个成员属性
public class Student {
private String name;private int age;private double score;public Student(String name,int age,double score){
this.name=name;
this.age=age;
this.score=score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
Ⅹ 定义一个表示学生的类(Student) Student类包括表示学生的学号姓名性别年龄和3门课程成绩的信息数据及
这是学生类:
public class Student {
private int xuehao;
private String xingming;
private String xingbie;
private int shuxue;
private int yuwen;
private int yingyu;
public int getShuxue() {
return shuxue;
}
public void setShuxue(int shuxue) {
this.shuxue = shuxue;
}
public String getXingbie() {
return xingbie;
}
public void setXingbie(String xingbie) {
this.xingbie = xingbie;
}
public String getXingming() {
return xingming;
}
public void setXingming(String xingming) {
this.xingming = xingming;
}
public int getXuehao() {
return xuehao;
}
public void setXuehao(int xuehao) {
this.xuehao = xuehao;
}
public int getYingyu() {
return yingyu;
}
public void setYingyu(int yingyu) {
this.yingyu = yingyu;
}
public int getYuwen() {
return yuwen;
}
public void setYuwen(int yuwen) {
this.yuwen = yuwen;
}
public int average(int x,int y,int z){
return (x+y+z)/3;
}
}
这是测试类:
public class TestStudent{
public static void main(String args[]) {
Student st1=new Student();
st1.setShuxue(75);
st1.setYingyu(80);
st1.setYuwen(85);
System.out.println("第一个学生的平均成绩为:"+st1.average(st1.getShuxue(),st1.getYingyu(),st1.getYuwen()));
Student st2=new Student();
Student st3=new Student();
Student st4=new Student();
Student st5=new Student();
}
}
你要求5个学生对象,我写出一个,后面的依次类推.应该会吧.