java例题代码 java代码笔试题
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题,求下题代码。
public class Product {
private String productName;
private double costPrice;
private double price;
private java.util.Date outDate;
private String batch;
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public double getCostPrice() {
return costPrice;
}
public void setCostPrice(double costPrice) {
this.costPrice = costPrice;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public java.util.Date getOutDate() {
return outDate;
}
public void setOutDate(java.util.Date outDate) {
this.outDate = outDate;
}
public String getBatch() {
return batch;
}
public void setBatch(String batch) {
this.batch = batch;
}
/**
* 判断产品是否过期
* @param product
* @return 是否过期
* @returnType boolean
*/
public boolean isOutDate(Product product){
return new java.util.Date().getTime() product.getOutDate().getTime();
}
/**
* 计算毛利润
* @param product
* @return 毛利润
* @returnType double
*/
public double getGrossProfit(Product product){
return product.getPrice() - product.getCostPrice();
}
/**
* 修改产品价格
* @param product
* @param price
*/
public void updateProductPrice(Product product, double price){
product.setPrice(price);
}
/**
* 打折出售
* @param product
* @param price
*/
public void discount(Product product, double discount){
product.setPrice(product.getPrice() * discount);
}
}
java例题代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Demo {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入密码:");
try{
String pass = in.readLine();
if(pass.length() 6 || pass.length() 4){
System.err.println("长度错误,密码长度必须是4-6位");
return;
}
int password = Integer.parseInt(pass);
System.out.println("信息合法");
}catch(Exception e){
System.err.println("错误,输入的密码必须为数字");
}
}
}
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?
创建一个名字为“ReportCard”的类,然后用下边的内容全部替换掉,你会成为全班最亮的仔。
import java.util.HashMap;
/**
* 学生成绩单
*/
public class ReportCard {
public static void main(String[] args) {
ReportCard reportCard = new ReportCard("张三", "070602213");
reportCard.set("语文", 80.0);
reportCard.set("数学", 59.5);
reportCard.set("英语", 66.0);
reportCard.set("java", 80, 99.0);
reportCard.set("数据库", 80, 66.0);
reportCard.set("毛概", null);
System.out.println(reportCard.getStudentName() + "语文分数:" + reportCard.get("语文"));
System.out.println(reportCard.getStudentName() + "数学考核结果:" + (reportCard.isPassed("数学") ? "合格" : "不合格"));
System.out.println(reportCard.getStudentName() + "期末是否挂科:" + (reportCard.isAllPassed() ? "否" : "是"));
}
// 学生姓名
private String studentName;
// 学生学号
private String studentNumber;
// 成绩单
private HashMapString, CourseResult cards = new HashMap();
public ReportCard() {
}
public ReportCard(String studentName, String studentNumber) {
this.studentName = studentName;
this.studentNumber = studentNumber;
}
public Double get(String courseName){
CourseResult courseResult = cards.get(courseName);
return courseResult == null ? Double.NaN : courseResult.getStudentScore();
}
public void set(String courseName, Double studentScore){
CourseResult courseResult = new CourseResult(courseName, studentScore);
cards.put(courseName, courseResult);
}
public void set(String courseName, double passMark, Double studentScore){
CourseResult courseResult = new CourseResult(courseName, passMark, studentScore);
cards.put(courseName, courseResult);
}
public boolean isPassed(String courseName){
return cards.get(courseName).isPassed();
}
public boolean isAllPassed(){
for(CourseResult cr : cards.values()){
if ( ! cr.isPassed()) {
return false;
}
}
return true;
}
public String getStudentName() {
return studentName;
}
public String getStudentNumber() {
return studentNumber;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
/**
* 课程
*/
class Course{
// 课程名称
protected String courseName;
// 及格分
protected double passMark = 60;
public Course(String courseName, Double passMark) {
this.courseName = courseName;
if ( passMark != null) {
this.passMark = passMark;
}
}
}
/**
* 课程成绩
*/
class CourseResult extends Course{
// 学生成绩
private Double studentScore;
public CourseResult(String courseName, Double studentScore) {
this(courseName, null, studentScore);
}
public CourseResult(String courseName, Double passMark, Double studentScore) {
super(courseName, passMark);
this.studentScore = studentScore == null ? Double.NaN : studentScore;
}
public boolean isPassed(){
return studentScore = passMark;
}
public String getCourseName() {
return courseName;
}
public double getPassMark() {
return passMark;
}
public Double getStudentScore() {
return studentScore;
}
}
当前题目:java例题代码 java代码笔试题
本文链接:http://myzitong.com/article/hiiids.html