java语言源代码大全,java的源代码
java新手,求完整的源代码
//都是从新手过来的,以下代码供参考
创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:做网站、成都网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的光泽网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
//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("不能构成三角形");
}
}
求JAVA源代码,要有注释,所有财富都在下面了
每天有时间的话 , 会回答一两个图形界面的问题, 但是分数最好还是高点才有兴趣.
具体代码和详细的注释如下
员工类
public class Emp {
private int num;//工号
private String name;//姓名
private double basicPay;//基本工资
private double meritPay;//绩效工资
public Emp(){//无参数构造器
}
public Emp(int num, String name, double basicPay, double meritPay) {//有参数构造器
super();
this.num = num;
this.name = name;
this.basicPay = basicPay;
this.meritPay = meritPay;
}
//重写Object的toString 方法
public String toString() {
return "工号:"+num+"\t姓名:"+name+"\t基本工资:"+basicPay+"\t绩效工资"+meritPay;
}
//下面是属性的set和get
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBasicPay() {
return basicPay;
}
public void setBasicPay(double basicPay) {
this.basicPay = basicPay;
}
public double getMeritPay() {
return meritPay;
}
public void setMeritPay(double meritPay) {
this.meritPay = meritPay;
}
}
输入界面类
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class EmpFrome extends JFrame implements ActionListener {
JTextField jtfnum, jtfname, jtfbp, jtfmp;
JButton jbwtf;
public EmpFrome() {
JLabel jl1 = new JLabel("工号");
jtfnum = new JTextField(8);
add(jl1);
add(jtfnum);
JLabel jl2 = new JLabel("姓名");
jtfname = new JTextField(8);
add(jl2);
add(jtfname);
JLabel jl3 = new JLabel("基本工资");
jtfbp = new JTextField(8);
add(jl3);
add(jtfbp);
JLabel jl4 = new JLabel("绩效工资");
jtfmp = new JTextField(8);
add(jl4);
add(jtfmp);
JLabel jl5 = new JLabel();
jbwtf = new JButton("写入文件");
jbwtf.addActionListener(this);
add(jl5);
add(jbwtf);
setLayout(new GridLayout(5, 2));
setTitle("员工信息录入");
setSize(290, 230);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("写入文件")) {
try{
//获取数据
int num = Integer.parseInt(jtfnum.getText().trim());
String name = jtfname.getText().trim();
double bp = Double.parseDouble(jtfbp.getText().trim());
double mp = Double.parseDouble(jtfmp.getText().trim());
Emp emp = new Emp(num, name, bp, mp);
writeToFile(emp);
JOptionPane.showMessageDialog(this, "录入成功");//提示成功
//清空文本框
jtfnum.setText("");
jtfname.setText("");
jtfbp.setText("");
jtfmp.setText("");
}catch(Exception ex){
//当输入不符合规范时 , 提示错误
JOptionPane.showMessageDialog(this, "请输入正确的数据:\n工号整型,工资浮点型","录入错误",JOptionPane.ERROR_MESSAGE);
}
}
}
//定义的文件路径
final static String FILE_PATH = "employee.dat";
public void writeToFile(Emp emp) {//IO操作,追加写入
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(new File(FILE_PATH), true));//为true表示追加
bw.write(emp.toString());//写入员工信息
bw.newLine();//换行
} catch (IOException e) {
e.printStackTrace();
}finally{
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
测试类
public class EmpTest {
public static void main(String[] args) {
new EmpFrome();
}
}
测试效果
速求用JAVA语言写聊天室的源代码
【ClientSocketDemo.java 客户端Java源代码】
import java.net.*;
import java.io.*;
public class ClientSocketDemo
{
//声明客户端Socket对象socket
Socket socket = null;
//声明客户器端数据输入输出流
DataInputStream in;
DataOutputStream out;
//声明字符串数组对象response,用于存储从服务器接收到的信息
String response[];
//执行过程中,没有参数时的构造方法,本地服务器在本地,取默认端口10745
public ClientSocketDemo()
{
try
{
//创建客户端socket,服务器地址取本地,端口号为10745
socket = new Socket("localhost",10745);
//创建客户端数据输入输出流,用于对服务器端发送或接收数据
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
//获取客户端地址及端口号
String ip = String.valueOf(socket.getLocalAddress());
String port = String.valueOf(socket.getLocalPort());
//向服务器发送数据
out.writeUTF("Hello Server.This connection is from client.");
out.writeUTF(ip);
out.writeUTF(port);
//从服务器接收数据
response = new String[3];
for (int i = 0; i response.length; i++)
{
response[i] = in.readUTF();
System.out.println(response[i]);
}
}
catch(UnknownHostException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}
//执行过程中,有一个参数时的构造方法,参数指定服务器地址,取默认端口10745
public ClientSocketDemo(String hostname)
{
try
{
//创建客户端socket,hostname参数指定服务器地址,端口号为10745
socket = new Socket(hostname,10745);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
String ip = String.valueOf(socket.getLocalAddress());
String port = String.valueOf(socket.getLocalPort());
out.writeUTF("Hello Server.This connection is from client.");
out.writeUTF(ip);
out.writeUTF(port);
response = new String[3];
for (int i = 0; i response.length; i++)
{
response[i] = in.readUTF();
System.out.println(response[i]);
}
}
catch(UnknownHostException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}
//执行过程中,有两个个参数时的构造方法,第一个参数hostname指定服务器地址
//第一个参数serverPort指定服务器端口号
public ClientSocketDemo(String hostname,String serverPort)
{
try
{
socket = new Socket(hostname,Integer.parseInt(serverPort));
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
String ip = String.valueOf(socket.getLocalAddress());
String port = String.valueOf(socket.getLocalPort());
out.writeUTF("Hello Server.This connection is from client.");
out.writeUTF(ip);
out.writeUTF(port);
response = new String[3];
for (int i = 0; i response.length; i++)
{
response[i] = in.readUTF();
System.out.println(response[i]);
}
}
catch(UnknownHostException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}
public static void main(String[] args)
{
String comd[] = args;
if(comd.length == 0)
{
System.out.println("Use localhost(127.0.0.1) and default port");
ClientSocketDemo demo = new ClientSocketDemo();
}
else if(comd.length == 1)
{
System.out.println("Use default port");
ClientSocketDemo demo = new ClientSocketDemo(args[0]);
}
else if(comd.length == 2)
{
System.out.println("Hostname and port are named by user");
ClientSocketDemo demo = new ClientSocketDemo(args[0],args[1]);
}
else System.out.println("ERROR");
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
【ServerSocketDemo.java 服务器端Java源代码】
import java.net.*;
import java.io.*;
public class ServerSocketDemo
{
//声明ServerSocket类对象
ServerSocket serverSocket;
//声明并初始化服务器端监听端口号常量
public static final int PORT = 10745;
//声明服务器端数据输入输出流
DataInputStream in;
DataOutputStream out;
//声明InetAddress类对象ip,用于获取服务器地址及端口号等信息
InetAddress ip = null;
//声明字符串数组对象request,用于存储从客户端发送来的信息
String request[];
public ServerSocketDemo()
{
request = new String[3]; //初始化字符串数组
try
{
//获取本地服务器地址信息
ip = InetAddress.getLocalHost();
//以PORT为服务端口号,创建serverSocket对象以监听该端口上的连接
serverSocket = new ServerSocket(PORT);
//创建Socket类的对象socket,用于保存连接到服务器的客户端socket对象
Socket socket = serverSocket.accept();
System.out.println("This is server:"+String.valueOf(ip)+PORT);
//创建服务器端数据输入输出流,用于对客户端接收或发送数据
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
//接收客户端发送来的数据信息,并显示
request[0] = in.readUTF();
request[1] = in.readUTF();
request[2] = in.readUTF();
System.out.println("Received messages form client is:");
System.out.println(request[0]);
System.out.println(request[1]);
System.out.println(request[2]);
//向客户端发送数据
out.writeUTF("Hello client!");
out.writeUTF("Your ip is:"+request[1]);
out.writeUTF("Your port is:"+request[2]);
}
catch(IOException e){e.printStackTrace();}
}
public static void main(String[] args)
{
ServerSocketDemo demo = new ServerSocketDemo();
}
}
java 源代码注释
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class GameTest extends JFrame implements ActionListener{
/*
* 新建一个主面板(这个类可能是自定义的,本程序和API中没有)。
*/
MainPanel j=new MainPanel();
JButton jPreview;
JLabel label;
Container container;
JPanel panel;
/**
* 主函数
* @param args
*/
public static void main(String[] args) {
//运行程序
new GameTest();
}
/**
* 构造函数。
*
*/
public GameTest()
{
//新建一个标题为“拼图”的窗口
JFrame fr =new JFrame("拼图");
//获取窗口容器。
container=fr.getContentPane();
//创建菜单条
JMenuBar jMenuBar=new JMenuBar();
//以下初始化菜单,并且设置快捷键和添加监听器。
JMenu jMenuGame=new JMenu("游戏(G)");
jMenuGame.setMnemonic('g');
JMenuItem jMenuItemStart = new JMenuItem("开始(S)");
jMenuItemStart.setMnemonic('s');
jMenuItemStart.addActionListener(this);
JMenuItem jMenuItemExit=new JMenuItem("退出(E)");
jMenuItemExit.setMnemonic('e');
jMenuItemExit.addActionListener(this);
jMenuGame.add(jMenuItemStart);
jMenuGame.add(jMenuItemExit);
//初始化按钮并设置快捷键和添加监听器
JButton jChoice=new JButton("选图(X)");
jChoice.setMnemonic('x');
jChoice.addActionListener(this);
jPreview=new JButton("预览(P)");
jPreview.setMnemonic('p');
jPreview.addActionListener(this);
//将菜单和按钮添加到菜单条中
jMenuBar.add(jMenuGame);
jMenuBar.add(jChoice);
jMenuBar.add(jPreview);
//将菜单条设为该窗口的主菜单
fr.setJMenuBar(jMenuBar);
//将主面板添加到该窗口的容器中。
container.add(j);
//设置大小
fr.setSize(315,360 );
fr.setVisible(true);
//设置默认关闭方式。
fr.setDefaultCloseOperation(3);
}
/**
* 事件处理函数。
*/
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="开始(S)")
{
j.Start();
}
if(e.getActionCommand()=="预览(P)")
{
j.setVisible(false);
panel=new JPanel();
Icon icon=new ImageIcon("pictrue/pic"+"_"+MainPanel.pictureID+".jpg");
label=new JLabel(icon);
label.setBounds(300, 300, 0, 0);
panel.add(label);
panel.setSize(300, 300);
panel.setVisible(true);
this.container.add(panel);
jPreview.setText("返回(P)");
}
if(e.getActionCommand()=="返回(P)")
{
panel.setVisible(false);
j.setVisible(true);
j.repaint();
jPreview.setText("预览(P)");
}
if(e.getActionCommand()=="退出(E)")
{
System.exit(0);
}
if(e.getActionCommand()=="选图(X)")
{
//初始化选择框,并提供选择。
Choice pic = new Choice();
pic.add("七里香");
pic.add("依然范特西");
pic.add("八度空间");
pic.add("十一月的肖邦");
pic.add("魔杰座");
pic.add("叶惠美");
pic.add("我很忙");
int i=JOptionPane.showConfirmDialog(this, pic, "选择图片", JOptionPane.OK_CANCEL_OPTION);
if(i==JOptionPane.YES_OPTION)
{
//选择图片
MainPanel.pictureID=pic.getSelectedIndex()+1;
j.removeAll();
j.reLoadPicture();
j.repaint();
}
}
}
}
求JAVA源代码
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class GradeStatistic {
public static void main(String[] args) {
GradeStatistic gs = new GradeStatistic();
ListMark list = new ArrayListMark();
float sum = 0;
while(true){
Scanner sc = new Scanner(System.in);
System.out.print("Please input student name: ");
String name = sc.nextLine();
if(name.equals("end")){
break;
}
System.out.print("Please input student score: ");
float score = sc.nextFloat();
sum += score;
list.add(gs.new Mark(name, score));
}
float max = list.get(0).getScore();
float min = list.get(0).getScore();
for(Mark mark: list){
if(max mark.getScore()){
max = mark.getScore();
}
if(min mark.getScore()){
min = mark.getScore();
}
}
float average = sum / list.size();
System.out.println("Average is: " + average);
System.out.println("Max is: " + max);
System.out.println("Min is: " + min);
}
private class Mark{
private String name;
private float score;
public Mark(String name, float score){
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public float getScore() {
return score;
}
}
}
----------------------
Please input student name: Zhang san
Please input student score: 100
Please input student name: Li Si
Please input student score: 91
Please input student name: Ec
Please input student score: 35
Please input student name: ma qi
Please input student score: 67
Please input student name: end
Average is: 73.25
Max is: 100.0
Min is: 35.0
标题名称:java语言源代码大全,java的源代码
当前URL:http://myzitong.com/article/dsephid.html