《Java公选课作业》
注意:
作业与作业之间用5行分开,好让我打分。请注意字体,涉及到平时成绩,望各位认真。
第一次作业:
1.Java软件的三个版本是什么?
答:针对企业级应用的J2EE(Java2 Enterprise Edition)、针对普通PC应用的J2SE(Java2 Standard Edition)和针对嵌入式设备及消费类电器J2ME(Java2 Micro Edition)三个版本
2. 什么是Applet
答:Java Applet是一种特殊的Java应用程序。Applet是能够嵌入到一个HTML页面中,并且通过Web浏览器下载和执行的一种Java类。它是Java技术容器的一种特定类型,其执行方式不同于Application。
作业与作业之间用5行分开
第二次作业:
1.举例说明算术运算的程序
答:public class Exp2_3 {
public static void main(String args[])
{
int a=10,b=3;
double f=21.5,g=7.2;
System.out.println("a/b="+a/b);
System.out.println("a%b="+a%b);
System.out.println("21.5%7.2="+f%g);
System.out.println("10.0/3="+10.0/3);
}
}
2. 举例说明逻辑运算的程序
答:public class Exp2_5 {
public static void main(String args[])
{
int a,b;
a=7;
b=34;
System.out.println("a=="+a+"b=="+(b));
System.out.println("ab"+(a>b));
System.out.println("a<=b"+(a<=b));
System.out.println("a>=b"+(a>=b));
System.out.println("a==b"+(a==b));
System.out.println("a!=b"+(a!=b));
System.out.println("注意:");
System.out.println("a=b"+(a=b));
}
}
作业与作业之间用5行分开
第三次作业:
1. 输入两个实数,按数值由大到小顺序输出这两个数
答:import java.io.*;
public class Exp3_1{
public static void main(String args[]) throws IOException{
BufferedReader inObj=new BufferedReader(new InputStreamReader(System.in));
float a,b,t;
System.out.println("请输入a的值:");
a=Float.parseFloat(inObj.readLine());
System.out.println("请输入b的值:");
b=Float.parseFloat(inObj.readLine());
if(a>b)
{t=a;
a=b;
b=t;
}
System.out.println("a="+a);
System.out.println("b="+b);
}
}
作业与作业之间用5行分开
第四次作业:
1. 举例说明形参和实参
答:
public class Exp4_2{
public int min (int a,int b){
int c;
c=(a>b)?a:b;
return c;
}
public static void main(String[] args) {
int min=0,x=10,y=20;
Exp4_2 t=new Exp4_2();
min=t.min(x,y);
System.out.println("min is "+min);
}
2. 举例说明类对象的产生和使用方式。
答:class Person
{
int age;
void shout()
{
System.out.println("oh,my god! my age is " + age);
}
}
public class Exp4_6
{
public static void main(String args[])
{
Person p1 = new Person();
Person p2 =new Person();
p1.age = -30;
p1.shout();
p2.shout();
}
}
作业与作业之间用5行分开
第五次作业:
1. 举例说明继承
class Person
{
public String name;
public int age;
public Person(String name,int age)
{
this.name=name;
this.age=age;
}
public Person() //如果你不写这个构造函数,看看对类Student有什么影响。
{
}
public void getInfo()
{
System.out.println(name);
System.out.println(age);
}
}
public class Exp5_1 extends Person
{
public void study()
{
System.out.println("Studding");
}
public static void main(String args[])
{
Person p=new Person();
p.name="person";
p.age=30;
p.getInfo();
Student s=new Student();
s.name="student";
s.age=16;
s.getInfo();
s.study();
}
}
作业与作业之间用5行分开
第六次作业:
1.某厂家Dmeng生产VideoCard接口规定的显卡,后主板上插入CPU和显卡,编程模拟其工作过程。
interface VideoCard {
void display();
String getName();
}
class Dmeng implements VideoCard {
String name;
Dmeng() { name = "Dmeng's VideoCard";}
public void display() {
System.out.println(name + "is working");
}
public String getName() { return name;}
public void setName(String name) {this.name = name; }
}
class MainBoard {
String cpu;
VideoCard vc;
void setCPU(String cpu) { this.cpu = cpu;}
void setVideoCard(VideoCard vc) { this.vc = vc;}
void run() {
System.out.println(this.cpu);
System.out.println(vc.getName());
vc.display();
System.out.println("Mainboard is working.");
}
}
public class Exp6_2{
public static void main(String[] args) {
MainBoard mb = new MainBoard();
String cpu = "Intel's CPU";
VideoCard vc = new Dmeng();
mb.setCPU(cpu);
mb.setVideoCard(vc);
mb.run();
}
}
作业与作业之间用5行分开
第七次作业:
1.举例说明finally关键字
import javax.swing.*;
public class Exp6_6{
public static void main(String arg[])
{
String s=JOptionPane.showInputDialog("your age?");
try{
int age=Integer.parseInt(s);
System.out.println(100/age);
}catch(NumberFormatException e){
System.out.println("You should input a integer");
}finally{
System.out.println("bye.");
System.exit(0);
}
}
}
作业与作业之间用5行分开
第八次作业:
1.简单读取文件:读取Java应用程序的源代码并正确显示汉字。
import java.io.*;
public class Exp8_1{
public static void main(String args[]){
byte[] buffer=new byte[2056];
try
{
FileInputStream fileIn=new
FileInputStream("C:\\A.java");
int bytes=fileIn.read(buffer,0,2056);
String str=new String(buffer,0,bytes,"gb2312");
System.out.println(str);
}
catch(Exception e)
{
String err=e.toString();
System.out.println(err);
}
}
}
作业与作业之间用5行分开
第九次作业:
1.举例说明生成并启动线程程序
public class Exp9_1 extends Thread
{
public void run(){
System.out.println("1");
System.out.println("2");
}
public static void main(String args[]){
Thread obj1=new Exp9_1();
Thread t1=new Thread(obj1);
t1.start();
}
}
作业与作业之间用5行分开
第十次作业:
1.创建了一个黄色面板,并且将该面板加到一个Frame对象中
import java.awt .*;
public class Exp7_2 extends Frame{
//构造函数
public Exp7_2(String str){
super(str);
}
public static void main(String args[]){
Exp7_2 fr = new Exp7_2("Frame with panel");
Panel pan = new Panel();
fr.setSize(300,200);
fr.setBackground(Color.blue);
fr.setLayout(null);
pan.setSize(100,100);
pan.setBackground(Color.yellow);
fr.add(pan);
fr.setVisible(true);
}
}
作业与作业之间用5行分开
第十一次作业:
1.举例说明Applet几个方法的调用过程。
import java.awt.*;
import java.applet.Applet;
public class exp10_1 extends Applet
{
public static int colors=10;
Font font;
public void init()
{
System.out.println("Now init");
Font Font= new Font("TimesRoman",font.PLAIN,36);
}
public void start()
{
System.out.println("now start");
}
public void stop()
{
System.out.println("now stop");}
public void paint(Graphics g)
{
//随即生成颜色
int red=(int) (Math.random()*50);
int green=(int)(Math.random()*50);
int blue=(int)(Math.random()*256);
g.setFont(font);
g.setColor(new Color((red+colors*30)%256,(green+colors/3)%256,blue));
colors=colors+10;
System.out.println("now paint");
g.drawString("hello",30,30);
}
public void destroy()
{
System.out.println("now destroy");
}
}
本文档为【Java】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。