//都是從新手過來的,以下代碼供參考
創(chuàng)新互聯(lián)自2013年起,公司以網(wǎng)站建設、成都網(wǎng)站設計、系統(tǒng)開發(fā)、網(wǎng)絡推廣、文化傳媒、企業(yè)宣傳、平面廣告設計等為主要業(yè)務,適用行業(yè)近百種。服務企業(yè)客戶上千多家,涉及國內多個省份客戶。擁有多年網(wǎng)站建設開發(fā)經(jīng)驗。為企業(yè)提供專業(yè)的網(wǎng)站建設、創(chuàng)意設計、宣傳推廣等服務。 通過專業(yè)的設計、獨特的風格,為不同客戶提供各種風格的特色服務。
//1.
public?class?BankAccount?{
private?static?String?acctnum;
private?static?double?money;
private?static?void?showAcct()?{
System.out.println("賬號為:?"?+?acctnum);
}
private?static?void?showMoney()?{
System.out.println("余額為:?"?+?money);
}
public?BankAccount(String?acc,?double?m)?{
this.acctnum?=?acc;
this.money?=?m;
}
public?static?void?main(String[]?args)?{
BankAccount?ba?=?new?BankAccount("626600018888",?5000.00);
ba.showAcct();
ba.showMoney();
}
}
//2.
public?class?Triangle?{
private?static?float?a;
private?static?float?b;
private?static?float?c;
public?Triangle(float?a,?float?b,?float?c)?{
this.a?=?a;
this.b?=?b;
this.c?=?c;
}
public?static?boolean?judgeTriangle(float?a,?float?b,?float?c)?{
if?((a??Math.abs(b?-?c)??a??b?+?c)
?(b??Math.abs(a?-?c)??b??a?+?c)
?(c??Math.abs(a?-?b)??c??a?+?b))
return?true;
else
return?false;
}
public?float?getCircumference()?{
return?this.a?+?this.b?+?this.c;
}
}
//3.
public?class?TestTriangle?{
public?static?void?main(String[]?args)?{
Triangle?t?=?new?Triangle(5.3f,7.8f,9.3f);
if(t.judgeTriangle(5.3f,7.8f,9.3f)){
System.out.print("能夠成三角形,周長為:?");
System.out.printf("%9.2f",t.getCircumference());}
else
System.out.println("不能構成三角形");
}
}
上面 wuzhikun12同學寫的不錯,但我想還不能運行,并且還不太完善。我給個能運行的:(注意:文件名為:Test.java)
//要實現(xiàn)對象間的比較,就必須實現(xiàn)Comparable接口,它里面有個compareTo方法
//Comparable最好使用泛型,這樣,無論是速度還是代碼量都會減少
@SuppressWarnings("unchecked")
class Student implements ComparableStudent{
private String studentNo; //學號
private String studentName; //姓名
private double englishScore; //英語成績
private double computerScore; //計算機成績
private double mathScore; //數(shù)學成績
private double totalScore; //總成績
//空構造函數(shù)
public Student() {}
//構造函數(shù)
public Student(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
this.studentNo = studentNo;
this.studentName = studentName;
this.englishScore = englishSocre;
this.computerScore = computerScore;
this.mathScore = mathScore;
}
//計算總成績
public double sum() {
this.totalScore = englishScore+computerScore+mathScore;
return totalScore;
}
//計算評測成績
public double testScore() {
return sum()/3;
}
//實現(xiàn)compareTO方法
@Override
public int compareTo(Student student) {
double studentTotal = student.getTotalScore();
return totalScore==studentTotal?0:(totalScorestudentTotal?1:-1);
}
//重寫toString方法
public String toString(){
return "學號:"+this.getStudentNo()+" 姓名:"+this.getStudentName()+" 英語成績:"+this.getEnglishScore()+" 數(shù)學成績:"+this.getMathScore()+" 計算機成績:"+this.getComputerScore()+" 總成績:"+this.getTotalScore();
}
//重寫equals方法
public boolean equals(Object obj) {
if(obj == null){
return false;
}
if(!(obj instanceof Student)){
return false;
}
Student student = (Student)obj;
if(this.studentNo.equals(student.getStudentName())) { //照現(xiàn)實來說,比較是不是同一個學生,應該只是看他的學號是不是相同
return true;
} else {
return false;
}
}
/*以下為get和set方法,我個人認為,totalScore的set的方法沒必要要,因為它是由其它成績計算出來的
在set方法中,沒設置一次值,調用一次sum方法,即重新計算總成績
*/
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
sum();
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
sum();
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
sum();
}
public double getComputerScore() {
return computerScore;
}
public void setComputerScore(double computerScore) {
this.computerScore = computerScore;
sum();
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
sum();
}
public double getTotalScore() {
return totalScore;
}
}
//Student子類學習委員類的實現(xiàn)
class StudentXW extends Student {
//重寫父類Student的testScore()方法
@Override
public double testScore() {
return sum()/3+3;
}
public StudentXW() {}
//StudentXW的構造函數(shù)
public StudentXW(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//Student子類班長類的實現(xiàn)
class StudentBZ extends Student {
//重寫父類Student的testScore()方法
@Override
public double testScore() {
return sum()/3+5;
}
public StudentBZ() {}
//StudentXW的構造函數(shù)
public StudentBZ(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//測試類
public class Test {
public static void main(String[] args) {
//生成若干個student類、StudentXW類、StudentBZ類
Student student1 = new Student("s001","張三",70.5,50,88.5);
Student student2 = new Student("s002","李四",88,65,88.5);
Student student3 = new Student("s003","王五",67,77,90);
StudentXW student4 = new StudentXW("s004","李六",99,88,99.5);
StudentBZ student5 = new StudentBZ("s005","朱漆",56,65.6,43.5);
Student[] students = {student1,student2,student3,student4,student5};
for(int i = 0 ; istudents.length; i++){
double avgScore = students[i].testScore();
System.out.println(students[i].getStudentName()+"學生的評測成績?yōu)椋?+ avgScore+"分");
}
}
}
運行結果為:
張三學生的評測成績?yōu)椋?9.66666666666667分
李四學生的評測成績?yōu)椋?0.5分
王五學生的評測成績?yōu)椋?8.0分
李六學生的評測成績?yōu)椋?8.5分
朱漆學生的評測成績?yōu)椋?0.03333333333333分
我用了半個小時 幫你寫了一個簡單的驗證用戶名和密碼登陸問題 別辜負我的好意 下面是代碼!(建好包和類 代碼粘過去就能用)
實體類 包entity
-------------------------------------------------------------
package entity;
/**
* 用戶實體類
* @author new
*
*/
public class Users {
private String name;//用戶名
private String pass;//用戶密碼
/**
* 空的構造函數(shù) 用戶實力化 此類對象
*/
public Users(){
}
/**
* 構造函數(shù) 接收用戶名和密碼
* @param name
* @param pass
*/
public Users(String name, String pass) {
this.name = name;
this.pass = pass;
}
/**
* 下面set和get方法就不用解釋了吧
* @return
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}
數(shù)據(jù)庫類 包dao(我是模擬一下數(shù)據(jù)庫 沒有用到數(shù)據(jù)庫)
--------------------------------------------------------------
package dao;
import java.util.*;
import entity.Users;//導入實體類
/**
* 模擬數(shù)據(jù)庫 用戶DAO
* @author new
*
*/
public class UsersDAO {
private static Users users=new Users();
static
{
users.setName("tom");
users.setPass("jerry");
}
/**
* 根據(jù)姓名查找這個用戶 (模擬一下數(shù)據(jù)庫)
* @param name
* @return
*/
public Users findUserByName(String name)
{
if(name.equals(this.users.getName()))
{
return this.users;
}
return null;
}
}
業(yè)務類 包service (驗證用戶名和密碼)
------------------------------------------------------------
package service;
import dao.UsersDAO;
import entity.Users;
/**
* 驗證密碼 業(yè)務類
* @author new
*
*/
public class validatePass {
//實力化DAO對象
private UsersDAO us=new UsersDAO();
/**
* 驗證輸入的密碼是否正確
* @param name
* @param pass
* @return
*/
public Users validate(String name,String pass)
{
Users user=null;
user=us.findUserByName(name);
//如果不為空 說明查到了
if(user!=null)
{
//用查詢出來對象的密碼和傳過來的密碼比較
if(user.getPass().equals(pass))
{
return user;
}
}
return null;
}
}
最后是測試test類 包test
----------------------------------------------------------
package test;
import entity.Users;
import service.validatePass;
/**
* 測試類
* @author new
*
*/
public class test {
/**
* main方法 用于測試
* @param args
*/
public static void main(String[] args)
{
//實例化業(yè)務類對象
validatePass v=new validatePass();
//用戶名和密碼
String name="tom";
String pass="jerry";
//開始驗證
Users user=v.validate(name, pass);
if(user==null)
{
System.out.println("你輸入的用戶名或密碼錯誤!");
}else
{
System.out.println("你已經(jīng)通過驗證,成功登陸!");
}
}
}
分享標題:java源碼源代碼大全 java源碼分享平臺
文章鏈接:http://jinyejixie.com/article32/ddogppc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供服務器托管、品牌網(wǎng)站建設、域名注冊、網(wǎng)站維護、、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)