Java面向对象程序设计.哪位JAVA高手,帮我做一下下面的编程试题...
三、编程题.(25分)1.(15分)编写一个空调类AirCondition,该类继承于设备类Device,该类有:成员变量:privateinttemperature//...
三、编程题.(25分) 1.(15分)编写一个空调类AirCondition,该类继承于设备类Device,该类有: 成员变量: private int temperature //空调的温度 构造方法: AirCondition(String dName, String address,double price,int temperature) //初始化化空调的名称、出厂厂家、价格和初始温度 三个成员方法: public void raiseTemp (int temp) //给空调升温temp度 public double lowerTemp(int temp) //给空调降温temp度 public String toString() //用于返回设备的设备名称、出厂厂家、价格信息和当前温度信息 2.(10分)文件“studentdata”中存放了用DataOutputStream方式写入的10个学生记录,记录的内容与格式为: 学号 java课程成绩 学号 java课程成绩 …… 其中学号的数据类型为String,java课程成绩的数据类型为int,请编程读取该文件信息并显示在屏幕上,并求最高成绩、最低成绩和平均成绩。
展开
3个回答
展开全部
第一题
public class AirCondition extends Device {
private String dName;//空调名称
private String address;//空调厂家
private double price;//空调价格
private int temperature;//空调的温度
public AirCondition(String dName, String address,double price,int temperature) {
this.dName = dName;
this.address = address;
this.price = price;
this.temperature = temperature;
}
//给空调升温temp度
public void raiseTemp (int temp) {
this.temperature += temp;
}
//给空调降温temp度
public double lowerTemp(int temp) {
return this.temperature -= temp;
}
public String getdName() {
return dName;
}
public void setdName(String dName) {
this.dName = dName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getTemperature() {
return temperature;
}
public void setTemperature(int temperature) {
this.temperature = temperature;
}
public String toString() {
StringBuffer r = new StringBuffer();
r.append("设备名称: " + getdName() + "\n");
r.append("出厂厂家: " + getAddress() + "\n");
r.append("价格: " + getPrice() + "\n");
r.append("初始温度: " + getTemperature() + "\n");
return r.toString();
}
}
第二题
import java.io.DataInputStream;
import java.io.FileInputStream;
public class Score {
private Object[][] data = new Object[10][2];//存放学好和密码
public static void main(String[] args) {
new Score().readData();
}
//读取数据
public void readData(){
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream("studentdata"));
for(int i = 0; i < data.length; i++){
data[i][0] = dis.readUTF();//学好
data[i][1] = dis.readInt();//密码
}
//调用计算
calc();
} catch (Exception e) {
e.printStackTrace();
}
}
//计算
public void calc(){
int total = 0;
double avg = 0;
int max = 0;
int min = 0;
Object[] temp;
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data.length - 1; j++) {
if((Integer)(data[j][1]) > (Integer)(data[j + 1][1])){
temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
for (int i = 0; i < data.length; i++) {
total += (Integer)data[i][1];
System.out.println("学号: " + data[i][0] + "\t" + "分数: " + data[i][1]);
}
avg = total / data.length;//平均
min = (Integer)data[0][1];
max = (Integer)data[data.length - 1][1];
System.out.println("最高分: " + max);
System.out.println("最低分: " + min);
System.out.println("平均分: " + avg);
}
}
public class AirCondition extends Device {
private String dName;//空调名称
private String address;//空调厂家
private double price;//空调价格
private int temperature;//空调的温度
public AirCondition(String dName, String address,double price,int temperature) {
this.dName = dName;
this.address = address;
this.price = price;
this.temperature = temperature;
}
//给空调升温temp度
public void raiseTemp (int temp) {
this.temperature += temp;
}
//给空调降温temp度
public double lowerTemp(int temp) {
return this.temperature -= temp;
}
public String getdName() {
return dName;
}
public void setdName(String dName) {
this.dName = dName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getTemperature() {
return temperature;
}
public void setTemperature(int temperature) {
this.temperature = temperature;
}
public String toString() {
StringBuffer r = new StringBuffer();
r.append("设备名称: " + getdName() + "\n");
r.append("出厂厂家: " + getAddress() + "\n");
r.append("价格: " + getPrice() + "\n");
r.append("初始温度: " + getTemperature() + "\n");
return r.toString();
}
}
第二题
import java.io.DataInputStream;
import java.io.FileInputStream;
public class Score {
private Object[][] data = new Object[10][2];//存放学好和密码
public static void main(String[] args) {
new Score().readData();
}
//读取数据
public void readData(){
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream("studentdata"));
for(int i = 0; i < data.length; i++){
data[i][0] = dis.readUTF();//学好
data[i][1] = dis.readInt();//密码
}
//调用计算
calc();
} catch (Exception e) {
e.printStackTrace();
}
}
//计算
public void calc(){
int total = 0;
double avg = 0;
int max = 0;
int min = 0;
Object[] temp;
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data.length - 1; j++) {
if((Integer)(data[j][1]) > (Integer)(data[j + 1][1])){
temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
for (int i = 0; i < data.length; i++) {
total += (Integer)data[i][1];
System.out.println("学号: " + data[i][0] + "\t" + "分数: " + data[i][1]);
}
avg = total / data.length;//平均
min = (Integer)data[0][1];
max = (Integer)data[data.length - 1][1];
System.out.println("最高分: " + max);
System.out.println("最低分: " + min);
System.out.println("平均分: " + avg);
}
}
展开全部
第一题
public class AirCondition extends Device {
private String dName;//空调名称
private String address;//空调厂家
private double price;//空调价格
private int temperature;//空调的温度
public AirCondition(String dName, String address,double price,int temperature) {
this.dName = dName;
this.address = address;
this.price = price;
this.temperature = temperature;
}
//给空调升温temp度
public void raiseTemp (int temp) {
this.temperature += temp;
}
//给空调降温temp度
public double lowerTemp(int temp) {
return this.temperature -= temp;
}
public String getdName() {
return dName;
}
public void setdName(String dName) {
this.dName = dName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getTemperature() {
return temperature;
}
public void setTemperature(int temperature) {
this.temperature = temperature;
}
public String toString() {
StringBuffer r = new StringBuffer();
r.append("设备名称: " + getdName() + "\n");
r.append("出厂厂家: " + getAddress() + "\n");
r.append("价格: " + getPrice() + "\n");
r.append("初始温度: " + getTemperature() + "\n");
return r.toString();
}
}
第二题
import java.io.DataInputStream;
import java.io.FileInputStream;
public class Score {
private Object[][] data = new Object[10][2];//存放学好和密码
public static void main(String[] args) {
new Score().readData();
}
//读取数据
public void readData(){
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream("studentdata"));
for(int i = 0; i < data.length; i++){
data[i][0] = dis.readUTF();//学好
data[i][1] = dis.readInt();//密码
}
//调用计算
calc();
} catch (Exception e) {
e.printStackTrace();
}
}
//计算
public void calc(){
int total = 0;
double avg = 0;
int max = 0;
int min = 0;
Object[] temp;
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data.length - 1; j++) {
if((Integer)(data[j][1]) > (Integer)(data[j + 1][1])){
temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
for (int i = 0; i < data.length; i++) {
total += (Integer)data[i][1];
System.out.println("学号: " + data[i][0] + "\t" + "分数: " + data[i][1]);
}
avg = total / data.length;//平均
min = (Integer)data[0][1];
max = (Integer)data[data.length - 1][1];
System.out.println("最高分: " + max);
System.out.println("最低分: " + min);
System.out.println("平均分: " + avg);
}
} 赞同0| 评论 检举 | 2012-2-11 00:42 hugo890308 | 三级
1.AirCondition
public class AirCondition extends Device{
private int temperature;
public AirCondition(){}
public AirCondition(String dName,String address,double price,int temperature){
this.dName = dName;
this.address = address;
this.price = price;
this.temperature = temperature;
}
public void raiseTemp(int temp){
this.temperature += temp;
}
public double lowerTemp(int temp){
return this.temperature - temp;
}
public String toString(){
StringBuffer sb = new StringBuffer();
sb.append("名称:" + this.dName);
sb.append(" || 厂家:" + this.address);
sb.append(" || 价格:" + this.price);
sb.append(" || 温度:" + this.temperature);
return sb.toString();
}
}
class Device{
protected String dName;
protected String address;
protected double price;
}
2.studentdate
import java.util.*;
public class Student{
private String stuNo;
private int score;
public void setStuNo(String stuNo){
this.stuNo = stuNo;
}
public String getStuNo(){
return this.stuNo;
}
public void setScore(int score){
this.score = score;
}
public int getScore(){
return this.score;
}
private static int getValue(List<Student> students,String request){
List<Integer> scores = new ArrayList<Integer>();
for(Student stu : students){
scores.add(stu.getScore);
}
if(request.equals("avg")){
int sum = 0;
for(int score : scores){
sum += score;
}
return sum/scores.size();
}else if (request.equals("max")){
for(int i = 0; i < scores.size() - 1; i++){
if(scores.get(i) > scores.get(i + 1)){
int temp = scores.get(i);
scores.set(i, scores.get(i + 1));
scores.set(i + 1, temp);
}
}
return scores.get(scores.size() - 1);
}else if (request.equals("min")){
for(int i = 0; i < scores.size() - 1; i++){
if(scores.get(i) < scores.get(i + 1)){
int temp = scores.get(i);
scores.set(i, scores.get(i + 1));
scores.set(i + 1, temp);
}
}
return scores.get(scores.size() - 1);
}
return 0;
}
public static void main(String args[]) throws Exception {
String fileName = "studentdate";
FileInputStream fis = new FileInputStream(fileName);
DateInputStream dis = new DateInputStream(fis);
List<Student> students = new ArrayList<Student>();
for(int i = 0; i < 10; i++){
Student student = new Student();
String stuNo = dis.readUTF();
String score = dis.readInt();
student.setStuNo(stuNo);
student.setScore(score);
students.add(student);
System.out.println("成绩:" + student.getScore() + " || 学号:" + student.getStuNo());
System.out.println("最高成绩:" + getValue(students, "max"));
System.out.println("最低成绩:" + getValue(students, "min"));
System.out.println("平均成绩:" + getValue(students, "avg"));
}
}
}
public class AirCondition extends Device {
private String dName;//空调名称
private String address;//空调厂家
private double price;//空调价格
private int temperature;//空调的温度
public AirCondition(String dName, String address,double price,int temperature) {
this.dName = dName;
this.address = address;
this.price = price;
this.temperature = temperature;
}
//给空调升温temp度
public void raiseTemp (int temp) {
this.temperature += temp;
}
//给空调降温temp度
public double lowerTemp(int temp) {
return this.temperature -= temp;
}
public String getdName() {
return dName;
}
public void setdName(String dName) {
this.dName = dName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getTemperature() {
return temperature;
}
public void setTemperature(int temperature) {
this.temperature = temperature;
}
public String toString() {
StringBuffer r = new StringBuffer();
r.append("设备名称: " + getdName() + "\n");
r.append("出厂厂家: " + getAddress() + "\n");
r.append("价格: " + getPrice() + "\n");
r.append("初始温度: " + getTemperature() + "\n");
return r.toString();
}
}
第二题
import java.io.DataInputStream;
import java.io.FileInputStream;
public class Score {
private Object[][] data = new Object[10][2];//存放学好和密码
public static void main(String[] args) {
new Score().readData();
}
//读取数据
public void readData(){
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream("studentdata"));
for(int i = 0; i < data.length; i++){
data[i][0] = dis.readUTF();//学好
data[i][1] = dis.readInt();//密码
}
//调用计算
calc();
} catch (Exception e) {
e.printStackTrace();
}
}
//计算
public void calc(){
int total = 0;
double avg = 0;
int max = 0;
int min = 0;
Object[] temp;
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data.length - 1; j++) {
if((Integer)(data[j][1]) > (Integer)(data[j + 1][1])){
temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
for (int i = 0; i < data.length; i++) {
total += (Integer)data[i][1];
System.out.println("学号: " + data[i][0] + "\t" + "分数: " + data[i][1]);
}
avg = total / data.length;//平均
min = (Integer)data[0][1];
max = (Integer)data[data.length - 1][1];
System.out.println("最高分: " + max);
System.out.println("最低分: " + min);
System.out.println("平均分: " + avg);
}
} 赞同0| 评论 检举 | 2012-2-11 00:42 hugo890308 | 三级
1.AirCondition
public class AirCondition extends Device{
private int temperature;
public AirCondition(){}
public AirCondition(String dName,String address,double price,int temperature){
this.dName = dName;
this.address = address;
this.price = price;
this.temperature = temperature;
}
public void raiseTemp(int temp){
this.temperature += temp;
}
public double lowerTemp(int temp){
return this.temperature - temp;
}
public String toString(){
StringBuffer sb = new StringBuffer();
sb.append("名称:" + this.dName);
sb.append(" || 厂家:" + this.address);
sb.append(" || 价格:" + this.price);
sb.append(" || 温度:" + this.temperature);
return sb.toString();
}
}
class Device{
protected String dName;
protected String address;
protected double price;
}
2.studentdate
import java.util.*;
public class Student{
private String stuNo;
private int score;
public void setStuNo(String stuNo){
this.stuNo = stuNo;
}
public String getStuNo(){
return this.stuNo;
}
public void setScore(int score){
this.score = score;
}
public int getScore(){
return this.score;
}
private static int getValue(List<Student> students,String request){
List<Integer> scores = new ArrayList<Integer>();
for(Student stu : students){
scores.add(stu.getScore);
}
if(request.equals("avg")){
int sum = 0;
for(int score : scores){
sum += score;
}
return sum/scores.size();
}else if (request.equals("max")){
for(int i = 0; i < scores.size() - 1; i++){
if(scores.get(i) > scores.get(i + 1)){
int temp = scores.get(i);
scores.set(i, scores.get(i + 1));
scores.set(i + 1, temp);
}
}
return scores.get(scores.size() - 1);
}else if (request.equals("min")){
for(int i = 0; i < scores.size() - 1; i++){
if(scores.get(i) < scores.get(i + 1)){
int temp = scores.get(i);
scores.set(i, scores.get(i + 1));
scores.set(i + 1, temp);
}
}
return scores.get(scores.size() - 1);
}
return 0;
}
public static void main(String args[]) throws Exception {
String fileName = "studentdate";
FileInputStream fis = new FileInputStream(fileName);
DateInputStream dis = new DateInputStream(fis);
List<Student> students = new ArrayList<Student>();
for(int i = 0; i < 10; i++){
Student student = new Student();
String stuNo = dis.readUTF();
String score = dis.readInt();
student.setStuNo(stuNo);
student.setScore(score);
students.add(student);
System.out.println("成绩:" + student.getScore() + " || 学号:" + student.getStuNo());
System.out.println("最高成绩:" + getValue(students, "max"));
System.out.println("最低成绩:" + getValue(students, "min"));
System.out.println("平均成绩:" + getValue(students, "avg"));
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1.AirCondition
public class AirCondition extends Device{
private int temperature;
public AirCondition(){}
public AirCondition(String dName,String address,double price,int temperature){
this.dName = dName;
this.address = address;
this.price = price;
this.temperature = temperature;
}
public void raiseTemp(int temp){
this.temperature += temp;
}
public double lowerTemp(int temp){
return this.temperature - temp;
}
public String toString(){
StringBuffer sb = new StringBuffer();
sb.append("名称:" + this.dName);
sb.append(" || 厂家:" + this.address);
sb.append(" || 价格:" + this.price);
sb.append(" || 温度:" + this.temperature);
return sb.toString();
}
}
class Device{
protected String dName;
protected String address;
protected double price;
}
2.studentdate
import java.util.*;
public class Student{
private String stuNo;
private int score;
public void setStuNo(String stuNo){
this.stuNo = stuNo;
}
public String getStuNo(){
return this.stuNo;
}
public void setScore(int score){
this.score = score;
}
public int getScore(){
return this.score;
}
private static int getValue(List<Student> students,String request){
List<Integer> scores = new ArrayList<Integer>();
for(Student stu : students){
scores.add(stu.getScore);
}
if(request.equals("avg")){
int sum = 0;
for(int score : scores){
sum += score;
}
return sum/scores.size();
}else if (request.equals("max")){
for(int i = 0; i < scores.size() - 1; i++){
if(scores.get(i) > scores.get(i + 1)){
int temp = scores.get(i);
scores.set(i, scores.get(i + 1));
scores.set(i + 1, temp);
}
}
return scores.get(scores.size() - 1);
}else if (request.equals("min")){
for(int i = 0; i < scores.size() - 1; i++){
if(scores.get(i) < scores.get(i + 1)){
int temp = scores.get(i);
scores.set(i, scores.get(i + 1));
scores.set(i + 1, temp);
}
}
return scores.get(scores.size() - 1);
}
return 0;
}
public static void main(String args[]) throws Exception {
String fileName = "studentdate";
FileInputStream fis = new FileInputStream(fileName);
DateInputStream dis = new DateInputStream(fis);
List<Student> students = new ArrayList<Student>();
for(int i = 0; i < 10; i++){
Student student = new Student();
String stuNo = dis.readUTF();
String score = dis.readInt();
student.setStuNo(stuNo);
student.setScore(score);
students.add(student);
System.out.println("成绩:" + student.getScore() + " || 学号:" + student.getStuNo());
System.out.println("最高成绩:" + getValue(students, "max"));
System.out.println("最低成绩:" + getValue(students, "min"));
System.out.println("平均成绩:" + getValue(students, "avg"));
}
}
}
ps:DateInputStream的读入不肯定,不常使,没仔细查,应该是这样,求分数!!
public class AirCondition extends Device{
private int temperature;
public AirCondition(){}
public AirCondition(String dName,String address,double price,int temperature){
this.dName = dName;
this.address = address;
this.price = price;
this.temperature = temperature;
}
public void raiseTemp(int temp){
this.temperature += temp;
}
public double lowerTemp(int temp){
return this.temperature - temp;
}
public String toString(){
StringBuffer sb = new StringBuffer();
sb.append("名称:" + this.dName);
sb.append(" || 厂家:" + this.address);
sb.append(" || 价格:" + this.price);
sb.append(" || 温度:" + this.temperature);
return sb.toString();
}
}
class Device{
protected String dName;
protected String address;
protected double price;
}
2.studentdate
import java.util.*;
public class Student{
private String stuNo;
private int score;
public void setStuNo(String stuNo){
this.stuNo = stuNo;
}
public String getStuNo(){
return this.stuNo;
}
public void setScore(int score){
this.score = score;
}
public int getScore(){
return this.score;
}
private static int getValue(List<Student> students,String request){
List<Integer> scores = new ArrayList<Integer>();
for(Student stu : students){
scores.add(stu.getScore);
}
if(request.equals("avg")){
int sum = 0;
for(int score : scores){
sum += score;
}
return sum/scores.size();
}else if (request.equals("max")){
for(int i = 0; i < scores.size() - 1; i++){
if(scores.get(i) > scores.get(i + 1)){
int temp = scores.get(i);
scores.set(i, scores.get(i + 1));
scores.set(i + 1, temp);
}
}
return scores.get(scores.size() - 1);
}else if (request.equals("min")){
for(int i = 0; i < scores.size() - 1; i++){
if(scores.get(i) < scores.get(i + 1)){
int temp = scores.get(i);
scores.set(i, scores.get(i + 1));
scores.set(i + 1, temp);
}
}
return scores.get(scores.size() - 1);
}
return 0;
}
public static void main(String args[]) throws Exception {
String fileName = "studentdate";
FileInputStream fis = new FileInputStream(fileName);
DateInputStream dis = new DateInputStream(fis);
List<Student> students = new ArrayList<Student>();
for(int i = 0; i < 10; i++){
Student student = new Student();
String stuNo = dis.readUTF();
String score = dis.readInt();
student.setStuNo(stuNo);
student.setScore(score);
students.add(student);
System.out.println("成绩:" + student.getScore() + " || 学号:" + student.getStuNo());
System.out.println("最高成绩:" + getValue(students, "max"));
System.out.println("最低成绩:" + getValue(students, "min"));
System.out.println("平均成绩:" + getValue(students, "avg"));
}
}
}
ps:DateInputStream的读入不肯定,不常使,没仔细查,应该是这样,求分数!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |