java网络编程实验报告.docx_第1页
java网络编程实验报告.docx_第2页
java网络编程实验报告.docx_第3页
java网络编程实验报告.docx_第4页
java网络编程实验报告.docx_第5页
免费预览已结束,剩余51页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

网络编程技术键入作者姓名网络C102实验一:Java 基本语法实验目的:了解 Java 的数据类型,掌握各种变量的声明方式,理解运算符的优先级,掌握 Java 基本数据类型、运算符与表达式,掌握顺序结构、选择结构和循环结构语法的程序设计方法。实验要求:1、编写一个声明 Java 不同数据类型变量的程序。2、编写使用不同选择结构的程序。3、编写使用不同循环结构结构的程序。 实验内容:1、声明不同数据类型变量1)编写声明不同数据类型变量的程序public class SimpleTypes public static void main(String args) byte b=0x55; short s=0x55ff; int i=1000000; long l=0xfffL; char c=c; float f=0.23F; double d=0.7E-3; boolean bool=true; System.out.println(b=+b); System.out.println(s=+s); System.out.println(i=+i); System.out.println(l=+l); System.out.println(c=+c); System.out.println(f=+f); System.out.println(d=+d); System.out.println(bool=+bool); 编译并运行该程序,分析程序的运行结果。1)Integer类在某对象中打包了原始类型为int的值。Integer类型对象包含int型的单个域。此外,此类提供了许多方法,可以将int型转换为string型,也可以将Sring型转换为int型,还包含处理int类型时的其他有用常量和方法。public class IntegerDemopublic static void main(String args)Integer array=new Integer(20), new Integer(40), new Integer(110);for(int i=0;iarray.length;i+) System.out.println(Integer.toBinaryString(Value()+t); System.out.println(Integer.toHexString(Value()+t); System.out.println(Integer.toOctalString(Value()+n);编译并运行该程序,分析程序的运行结果2、使用选择结构使用 if.else 语句1)编写源程序文件,代码如下。 public class TestIf public static void main(String args) boolean leap; int year=2005; if (year%4=0 & year%100!=0) | (year%400=0) System.out.println(year+ 年是闰年); else System.out.println(year+ 年不是闰年); year=2008; / 方法 2 if (year%4!=0) leap=false; else if (year%100!=0) leap=true; else if (year%400!=0) leap=false; else leap=true; if (leap=true) System.out.println(year+ 年是闰年); else System.out.println(year+ 年不是闰年); year=2050; / 方法 3 if (year%4=0) if (year%100=0) if (year%400=0) leap=true; else leap=false; else leap=false; else leap=false; if (leap=true) System.out.println(year+ 年是闰年); else System.out.println(year+ 年不是闰年); 1) 编译运行程序,分析程序的运行结果。使用 switch 语句1)编写程序用swith语句实现从键盘读如1,2,3时,屏幕提示不同信息class SwitchTest public static void main(String args) throws java.io.IOException char a; System.out.println(Enter a number from 1-3:); a=(char)System.in.read(); switch(a) case1:System.out.println(win a Car!);break; case2:System.out.println(picked the goat);break; case3:System.out.println(get to keep your 100); break; default:System.out.println(entry); 2)编译运行程序,分析程序的运行结果。3、使用循环结构使用for语句1)程序源代码如下:class TestFor public static void main (String args) throws java.io.IOException int fahr,cels; System.out.println(Celsius Fahrenheit); for(cels=0;cels=100;cels+=5) fahr=cels*9/5+32; System.out.println(cels+fahr); char a; outer:/this is the label for the outer loop for(int i=0;i10;i+) for(int j=0;j10;j+) a=(char)System.in.read(); if(a=b) break outer; if(a=c) continue outer; 2)改正源程序中错误,分析错误原因和程序的运行结果。使用while语句1)程序源代码如下:import java.io.*; class TestWhile public static void main(String args) throws IOException char ch; System.out.println(按 1/2/3 数字键可得大奖!); System.out.println(按空格键后回车可退出循环操作.); while (ch=(char)System.in.read()!= ) System.in.skip(2); / 跳过回车键 switch (ch) case 1: System.out.println(恭喜你得大奖,一辆汽车!); break; case 2: System.out.println(不错呀,你得到一台笔记本电脑!); break; case 3: System.out.println(没有白来,你得到一台冰箱!); break; default: System.out.println(真不幸,你没有奖品!下次再来吧。); 请编译运行程序,分析程序的运行结果。多重循环,输出九九乘法表的程序。 public class TestMul public static void main(String args) int i,j,n=9; System.out.print( * |); for (i=1;i=n;i+) System.out.print( +i); System.out.print(n-|); for (i=1;i=n;i+) System.out.print(-); System.out.println(); for (i=1;i=n;i+) System.out.print( +i+ |); for (j=1;j=90&n=80&n=70&n=60&n=0&n=59) System.out.println(不及格); else System.out.println(输入有误); break; 2) 采用for循环求1至1000之内的所有“完全数”。 所谓“完全数”是指一个数,恰好等于它的因子之和。例如,6是一个完全数,因为6的因子为1、2、3,而61十2十3 public class Wanquanshu public static void main(String args) throws IOException int a,b; System.out.println(满足要求的完全数有:); for(a=1;a=1000;a+) int sum=0; for(b=1;ba;b+) if(a%b=0) sum+=b; if(a=sum) System.out.print( +a); 3)一个整数的各位数字之和能被9整除,则该数也能被9整除。编程验 给定的整数能否被9整除。import java.util.Scanner;public class mmm public static void main(String args) Scanner sc=new Scanner(System.in); System.out.println(请输入一个数); int m=sc.nextInt(); int sum=0; int n=0; for(int i=1;i=10;i+) n=m%10; m=m/10; sum=sum+n; if(m%9=0&sum%9=0) System.out.println(该数可以被9整除); else System.out.println(该数不能被9整除); 4)已知XYZ+YZZ=532,其中X、Y和Z为数字,编程求出X,Y和Z的值。public class XYZ public static void main(String args) int x,y,z,sum=0; for(x=0;x=9;x+) for(y=0;y=9;y+) for(z=0;z=1) & (m=1) & (d=31) ? d : 1); public YMD() this(0,0,0); public static int thisyear() return Calendar.getInstance().get(Calendar.YEAR);/返回当年的年份 public int year() return year;/返回年份 public String toString() return year+-+month+-+day;/返回转化为字符串的年-月-日 l 编写使用包Mypackage中Test_YMD类的程序 YMD_2.java程序功能:给定某人姓名与出生日期,计算该人年龄,并输出该人姓名,年龄,出生日期。程序使用了Test_YMD 的方法来计算年龄。源代码如下package Mypackage;import Mypackage.YMD; public class YMD_2 private String name; private YMD birth; public static void main(String args) YMD_2 a = new YMD_2(张驰,1990,1,11); a.output(); public YMD_2(String n1,YMD d1) name = n1; birth = d1; public YMD_2(String n1,int y,int m,int d) this(n1,new YMD(y,m,d);/初始化变量与对象 public int age() /计算年龄 return YMD.thisyear() - birth.year(); /返回当前年与出生年的差即年龄 public void output() System.out.println(姓名 : +name); System.out.println(出生日期: +birth.toString(); System.out.println(今年年龄 : +age(); 编译并运行程序,分析程序的运行结果。6、编程题,编写程序并写出运行结果 (1)创建一个名称为Pay的类,该类包括工作小时、每小时工资、扣缴率、应得工资总额和实付工资等5个双精度型的成员变量。创建3个重载的应得工资computeNetPay()方法。应得工资是工时乘以每小时工资的计算结果。当computeNetPay()接收代表小时、扣缴率和工资率的数值时,计算出应得工资=工作小时*每小时工资*(1扣缴率)*(1工资率)。当computeNetPay()接收两个参数时,扣缴率假定为15%,计算出应得工资=工作小时*每小时工资*(10.15)*(1工资率)当computeNetPay()接收一个参数时,扣缴率假定为15%,每小时工资率为4.65%。同时编写一个测试类,该测试类的main方法测试所有3个重载的方法。class Paydouble work_hour,price=100,deduct_rate;double should_pay,real_pay=0;public double computeNetPay(double work_hour)should_pay=work_hour*this.price*(1-0.15)*(1-0.0465);return should_pay;public double computeNetPay(double work_hour,double price_rate)should_pay=work_hour*this.price*(1-0.15)*(1-price_rate);return should_pay;public double computeNetPay(double work_hour,double price_rate,double deduct_rate)should_pay=work_hour*this.price*(1-deduct_rate)*(1-price_rate);return should_pay;public void display()System.out.println(The employee should be pay $ +should_pay); public static void main(String args) Pay pay=new Pay(); System.out.println(The first employee worked 8hour); puteNetPay(8.0); pay.display(); System.out.println(The first employee worked 8hour and price_rate 8%); puteNetPay(8.0,0.08); pay.display(); System.out.println(The first employee worked 8hour and price_rate 10% and deduct_rate is 18%); puteNetPay(8.0,0.1,0.18); pay.display(); (2)商店销售某一件商品,商店每天公布统一的折扣(discount)。同时允许销售人员在销售时灵活掌握价格(price),在统一折扣的基础上,对一次购入10件以上者,还可以销售9.5折优惠。现已知当天5名售货员的销售情况为:售货员编号(num) 销售件数(quantity) 销售单价(price) 101 3 126.8221 8 125.6325 10 124.8108 45 123.4901 100 121.5编写销售商品类Sale和含有main方法的公共类Test,计算当天此商品的总销售额sum,以及每件商品的平均售价,并在显示器上显示。import java.io.*;class Saledouble discount,price;int sale_sum=0;double sum=0.0,price_avg=0.0;public void change_discount(double discount)this.discount=discount;public void SaleCount(int person_sale_num,double price)if(person_sale_num10) sum+=price*discount*person_sale_num;else sum+=0.95*price*discount*person_sale_num;sale_sum+=person_sale_num;price_avg=sum/sale_sum;/return sale_sum;public void display()System.out.println(The total sale namber is: +sale_sum);System.out.println(The total sale money is: +sum);System

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论