成都创新互联网站制作重庆分公司

java作业代码 java代码详解

JAVA作业求助

做个练习,仅供参考。

创新互联建站网站建设提供从项目策划、软件开发,软件安全维护、网站优化(SEO)、网站分析、效果评估等整套的建站服务,主营业务为成都网站建设、成都做网站,app软件开发公司以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。创新互联建站深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

代码如下:

package com.hello.algorithm;

import java.util.Arrays;

import java.util.Scanner;

public class InsertSort {

public static void main(String[] args) {

// 创建一个数组存储成绩,需要指定长度

int[] scores = new int[6]; //假设存储6个成绩:100 99 85 82 63 60

int index = 0;

while(index scores.length) {

// 控制台接收成绩

System.out.print("请输入成绩(100 99 85 82 63 60):");

Scanner scanner = new Scanner(System.in); // 随机插入成绩

int s = scanner.nextInt();

// 将接收的成绩与数组中的所有成绩进行比较,按照从大到小的规则,找到插入位置,然后插入数组

int insertPos = 0;

int temp = 0;

int temp2 = 0;

// 第一遍遍历数组找到插入位置

for(int i = 0; i scores.length; i++){

if(s scores[i]){

insertPos = i;

temp = scores[i];

scores[i] = s;

break;

}

}

// 第二遍遍历从插入位置整体后移

for(int i = 0; i scores.length; i++){

// 插入位置后面的元素整体后移

if(i insertPos){

temp2 = scores[i];

scores[i] = temp;

temp = temp2;

}

}

// 打印该成绩的插入位置以及打印整个数组

System.out.println("插入成绩的下标:" + insertPos);

System.out.println(Arrays.toString(scores));

index++;

}

}

}

运行结果如下:

请输入成绩(100 99 85 82 63 60):63

插入成绩的下标:0

[63, 0, 0, 0, 0, 0]

请输入成绩(100 99 85 82 63 60):60

插入成绩的下标:1

[63, 60, 0, 0, 0, 0]

请输入成绩(100 99 85 82 63 60):82

插入成绩的下标:0

[82, 63, 60, 0, 0, 0]

请输入成绩(100 99 85 82 63 60):99

插入成绩的下标:0

[99, 82, 63, 60, 0, 0]

请输入成绩(100 99 85 82 63 60):85

插入成绩的下标:1

[99, 85, 82, 63, 60, 0]

请输入成绩(100 99 85 82 63 60):100

插入成绩的下标:0

[100, 99, 85, 82, 63, 60]

Process finished with exit code 0

Java的作业,新手不会做,求代码

class Person{

string name;

int age;

string address;

public String toString(){

return "姓名:"+name+" 家庭住址:"+address+" 年龄:"+age;

}

public Person(string name,int age,string address){

this.name = name;

this.age = age;

this.address = address;

}

}

class Employee extends Person{

int num;

public Employee(string a,int b,string c,int n){

name = a;

age = b;

address = c;

num = n;

}

}

class Student extends Person{

public Student(string a,int b,string c){

name = a;

age = b;

address = c;

}

}

public class

Java实验,代码怎么写?

Shape.java接口代码

public interface Shape {

public static final double PI = 3.14d;

public double area();

}

Circle.java圆类代码

public class Circle implements Shape {

private double radius;

public Circle(double radius) {

  this.radius = radius;

}

@Override

public double area() {

  return PI * this.radius * this.radius;

}

public double perimeter() {

  return 2 * PI * this.radius;

}

}

Cylinder.java圆柱体类代码

public class Cylinder extends Circle {

private double height;

public Cylinder(double radius, double height) {

  super(radius);

  this.height = height;

}

public double area() {

  return 2 * super.area() + super.perimeter() * this.height;

}

public double volume() {

  return super.area() * this.height;

}

}

X5_3_6.java主类代码

public class X5_3_6 {

public static void main(String[] args) {

  Circle cir1 = new Circle(5);

  System.out.println("圆的面积为:" + cir1.area());

  System.out.println("圆的周长为:" + cir1.perimeter());

  Cylinder cy1 = new Cylinder(10, 15);

  System.out.println("圆柱体的表面积为:" + cy1.area());

  System.out.println("圆柱体的体积为:" + cy1.volume());

}

}

上面是我写的代码,下图是执行结果,麻烦看一下,是否可以。

java作业,利用接口实现多态

代码如下:

interface Shape {

float perimeter();

}

class Sequare implements Shape {

private float a;

public Sequare(float a) {

super();

this.a = a;

}

@Override

public float perimeter() {

return 4 * a;

}

}

class Rou implements Shape {

private float r;

public Rou(float r) {

this.r = r;

}

@Override

public float perimeter() {

return 2 * 3.14f * r;

}

}

public class MainClass {

public static float getPerimeter(Shape s) {

return s.perimeter();

}

public static void main(String[] args) {

Sequare s = new Sequare(20);

System.out.println("Square周长:" + getPerimeter(s));

Rou r = new Rou(15.5f);

System.out.println("Rou周长:" + getPerimeter(r));

}

}

运行结果:


网页标题:java作业代码 java代码详解
文章起源:http://cxhlcq.com/article/docigcj.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部