1. java 设计一个Stock的类,这个类包括:
代码如下:
import java.math.BigDecimal;import java.math.RoundingMode;public class Stock { private String symbol; private String name; private double previousClosingPrice; private double currentPrice; // 构造方法 public Stock(String symbol, String name) { this.symbol = symbol; this.name = name; } // public double getChangePercent() { return (currentPrice - previousClosingPrice) / previousClosingPrice; } public void setPreviousClosingPrice(double previousClosingPrice) { this.previousClosingPrice = previousClosingPrice; } public void setCurrentPrice(double currentPrice) { this.currentPrice = currentPrice; } public String getSymbol() { return symbol; } public String getName() { return name; } public static void main(String[] args) { Stock stock = new Stock("Java", "Sun Mircro"); stock.setCurrentPrice(4.35); // 当前价格 stock.setPreviousClosingPrice(4.5);// 前一交易日价格 double d = stock.getChangePercent(); // 价格浮动: 由于是double,下面的计算是N位小数 System.out.println("价格浮动:" + stock.getChangePercent()); // 处理下 BigDecimal bd = new BigDecimal(d * 100); // 乘个100,是为了看效果 bd = bd.setScale(2, RoundingMode.HALF_UP); // 设置2位小数,采用四舍五入 System.out.println("[" + stock.getSymbol() + "] " + stock.getName() + " 价格浮动:" + bd.toString() + "%"); }}亲,如果回答满意,亲及时采纳,你的合作是我们回答的动力,谢谢!
2. 设计一个java程序?
public static void main(String... args) throws Exception { Collection collection = new ArrayList(); Scanner scanner = new Scanner(System.in); long maxValue, iv,counter=0L; maxValue = iv = -1L; while (scanner.hasNext()) { String s = scanner.next(); try { iv = Long.parseLong(s); } catch (NumberFormatException e) { break; } if(iv==maxValue) counter++; if (iv > maxValue) { counter=1; maxValue = iv; } if (iv != 0) { collection.add(iv); continue; } break; } scanner.close(); System.out.println("最大值:"+maxValue+",出现次数:"+counter); }
3. 用java设计一个程序
一分钟读懂Java语言程序设计
4. 帮忙设计一个java程序
class SanJiao { private double a; private double b; private double c; public boolean isLegal; public double zhouChang() { return a+b+c; } public double mainJi() { double p = zhouChang()/2; return Math.sqrt(p*(p-a)*(p-b)*(p-c)); } public SanJiao(double a, double b, double c) { this.a = a; this.b = b; this.c = c; isLegal = a+b>c && b+c>a && a+c>b; }}public class Main { public static void main(String[] args) { SanJiao sj = new SanJiao(3,4,5); if (sj.isLegal) { System.out.println("三角形周长:"+sj.zhouChang()+" 面积:"+sj.mainJi()); } else { System.out.println("不能构成合法三角形"); } }}
5. 设计一个Java程序,能够实现加减乘除运算,谢谢!
import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Calculator extends JFrame implements ActionListener{private static final long serialVersionUID = 8199443193151152362L;private JButton bto_s=new JButton("sqrt"),bto_zf=new JButton("+/-"),bto_ce=new JButton("CE"),bto_c=new JButton("C"),bto_7=new JButton("7"),bto_8=new JButton("8"),bto_9=new JButton("9"),bto_chu=new JButton("/"),bto_4=new JButton("4"),bto_5=new JButton("5"),bto_6=new JButton("6"),bto_cheng=new JButton("*"),bto_1=new JButton("1"),bto_2=new JButton("2"),bto_3=new JButton("3"),bto_jian=new JButton("-"),bto_0=new JButton("0"),bto_dian=new JButton("."),bto_deng=new JButton("="),bto_jia=new JButton("+");JButton button[]={bto_s,bto_zf,bto_ce,bto_c,bto_7,bto_8,bto_9,bto_chu,bto_4,bto_5,bto_6,bto_cheng,bto_1,bto_2,bto_3,bto_jian, bto_0,bto_dian,bto_deng,bto_jia};private JTextField text_double;// = new JTextField("0");private String operator = "="; //当前运算的运算符private boolean firstDigit = true; // 标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字private double resultNum = 0.0; // 计算的中间结果private boolean operateValidFlag = true; //判断操作是否合法public Calculator(){ super("Calculator"); this.setBounds(300, 300, 300, 300); this.setResizable(false); this.setBackground(Color.orange); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.getContentPane().setLayout(new BorderLayout());//设置布局 text_double=new JTextField("0",20);//设置文本区 text_double.setHorizontalAlignment(JTextField.RIGHT);//设置水平对齐方式未右对齐 this.getContentPane().add(text_double,BorderLayout.NORTH);//将文本区添加到Content北部 JPanel panel=new JPanel(new GridLayout(5,4));//在内容窗口添加一个网格布局 this.getContentPane().add(panel);//添加panel面板 for(int i=0;i= 0) // 用户按了数字键或者小数点键 { handleNumber(c); // handlezero(zero); } else //用户按了运算符键 { handleOperator(c); }}private void handleC() // 初始化计算器的各种值{ text_double.setText("0"); firstDigit = true; operator = "="; }private void handleNumber(String button) { if (firstDigit)//输入的第一个数字 { text_double.setText(button); } else if ((button.equals(".")) && (text_double.getText().indexOf(".") < 0))//输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面 //如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回 -1 { text_double.setText(text_double.getText() + "."); } else if (!button.equals("."))// 如果输入的不是小数点,则将数字附在结果文本框的后面 { text_double.setText(text_double.getText() + button); }// 以后输入的肯定不是第一个数字了 firstDigit = false; }private void handleOperator(String button) { if (operator.equals("/")) {// 除法运算// 如果当前结果文本框中的值等于0 if (getNumberFromText() == 0.0){// 操作不合法 operateValidFlag = false; text_double.setText("除数不能为零"); } else { resultNum /= getNumberFromText(); } } else if (operator.equals("+")){// 加法运算 resultNum += getNumberFromText(); } else if (operator.equals("-")){// 减法运算 resultNum -= getNumberFromText(); } else if (operator.equals("*")){// 乘法运算 resultNum *= getNumberFromText(); } else if (operator.equals("sqrt")) {// 平方根运算 if(getNumberFromText()<0){ operateValidFlag = false; text_double.setText("被开方数不能为负数");} else resultNum = Math.sqrt(resultNum); } else if (operator.equals("+/-")){// 正数负数运算 resultNum = resultNum * (-1); } else if (operator.equals("=")){// 赋值运算 resultNum = getNumberFromText(); } if (operateValidFlag) {// 双精度浮点数的运算 long t1; double t2; t1 = (long) resultNum; t2 = resultNum - t1; if (t2 == 0) { text_double.setText(String.valueOf(t1)); } else { text_double.setText(String.valueOf(resultNum)); } } operator = button; //运算符等于用户按的按钮 firstDigit = true; operateValidFlag = true;}private double getNumberFromText() //从结果的文本框获取数字{ double result = 0; try { result = Double.valueOf(text_double.getText()).doubleValue(); // ValueOf()返回表示指定的 double 值的 Double 实例 } catch (NumberFormatException e){ } return result; }public static void main(final String[] args) {new Calculator();}}
6. 用java设计一个发牌程序
// 发牌程序。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardBuffer //加互斥锁的缓冲区
{
private int value;
private boolean isEmpty = true; //value是否为空的信号量
private int order=0; //信号量,约定取牌线程的次序
synchronized void put(int i)
{
while (!isEmpty) //当value不空时,等待
{
try
{
this.wait(); //等待
}
catch(InterruptedException e) {}
}
value = i; //当value空时,value获得值
isEmpty = false; //设置value为不空状态
notifyAll(); //唤醒所有其他等待线程
}
synchronized int get(int order) //order是取牌线程约定的次序
{
while (isEmpty || (this.order!=order)) //当value空或取牌次序不符时等待
{
try
{
this.wait();
}
catch(InterruptedException e) {}
}
isEmpty = true; //设置value为空状态,并返回值
notifyAll();
this.order = (this.order+1)%4; //加1使取牌次序轮转
return value;
}
}
class Sender extends Thread //发牌线程类
{
private CardBuffer cardbuffer;
private int count;
public Sender(CardBuffer cardbuffer,int count)
{
this.cardbuffer = cardbuffer;
this.count = count;
}
public void run()
{
for (int i=1;i<=this.count;i++)
cardbuffer.put(i);
}
}
class Receiver extends Thread //取牌线程类
{
private CardBuffer cardbuffer;
private JTextArea text;
private int order; //信号量,约定取牌线程的次序
public Receiver(CardBuffer cardbuffer,JTextArea text,int order)
{
this.cardbuffer = cardbuffer ;
this.text = text ;
this.order = order;
}
public void run()
{
while(true)
{
text.append(" "+cardbuffer.get(this.order));
try
{
this.sleep(100);
}
catch(InterruptedException e) {}
}
}
}
class CardJFrame extends JFrame
{
public CardJFrame()
{
super("发牌程序");
this.setSize(430,200);
this.setLocation(300,240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new GridLayout(3,3,5,5)); //3行3列网格布局,间隔为5
JTextArea text_north,text_east,text_south,text_west; //获得牌的4个文本区
text_north = new JTextArea();
text_east = new JTextArea();
text_south = new JTextArea();
text_west = new JTextArea();
text_north.setLineWrap(true); //设置文本区自动换行
text_east.setLineWrap(true);
text_south.setLineWrap(true);
text_west.setLineWrap(true);
text_north.setEditable(false);
text_east.setEditable(false);
text_south.setEditable(false);
text_west.setEditable(false);
Font font = new Font("Helvetica", Font.PLAIN, 16);
text_north.setFont(font);
text_east.setFont(font);
text_south.setFont(font);
text_west.setFont(font);
this.add(new JPanel()); //网格布局的第1行
this.add(text_north);
this.add(new JPanel());
this.add(text_west); //网格布局的第2行
this.add(new JPanel());
this.add(text_east);
this.add(new JPanel()); //网格布局的第3行
this.add(text_south);
this.add(new JPanel());
this.setVisible(true);
CardBuffer cardbuffer = new CardBuffer();
Sender s = new Sender(cardbuffer,52);
s.setPriority(10); //设置最高优先级
s.start(); //启动发牌线程
(new Receiver(cardbuffer,text_north,0)).start(); //创建并启动4个取牌线程,优先级为5
(new Receiver(cardbuffer,text_east,1)).start();
(new Receiver(cardbuffer,text_south,2)).start();
(new Receiver(cardbuffer,text_west,3)).start();
}
public static void main(String arg[])
{
new CardJFrame();
}
}
7. 设计一个别java程序模仿记事本的部分功能
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MyNotepad implements ActionListener{
//声明所需组件
Frame f = new Frame("记事本 作者:刘欣 班级:java5");
MenuBar menubar;
Menu menufile;
MenuItem menuitemopen,menuitemsave,menuitemsaveas,menuitemclose;
TextArea textarea;
String filename = "";
FileDialog d1,d2;
public MyNotepad(){
}
void run(){
//设置大小并注册监听器
f.setSize(400,400);
f.addWindowListener(new WindowHander());
//实例化组件并添加至容器中,同时添加适配器
menubar = new MenuBar();
f.setMenuBar(menubar);
menufile = new Menu("文件");
menubar.add(menufile);
menuitemopen = new MenuItem("打开");
menufile.add(menuitemopen);
menuitemopen.addActionListener(this);
menuitemsave = new MenuItem("保存");
menufile.add(menuitemsave);
menuitemsave.addActionListener(this);
menuitemsaveas = new MenuItem("另存为");
menufile.add(menuitemsaveas);
menuitemsaveas.addActionListener(this);
menuitemclose = new MenuItem("关闭");
menufile.add(menuitemclose);
menuitemclose.addActionListener(this);
textarea = new TextArea();
f.add(textarea);
f.setVisible(true);
d1 = new FileDialog(f,"打开",FileDialog.LOAD);
d2 = new FileDialog(f,"另存为",FileDialog.SAVE);
}
public static void main(String[] args){
MyNotepad m = new MyNotepad();
m.run();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==menuitemclose){
System.exit(1);
}else
if(e.getSource()==menuitemopen){
textarea.setText("");
d1.setVisible(true);
filename = d1.getDirectory()+d1.getFile();
if(filename.endsWith(".txt"))
{
read(filename);
}
else{
textarea.append("不支持此类型文件");
}
}else
if(e.getSource()==menuitemsaveas){
d2.setVisible(true);
filename = d2.getDirectory()+d2.getFile();
write(filename);
}else if(e.getSource()==menuitemsave){
if(filename==""){
d2.setVisible(true);
filename = d2.getDirectory()+d2.getFile();
write(filename);
}else{
write(filename);
}
}
}
void write(String fileneme){
try{
File file1 = new File(filename);
FileWriter fw = new FileWriter(file1);
fw.write(textarea.getText());
fw.close();
}catch(IOException e){
System.out.println(e);
}
}
void read(String filename){
try{
File file2 = new File(filename);
FileReader fr = new FileReader(file2);
int length = (int)file2.length();
char[] cbuf = new char[length];
int r = 0;
while(fr.ready()){
r += fr.read(cbuf,r,length-r);
}
fr.close();
textarea.setText(new String(cbuf,0,r));
}catch(IOException e){
System.out.println(e);
}
}
}
class WindowHander extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
实现了打开保存新建另存为功能
8. java小程序设计,设计一个窗口程序
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameTest {
private JFrame jf;
private JButton yellow,blue,green;
private JPanel panel1;
public FrameTest(){
jf = new JFrame("Frame");
yellow = new JButton("yellow");
blue = new JButton("blue");
green = new JButton("green");
panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
jf.add(panel1,BorderLayout.CENTER);
jf.setVisible(true);
jf.setSize(500,600);
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
blue.setForeground(Color.blue);
panel1.add(blue);
blue.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel1.setBackground(Color.blue);
}
});
yellow.setForeground(Color.yellow);
panel1.add(yellow);
yellow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel1.setBackground(Color.yellow);
}
});
green.setForeground(Color.green);
panel1.add(green);
green.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel1.setBackground(Color.green);
}
});
}
public static void main(String args[]){
FrameTest ft = new FrameTest();
}
}