重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
代码如下,Rectangle类在java.lang包下有同名类,如需引包请注意,望采纳。
创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站建设、网站设计、漠河网络推广、微信小程序定制开发、漠河网络营销、漠河企业策划、漠河品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供漠河建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com
补充:
Rectangle类
package com.baidu.question183173554138589044;
public class Rectangle {
private double width;
private double height;
public void setRectangle(int w, int h){
if(w0 h0){
width = w;
height = h;
}else{
System.out.println("错误:长宽需要大于0");
}
}
public double getArea() {
return width*height;
}
}
运行主类
package com.baidu.question183173554138589044;
public class Main {
public static void main(String[] args) {
Rectangle rect = new Rectangle();
rect.setRectangle(5,6);
System.out.println(rect.getArea());
}
}
运行代码
具体的代码如下:
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);
}
}
运行结果
按照题目要求编写的用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());
}
}
}