永发信息网

java作业

答案:6  悬赏:60  手机版
解决时间 2021-01-31 05:09
  • 提问者网友:遮云壑
  • 2021-01-31 00:43
百分悬赏,本人学JAVA才3天,这是老师布置的作业7个题目,老师教得非常快.我现在打代码还没什么头绪..谁帮我做下.让我研究下代码..
1 完成数组int[] a = {100,40, 60, 87, 34, 11, 56, 0}的快速排序、冒泡排序;

2采用折半查找的算法,在数组中查询到某个数;

3在中文环境下,有字符串,将其每个字节的数据相加求和。

4将一个数组中值=0的项去掉,将不为0的值存入一个新的数组,比如:
int a[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
生成的新数组为:
int b[]={1,3,4,5,6,6,5,4,7,6,7,5}

5定义10个长度的Student数组,将10个Student对象的年龄全部加1,然后把10个Student对象的详细信息逐行打印出来(数组和ArrayList实现)。

6有工人,农民,教师,科学家,服务生,其中,工人,农民,服务生只有基本工资.教师除基本工资外,还有课酬(元/天),科学家除基本工资外,还有年终奖,请你写出相关类,将各种类型的员工的全年工资打印出来;

7完成一个打印类,要求能用同名方法打印不同的类型的输入参数;
write(int s);
write(float s);
write(String s);
最佳答案
  • 五星知识达人网友:你可爱的野爹
  • 2021-01-31 02:22
下面一道一道的完成:
第一题:
1)快速排序
package src;



public class QuickSort { //快速排序

public static void main(String[] args) {

int[] a = {100,40, 60, 87, 34, 11, 56, 0};
int left = 0 ;
int right = a.length - 1;
quickSort(a , left , right);

//循环显示数组
for(int i = 0 ; i < a.length ; i++){
System.out.print(a[i] + " ");
}
}

public static void quickSort(int[] a , int left , int right ){
int middle , temp ;
int i = left ;
int j = right ;
middle = a[left];
while(i < j){
while((a[i] < middle) && (i < right)){
i++;
}
while((a[j] > middle) && (j > left)){
j--;
}
if(i <= j){
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}

}
if(left < j){
quickSort(a , left , j);
}
if(right > i){
quickSort(a , i , right);
}
}
}

2)冒泡排序

package src;

public class PopSort {
public static void main(String[] args) {

int[] a = {100,40, 60, 87, 34, 11, 56, 0};
for(int i = 0 ; i < a.length ; i++)
for(int j = 0 ; j < a.length - i -1 ; j++){
if(a[j] > a[j+1]){
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
for(int k = 0 ; k < a.length ; k++){
System.out.print(a[k] + " ");
}
}
}

第二题

package src;

public class BinarySearch {


public static void main(String[] args) {

int[] datas = { 1, 2, 3, 4, 5, 6 ,7, 8, 9, 10}; //二分法必须应用在有序数组
int key = 9;
System.out.println("the result is " + bSearch(datas , key));

}
public static int bSearch(int[] datas , int key){
int index = -1;
int low = 0;
int high = datas.length - 1;
while (low <= high) {
int middle = (low + high) >> 1;
if (key == datas[middle]) {
index = middle;
break;
} else if (key > datas[middle]) {
low = middle + 1;
} else if (key < datas[middle]) {
high = middle - 1;
}
}
return index;
}

}

第三题

package src;

public class CharCode {
public static void main(String[] args){
String str = "hello world";
char[] strToChar = str.toCharArray();
int charSum = 0;
for(int i = 0 ; i < strToChar.length ; i++){
charSum = charSum + strToChar[i];
}
System.out.print("the charArray's Sum is " + charSum);
}

}

第四题

package src;

public class NewArray {


public static void main(String[] args) {
int a[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
int[] b = new int[a.length];
int j = 0;
for(int i = 0 ; i < a.length ; i++){
if(a[i] != 0){
b[j] = a[i];
j++;
}
}
for(int k = 0 ; k < j ; k++){
System.out.print(b[k] + " ");
}

}

}

第五题:

ArrayList实现

package src;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class StudentList {


public String name;
public int age;
public String grade;
public String school;

public static List stuList = null;

public StudentList(String name, int age, String grade, String school) {
super();
this.name = name;
this.age = age;
this.grade = grade;
this.school = school;
}

public static void main(String[] args) {
stuList = new ArrayList();
stuList.add(new StudentList("aaa",13 , "6" ,"tianjin"));
stuList.add(new StudentList("kkk",13 , "6" ,"tianjin"));
stuList.add(new StudentList("ddd",13 , "6" ,"shenzhen"));
stuList.add(new StudentList("ccc",13 , "6" ,"beijing"));
stuList.add(new StudentList("eee",15 , "6" ,"tianjin"));
stuList.add(new StudentList("aaa",13 , "6" ,"tianjin"));
stuList.add(new StudentList("kkk",13 , "6" ,"tianjin"));
stuList.add(new StudentList("ddd",13 , "6" ,"shenzhen"));
stuList.add(new StudentList("ccc",13 , "6" ,"beijing"));
stuList.add(new StudentList("eee",15 , "6" ,"tianjin"));

for(Iterator it = stuList.listIterator() ; it.hasNext(); ){
StudentList stu = (StudentList)it.next();
System.out.println(stu.name + " ," +stu.age + " ," + stu.grade + " ," + stu.school );
}
}

}

数组实现:

package src;

public class StudentArray {


public String name;
public int age;
public String grade;
public String school;

public StudentArray(String name, int age, String grade, String school) {
super();
this.name = name;
this.age = age;
this.grade = grade;
this.school = school;
}

public static void main(String[] args) {
StudentArray[] stua = {new StudentArray("aaa" ,13 ,"4" ,"beijing") , new StudentArray("bbb" ,14 ,"4" ,"dalian") ,new StudentArray("ddd" ,13 ,"4" ,"beijing") ,new StudentArray("ccc" ,13 ,"4" ,"tianjin") ,new StudentArray("eee" ,20 ,"8" ,"beijing") ,new StudentArray("fff" ,23 ,"10" ,"beijing") ,new StudentArray("aaa" ,13 ,"4" ,"beijing") ,new StudentArray("aaa" ,13 ,"4" ,"beijing") ,new StudentArray("kkk" ,13 ,"4" ,"beijing") ,new StudentArray("www" ,13 ,"4" ,"beijing") ,new StudentArray("aaa" ,13 ,"4" ,"beijing") };
for(int i = 0 ; i < stua.length ; i++){
System.out.println(stua[i].name + " , " + stua[i].age + " , " +stua[i].grade + " , " +stua[i].school);
}
}

}

第六题:

package job;

public abstract class Job { //这个是父类抽象类,表示职业类

public double baseWage; //基本工资

public Job(double baseWage) {
super();
this.baseWage = baseWage;
}

public double getBaseWage() {
return baseWage;
}

public void setBaseWage(double baseWage) {
this.baseWage = baseWage;
}

public double getYearWage(){ //一年的工资
return baseWage * 12 ;
}
}

package job;

public class Farmer extends Job {

public Farmer(double baseWage) {
super(baseWage);
// TODO 自动生成构造函数存根
}

public double getYearWage() {
// TODO 自动生成方法存根
return super.getYearWage();
}

}

package job;

public class Scientist extends Job {
public String yearendBonus; //年终奖
public Scientist(double baseWage) {
super(baseWage);
//
}

public String getYearendBonus() {
return yearendBonus;
}

public void setYearendBonus(String yearendBonus) {
this.yearendBonus = yearendBonus;
}

public double getYearWage() {
//
return super.getYearWage();
}

}

package job;

public class Teacher extends Job {
public double reward; //每天的课酬

public Teacher(double baseWage) {
super(baseWage);
// TODO 自动生成构造函数存根
}

public double getReward() {
return reward;
}

public void setReward(double reward) {
this.reward = reward;
}

public double getYearWage() {
// TODO 自动生成方法存根
return super.getYearWage();
}

}

package job;

public class Waiter extends Job {

public Waiter(double baseWage) {
super(baseWage);
// TODO 自动生成构造函数存根
}

public double getYearWage() {
// TODO 自动生成方法存根
return super.getYearWage();
}

}

package job;

public class Worker extends Job {

public Worker(double baseWage) {
super(baseWage);
// TODO 自动生成构造函数存根
}

public double getYearWage() {
// TODO 自动生成方法存根
return super.getYearWage();
}

}

package job;

public class Manager { //这是一个管理各种职业工资的类,这是一个含有具体业务的类


public static void main(String[] args) {

Teacher tjob = new Teacher(5000); //教师的工资是5000美金一个月
printSalary(tjob);
Scientist sjob = new Scientist(8000);
printSalary(sjob);
Farmer fjob = new Farmer(4000);
printSalary(fjob);
Waiter wjob = new Waiter(3500);
printSalary(wjob);
Worker workjob = new Worker(4500);
printSalary(workjob);

}

public static void printSalary(Job job) {
System.out.println(job.getYearWage());
}

}

第七题:

package src;

public class Writer {


public static void main(String[] args) {
int i = 911;
float j = 911.911f;
String str = "hello world";
write(i);
write(j);
write(str);
}

public static void write(int s) {
System.out.print("int: " + s + "\n");
}

public static void write(float s) {
System.out.print("float:" + s + "\n");
}

public static void write(String s) {
System.out.print("String:" + s + "\n");
}

}

好困啊,睡觉了
全部回答
  • 1楼网友:行雁书
  • 2021-01-31 05:50
想学Java就自己写!!!
  • 2楼网友:十鸦
  • 2021-01-31 05:43
都是很简单的题目,我建议你题目分开求比较快
  • 3楼网友:北方的南先生
  • 2021-01-31 05:25
今天下班了 明天来看看
  • 4楼网友:等灯
  • 2021-01-31 04:23
1-4: public class ArrayString { public static int add(String s) {//在中文环境下,有字符串,将其每个字节的数据相加求和 int k = 0; byte[] a = s.getBytes(); // for (byte item : a) // k += item; for(int i=0;i<a.length;i++) { k+=a[i]; } return k; } public static void QKSort(int q[])//快速排序,冒泡排序 { int temp; for(int i=0;i<q.length;i++) { for(int j=0;j<q.length-i-1;j++) { if(q[j]>q[j+1]) { temp = q[j]; q[j] = q[j+1]; q[j+1]= temp; } } } } //折半查找 public static int binaryfind(int v[],int leng,int element) { int low,high,mid; low = 0; high = leng-1; while(low<=high) { mid=(low+high)/2; //折半 if(element<v[mid]) { high=mid-1; //前半部分查找 } else if(element>v[mid]) { low=mid+1; //后半部分查找 } else return mid;//该元素在数组中的下标 } return 0; //没找到 } //将数组中为0的去掉 public static int[] remve0(int a0[]) { int d[]=null; List<Integer> al = new LinkedList<Integer>(); for(int t=0;t<a0.length;t++) { if(a0[t]!=0) al.add(Integer.valueOf(a0[t])); } d = new int[al.size()]; for(int c=0;c<al.size();c++) { d[c] = al.get(c).intValue(); } return d; } public static void main(String[] args) { // TODO Auto-generated method stub int[] a = {100,40, 60, 87, 34, 11, 56, 0}; QKSort(a); for(int e=0;e<a.length;e++) { System.out.println("冒泡排序后元素的值:"+a[e]); } int seat= binaryfind(a,a.length,40); if(seat!=0){ System.out.println("在数组a中找到了元素40其下标为:"+seat); } int b[] =remve0(a); for(int e=0;e<b.length;e++) { System.out.println("新的数组各元素的值:"+b[e]); } System.out.println("3每个字节的数据相加求和:"+add("你好")); } } 5: import java.util.ArrayList; public class Student{ private long sNo;//学生的学号 private String sClass;//表示学生的班级 private int sage; private String name; private char sex; Student(String name,char sex,int age,long sNo,String sClass) { // TODO Auto-generated constructor stub this.name = name; this.sex = sex; this.sage = sage; this.sNo = sNo; this.sClass = sClass; } void add1age() { this.sage = this.sage+1; } public String toString(){ return "姓名:"+this.name+" 性别:"+this.sex +" 年龄:"+this.sage+" 学号:"+this.sNo+" 班级:"+this.sClass; } public static void main(String[] args) { Object[] param ={ new Student("a1",'男',11,200901,"218"), new Student("a2",'男',12,200902,"218"), new Student("a3",'男',13,200903,"218"), new Student("a4",'男',14,200904,"218"), new Student("a5",'男',15,200905,"218"), new Student("a6",'男',16,200906,"218"), new Student("a7",'男',17,200907,"218"), new Student("a8",'男',18,200908,"218"), new Student("a9",'男',19,200909,"218"), new Student("a10",'男',20,2009010,"218") }; ArrayList<Student> al = new ArrayList<Student>(); for(int p=0;p<param.length;p++) { al.add((Student) param[p]); } for(int i=0;i<al.size();i++) { al.get(i).add1age();//将年龄加1 System.out.println("Student a"+i+al.get(i).toString()); } } } 6: public class Woker { private int basepay;//基本工资 public void setbasepay(int basepay) { this.basepay = basepay; } public int getyearpay() { return this.basepay*12; } } public class Farmer { private int basepay;//基本工资 public void setbasepay(int basepay) { this.basepay = basepay; } public int getyearpay() { return this.basepay*12; } } public class Teacher { private int basepay;//基本工资 private int reward;//课酬 public void setbasepay(int basepay,int reward) { this.basepay = basepay; this.reward = reward; } public int getyearpay() { return this.basepay*12+reward; } } public class Scientist{ private int basepay;//基本工资 private int prize;//年终奖 public void setbasepay(int basepay,int prize) { this.basepay = basepay; this.prize = prize; } public int getyearpay() { return this.basepay*12+prize; } } public class Waiter { private int basepay; public void setbasepay(int basepay) { this.basepay = basepay; } public int getyearpay() { return this.basepay*12; } } public class Test{ public static void main(String[] args) { Farmer f = new Farmer(); f.setbasepay(1200); System.out.println("农民的全年工资:"+f.getyearpay()); Woker w = new Woker(); w.setbasepay(1800); System.out.println("工人的全年工资:"+w.getyearpay()); Waiter t = new Waiter(); t.setbasepay(1800); System.out.println("服务生的全年工资:"+t.getyearpay()); Teacher teacher = new Teacher(); teacher.setbasepay(2000,4000); System.out.println("教师的全年工资:"+teacher.getyearpay()); Scientist s = new Scientist(); s.setbasepay(2200,7000); System.out.println("科学家的全年工资:"+s.getyearpay()); } } 7: public class PrintClass { public static void write(int s) { System.out.println("int"+s); } public static void write(float s) { System.out.println("float"+s); } public static void write(String s) { System.out.println("String"+s); } public static void main(String[] args) { write(1); write(0.11f); write("string"); } }
  • 5楼网友:鱼忧
  • 2021-01-31 03:56
//第四题 import java.util.*; public class Test { private int[] i = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; private ArrayList getList(){ ArrayList list = new ArrayList(); for (int j = 0; j<i.length; j++) { if(i[j] != 0) list.add(i[j]); } return list; } private int[] getNewArray(){ ArrayList list = this.getList(); int[] j = new int[list.size()]; for (int i = 0; i<list.size(); i++) { j[i] = Integer.parseInt(String.valueOf(list.get(i))); } list = null; return j; } public static void main (String [] args){ Test t = new Test(); int[] j = t.getNewArray(); for (int i = 0; i<j.length; i++) { System.out.println (j[i]); } } }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯