重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java习题及代码 java代码题库

java练习题求解希望有完整代码

运行代码

创新互联建站服务项目包括尉犁网站建设、尉犁网站制作、尉犁网页制作以及尉犁网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,尉犁网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到尉犁省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

具体的代码如下:

import java.util.HashMap;

public class SearchDemo {

public HashMapInteger,Integer countMap(char[] arr){

HashMap countMap = new HashMapInteger, Integer();

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

if(!countMap.containsKey(arr[i])){

countMap.put(arr[i],1);

}else{

int value = (int)countMap.get(arr[i]) + 1;

countMap.put(arr[i],value);

}

}

return countMap;

}

public static void main(String[] args) {

char[] arr={'A','1','R','!','e','','A','5','A','g','#','D'};

int count = new SearchDemo().countMap(arr).get('A');

System.out.println("其中'A'字符的个数是:" + count);

}

}

运行结果

java练习题求完整代码

按照题目要求编写的用javaBean规范设计的学生类Student的Java程序如下

需要创建user.java.test包,把Student.java文件和Test.java文件放入包中,编译Student.java文件并且编译运行Test.java文件得到运行结果

Student.java文件代码如下

package user.java.test;

import java.io.Serializable;

public class Student implements Serializable{

private static final long serialVersionUID = 1L;

private String no;

private String name;

private double score;

public Student(){}

public Student(String no,String name,double score){

this.no=no;

this.name=name;

this.score=score;

}

public String getNo(){ return no;}

public void setNo(String no){ this.no=no;}

public String getName(){ return name;}

public void setName(String name){ this.name=name;}

public double getScore(){ return score;}

public void setScore(double score){ this.score=score;}

public String toString(){

return "学号:"+no+",姓名:"+name+",成绩:"+score;

}

public static double getAvg(Student[] sArray){

double sum=0,avg;

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

sum=sum+sArray[i].getScore();

}

avg=sum/sArray.length;

return avg;

}

}

Test.java文件代码如下

package user.java.test;

public class Test{

public static void main(String[] args){

Student[] sArray=new Student[5];

sArray[0]=new Student("001","张三",89.5);

sArray[1]=new Student("002","李四",82.5);

sArray[2]=new Student("003","王五",93);

sArray[3]=new Student("004","赵六",73.5);

sArray[4]=new Student("005","孙七",66);

System.out.println("这些学生的平均分:"+Student.getAvg(sArray));

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

System.out.println(sArray[i].toString());

}

}

}

请求大神帮我解答这道java编程题(填写代码)

此题的Employee类设计有缺陷,缺少员工薪水的属性。根据题意只能写出下面的程序:

abstract class Employee {

public abstract double earnings();

}

class YearWorker extends Employee {

@Override

public double earnings() {

return 0;

}

}

class MonthWorker extends Employee {

@Override

public double earnings() {

return 0;

}

}

class WeekWorker extends Employee {

@Override

public double earnings() {

return 0;

}

}

class Company {

Employee[] employee;

double salaries = 0;

Company(Employee[] employee) {

this.employee = employee;

}

public double salariesPay() {

salaries = 0;

//计算salaries

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

salaries += employee[i].earnings();

}

return salaries;

}

}

public class HardWork {

public static void main(String[] args) {

Employee[] employee = new Employee[20];

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

if(i % 3 == 0) {

employee[i] = new YearWorker();

}

else if(i % 3 == 1) {

employee[i] = new MonthWorker();

}

else if(i % 3 == 2) {

employee[i] = new WeekWorker();

}

}

Company company = new Company(employee);

System.out.println("公司年工资总额:" + company.salariesPay());

}

}

运行结果:

公司年工资总额:0.0

java实验题,求代码和运行方式!

下面是第一题,采纳此答案发第二题代码

========分割线=========

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;

public class Q1 {

public static void main(String[] args) {

try {

Scanner scanner=new Scanner(System.in);

String data = scanner.nextLine();

File file = new File("java\\code");

if (!file.exists()) {

file.mkdirs();

}

File file1=new File(file,"mydata.txt");

if (!file1.exists()) {

file1.createNewFile();

}

FileWriter fw = new FileWriter(file1.getAbsoluteFile());

BufferedWriter bw = new BufferedWriter(fw);

bw.write(data);

bw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}


本文标题:java习题及代码 java代码题库
URL链接:http://cqcxhl.cn/article/docopoe.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP