
正文
java上机实践题代码 java全部上机实践答案
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java程序设计精讲上机实践答案(最好是第3章和4,5章的答案)
第三章
3.import java.util.Scanner;
public class TestScanner3{
public static void main(String[] args){
Scanner s=new Scanner(System.in);
System.out.print("请输入三角形的三条边:");
float a=s.nextFloat();
float b=s.nextFloat();
float c=s.nextFloat();
if((a!=0)(b!=0)(c!=0)(a+bcMath.abs(a-b)c))
System.out.println("\t三条边构成三角形 ");
else
System.out.println("\t三条边构不成三角形 ");
}
}
4.import java.util.Scanner;
public class TestSwitch4{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
System.out.print("请输入两个整数:");
int a=s.nextInt();
int b=s.nextInt();
char c;
switch(1){
case 1: System.out.println(" "+"="+a+"+"+b);
case 2: System.out.println(" "+"="+a+"-"+b);
case 3: System.out.println(" "+"="+a+"*"+b);
case 4: System.out.println(" "+"="+a+"/"+b); break;
default: System.out.println("输入错误: ");
}
}
}
5.public class Sum5{
public static void main(String args[]){
int s=0,s1=0,s2=0;
for(int i=1;i=3;i++)
{
if(i/2==0)
s1+=(-1)*i*i;
else
s2+=i*i;
}
s=s1+s2;
System.out.println("s的值是:"+s);
}
}
6.public class Sum6{
public static void main(String args[]){
long t=0;
for(int i=1;i=20;i++)
{ long s=1;
for(int j=1;j=i;j++)
{
s=s*j;
}
System.out.println(i+"!的值是: \n"+s);
t=t+s;
}
System.out.println("t的值是: "+t);
}
}
第四章
1.public class Box{
public int length;
public int width;
public int height;
public Box(int length1,int width1,int height1)
{ length=length1;width=width1;height=height1;}
public int Volume()
{ int v;
v=length*width*height;
return v;
}
public int Som()
{ int s;
s=2*length*width+2*width*height+2*length*height;
return s;
}
public static void main(String[] args)
{ Box b;
b=new Box(20,10,5);
System.out.println("长方体的长: "+b.length);
System.out.println("长方体的宽: "+b.width);
System.out.println("长方体的高: "+b.height);
System.out.println("长方体的体积: "+ b.Volume());
System.out.println("长方体的表面积: "+ b.Som());
}
}
2.public class A {
private int data;
private String str;
public A(){
data = 0;
str = "";
}
public A(int data,String str){
this.data = data;
this.str = str;
}
public void add(int k,String s){
this.data+=k;
this.str+=s;
}
public void clear(){
this.data = 0;
this.str = "";
}
public String toString(){
String s = data+"";
s+=str;
return (s);
}
public static void main(String args[]){
A a = new A();
a.add(10,"Hello");
System.out.println("data is:"+a.data+" str is:"+a.str);
System.out.println(a.toString());
a.clear();
A b = new A(199,"nihao");
System.out.println("data is:"+b.data+" str is:"+b.str);
System.out.println(b.toString());
b.add(200, " dajiahao");System.out.println("data is:"+b.data+" str is:"+b.str);
System.out.println(b.toString());
}
}
没有第五章
相关问答
Q1: 1、编写一个Application程序【java上机作业,要完整代码,急求!!!!!!!!!!】
第一题:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class RadioTest extends JFrame{
private JRadioButton jrb1;
private JRadioButton jrb2;
private JLabel jlbl;
private JPanel jp;
private JButton jbtn;
private String jlstr;
private ButtonGroup bg;
public RadioTest(){
jlstr = "你选择的是:";
this.setTitle("实现单选按钮的效果");
jrb1 = new JRadioButton("男");
jrb2 = new JRadioButton("女");
bg = new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
jlbl = new JLabel(jlstr);
jbtn = new JButton("退出");
jbtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(1);
}
});
jrb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jrb1){
jlbl.setText(jlstr+jrb1.getText());
}
}
});
jrb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jrb2){
jlbl.setText(jlstr+jrb2.getText());
}
}
});
jp = new JPanel();
jp.add(jrb1);
jp.add(jrb2);
jp.add(jlbl);
jp.add(jbtn);
this.add(jp);
this.setBounds(300, 300, 230, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
RadioTest rt = new RadioTest();
}
}
Q2: java面试题求代码,最好有注解。。。
你好,代码如下。需要修改的话,你可以根据情况修改:
class Info{ // 定义信息类
private String name = "生产者"; // 定义name属性
private String content = "压入子弹" ; // 定义content属性
private boolean flag = false ; // 设置标志位
public synchronized void set(String name,String content){
if(!flag){
try{
super.wait() ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
}
this.setName(name) ; // 设置名称
try{
Thread.sleep(300) ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
this.setContent(content) ; // 设置内容
flag = false ; // 改变标志位,表示可以取走
super.notify() ;
}
public synchronized void get(){
if(flag){
try{
super.wait() ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
}
try{
Thread.sleep(300) ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
System.out.println(this.getName() +
" -- " + this.getContent()) ;
flag = true ; // 改变标志位,表示可以生产
super.notify() ;
}
public void setName(String name){
this.name = name ;
}
public void setContent(String content){
this.content = content ;
}
public String getName(){
return this.name ;
}
public String getContent(){
return this.content ;
}
};
class Producer implements Runnable{ // 通过Runnable实现多线程
private Info info = null ; // 保存Info引用
public Producer(Info info){
this.info = info ;
}
public void run(){
boolean flag = false ; // 定义标记位
for(int i=0;i12;i++){
if(flag){
this.info.set("生产者","压入子弹") ; // 设置名称
flag = false ;
}else{
this.info.set("消费者","射出子弹") ; // 设置名称
flag = true ;
}
}
}
};
class Consumer implements Runnable{
private Info info = null ;
public Consumer(Info info){
this.info = info ;
}
public void run(){
for(int i=0;i24;i++){
this.info.get() ;
}
}
};
public class ThreadCaseDemo03{
public static void main(String args[]){
Info info = new Info(); // 实例化Info对象
Producer pro = new Producer(info) ; // 生产者
Consumer con = new Consumer(info) ; // 消费者
new Thread(pro).start() ;
new Thread(con).start() ;
}
};
Q3: Java 上机题目
public class BookTest{ public static void main(String[] args){ Book book = new Book("Cook book",150); book.detail(); }} class Book{ private String title; private int pageNum; public Book(String title,int pageNum){ this.title = title; this.judgePage(pageNum); } public String getTitle(){ return this.title; } public void setTitle(String title){ this.title=title; }public int getPageNum(){ return this.pageNum; }public void setPageNum(int pageNum){ this.pageNum=this.judgePage(pageNum);}private int judgePage(int pageNum){ if(pageNum200){ System.out.println("页数["+pageNum+"]不能小于200"); pageNum = 200;
} return pageNum;}public void detail(){ System.out.println("书名:"+this.title+"\n页数:"+this.pageNum);}}
Q4: java课程设计题目及代码是什么?
java课程设计题目及代码分别是:
1、题目:计算器。设计内容是设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算。
设计要求是设计的计算器应用程序可以完成家法、减法、乘法、除法和取余运算。且有小数点、正负号、求倒数、退格和清零功能。
2、代码:
数字按钮NumberButton类如下:
import java.awt.
import java.awt.event.
import javax.swing.
public class NumberButton extends Button.
{
int number.
public NumberButton(int number).
{
super(""+number).
this.number=number.
setForeground(Color.blue).
}
public int getNumber().
{
return number;
}
}
其它java课程设计题目及代码是:
题目:华容道。编写一个按钮的子类,使用该子类创建的对象代表华容道中的人物。通过焦点事件控制人物颜色,当人物获得焦点时颜色为蓝色,当失去焦点时颜色为灰色。
通过键盘事件和鼠标事件来实现曹操、关羽等人物的移动。当人物上发生鼠标事件或键盘事件时,如果鼠标指针的位置是在人物的下方(也就是组件的下半部分)或按下键盘的“↓“键,该人物向下移动。向左、向右和向上的移动原理类似。
代码是:
String name[]={"曹操","关羽","张","刘","马","许","兵","兵","兵","兵"}.
for(int i=0;iname.length;i++).
{
person[i]=new Person(i,name[i]).
person[i].addKeyListener(this).
person[i].addMouseListener(this).
// person[i].addFocusListener(new Person).
add(person[i]).
}
person[0].setBounds(104,54,100,100).
person[1].setBounds(104,154,100,50).
person[2].setBounds(54,154,50,100).
person[3].setBounds(204,154,50,100).
person[4].setBounds(54,54,50,100).
person[5].setBounds(204,54,50,100);
person[6].setBounds(54,254,50,50);
person[7].setBounds(204,254,50,50);
person[8].setBounds(104,204,50,50);
person[9].setBounds(154,204,50,50);
java上机实践题代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java全部上机实践答案、java上机实践题代码的信息别忘了在本站进行查找喔。







