Java交通图代码实现的简单介绍
求教一个java问题 设计一个交通工具类Vehicle,其中的属性包括:速度speed、类别kind、颜色color;方法包括
没搞清楚get与set方法是怎样写的!
“只有客户发展了,才有我们的生存与发展!”这是创新互联的服务宗旨!把网站当作互联网产品,产品思维更注重全局思维、需求分析和迭代思维,在网站建设中就是为了建设一个不仅审美在线,而且实用性极高的网站。创新互联对成都做网站、成都网站设计、网站制作、网站开发、网页设计、网站优化、网络推广、探索永无止境。
先分析你的代码:涉及变量kind的方法是public String getKind(String string){return kind;} 下面又调用这个方法 veh.getKind("car"); 由你定义的方法看你传入的参数与返回的参数无关--【一个是string ,一个是kind】,即:即使调用这个方法也没有对kind这个变量赋值,因此kind 仍旧为默认值 null
正确写法:
class Vehicle
{
private static int speed;
private static String kind;
private static String color;
public void setSpeed(int s)
{
speed=s;
}
public void setColor(String c)
{
color=c;
}
public void setKind(String string){kind=string;} //这种赋值最好写成setXXX的形式
public static void main(String args[])
{
Vehicle veh=new Vehicle();
veh.setSpeed(100);
veh.setColor("black");
veh.setKind("car");
System.out.println("kind="+kind+"\nspeed="+speed+"\ncolor="+color);
}
}
java习题定义一个交通工具类Vehicle,包含属性(speed,name,color)和方法(start,stop,run)
1、Vehicle类所具有的属性题目没有给定数据类型,自主选择定义属性的数据类型。代码如下
public class Vehicle{
private double speed;//速度
private String name;//名字
private String color;//颜色
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
//start方法
public void start()
{
System.out.println("汽车,"+this.name+this.color+"启动。");
}
//stop方法
public void stop( )
{
System.out.println("汽车,"+this.name+this.color+"停止。");
}
//run方法
public void run( )
{
System.out.println("汽车,"+this.name+"行驶,速度 "+this.speed+"km/h");
}
public static void main(String[] args) {
//主方法,执行需要的操作
Vehicle v =new Vehicle();
v.setName("奔驰");
v.setColor("红色");
v.setSpeed(120);
v.start();
v.run();
v.stop();
}
}
2、在主方法程序中生成Vehicle的对象,通过set方法给对象赋值,运行效果
java难题,急,求代码?
接口和类全在一个包下,如果不在一个包下记得import调用,show的重写你根据需要自己改
//ShouFei 接口
public interface ShouFei {
public void fuFei(double mileage);
public void show();
}
//Bus类
public class Bus implements ShouFei{
String airConditioning;
double money;
public Bus(String airConditioning){
this.airConditioning = airConditioning;
}
@Override
public void fuFei(double mileage) {
if(airConditioning.equals("空调")){
money = 2;
}else{
money = 1;
}
}
@Override
public void show() {
System.out.println("公交车,"+airConditioning+",费用:"+money);
}
}
//Taxi类
public class Taxi implements ShouFei{
double mileage;
boolean isDay;
double money;
public Taxi(boolean isDay){
this.isDay = isDay;
}
@Override
public void fuFei(double mileage) {
this.mileage = mileage;
//判断是否为白天
if(isDay){
//判断里程数是否大于三公里
if(mileage3){
money=8;
}else{
money = 8 + (mileage-3)*1.5;
}
}else{
if(mileage3){
money=10;
}else{
money = 10 + (mileage-3)*1.5;
}
}
}
@Override
public void show() {
System.out.print("出租车,");
if(isDay){
System.out.print("白天");
}else{
System.out.print("晚上");
}
System.out.println(",里程数:"+mileage+"收费:"+money);
}
}
//测试类
public class Test {
public static void main(String[] args) {
Bus bus = new Bus("空调");
bus.fuFei(10);
bus.show();
Taxi taxi = new Taxi(true);
taxi.fuFei(20);
taxi.show();
}
}
求大神帮忙JAVA代码 实现下面图片所达到的效果。 要代码 最好简单点的
//Subject.java
/**
* 科目类
*/
public class Subject {
/** 名称 */
private String name;
/** 成绩 */
private int score;
public Subject(String name,int score){
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
import java.util.List;
//Student.java
/**
* 学生类
*/
public class Student{
/** 姓名 */
private String name;
/** 学科 */
private ListSubject subjects;
/** 总分 */
private int totalScore = 0;
/** 平均分 */
private int avgScore = 0;
public Student(String name,ListSubject subjects){
this.name = name;
this.subjects = subjects;
if(subjects != null subjects.size() 0){
for(Subject s : subjects){
totalScore += s.getScore();
}
avgScore = totalScore / subjects.size();
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ListSubject getSubjects() {
return subjects;
}
public void setSubjects(ListSubject subjects) {
this.subjects = subjects;
}
public int getTotalScore() {
return totalScore;
}
public int getAvgScore() {
return avgScore;
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
//StudentScore.java
/**
* 主测试程序
*/
public class StudentScore {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入课程数");
int subjectNum = scanner.nextInt();
System.out.println("请输入人数");
int stuNum = scanner.nextInt();
String[] subjectNames = new String[subjectNum];
for(int i = 0; i subjectNum; i++){
System.out.println("请输入第" + (i + 1) + "门科目名称");
subjectNames[i] = scanner.next();
}
String[] stuNames = new String[stuNum];
for(int i = 0; i stuNum; i++){
System.out.println("请输入第" + (i + 1) + "个学员名称");
stuNames[i] = scanner.next();
}
Student[] stuArr = new Student[stuNum];
for(int i = 0; i stuNum; i++){
ListSubject subjects = new ArrayListSubject();
for(int j = 0; j subjectNum; j++){
System.out.println("输入" + stuNames[i] + "的" + subjectNames[j] + "成绩");
int score = scanner.nextInt();
Subject subject = new Subject(subjectNames[j], score);
subjects.add(subject);
}
stuArr[i] = new Student(stuNames[i], subjects);
}
//按照总成绩从高到底排序
Arrays.sort(stuArr, new ComparatorStudent() {
@Override
public int compare(Student stu1, Student stu2) {
if(stu1.getTotalScore() stu2.getTotalScore()){
return 1;
} else if(stu1.getTotalScore() stu2.getTotalScore()){
return -1;
} else {
return 0;
}
}
});
//输出标题头
final String FORMAT = "\t";
System.out.print("姓名" + FORMAT);
for(int i = 0;i subjectNum; i++){
System.out.print(subjectNames[i] + FORMAT);
}
System.out.println("总分" + FORMAT + "平均分" + FORMAT + "排名");
//按照排名输出
for(int i = 0; i stuArr.length; i++){
System.out.print(stuArr[i].getName() + FORMAT);
for(int j = 0; j subjectNum; j++){
System.out.print(stuArr[i].getSubjects().get(j).getScore() + FORMAT);
}
System.out.println(stuArr[i].getTotalScore() + FORMAT + stuArr[i].getAvgScore() + FORMAT + "第" + (i + 1) + "名");
}
}
}
文章标题:Java交通图代码实现的简单介绍
标题来源:http://azwzsj.com/article/hcggcp.html