JAVA,
JAVA,
1. 编写程序,计算1到100以内奇数和。
要求:完成程序并运行求得计算结果,输出计算结果并有文字提示。
public class t1 {
public static void main(String[] args) {
int total=0;
for(int i=1;i<100;i=i+2)
{
total=total+i;
}
System.out.println("1到100以内奇数和为:"+total);
}
2. 已知函数
设计一个方法实现上面的函数,根据传入x值不同,返回对应的y值。
package com.test1;
import java.io.*;
public class Test2 {
public static void main(String[] args)
{
float y = 0;
try{
InputStreamReader isr=new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.println("传入x值");
//从控制台读取一行数据
String a1 = br.readLine();
// 把 String 转为 int
float x=Float.parseFloat(a1);
if(x>0)
{
y=x+3;
}else if(x==0)
{
y=0;
}else if(x<0)
{
y=x*x-1;
}
System.out.println(y);
}catch(Exception e){
e.printStackTrace();
}
}
}
*****************************************
import java.util.Scanner;
public class Test2_1 {
public static double function(double x) {
if (x > 0) {
return x + 3;
} else if (x == 0) {
return 0.0;
} else {
return x * x - 1;
}
}
public static void main(String args[]) {
Scanner s=new Scanner(System.in);
double x=s.nextInt();
double y = function(x);
System.out.println(y);
}
}
3. 编程实现:将一个文本文件的内容复制到另一个文本文件中。
package com.test1;
import java.io.*;
public class Test3{
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
FileInputStream fis=new FileInputStream("D:/t1.txt");
FileOutputStream fos=new FileOutputStream("D:/t2.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
BufferedOutputStream bos=new BufferedOutputStream(fos);
int ch=0;
while((ch=bis.read())!=-1)
bos.write(ch);
bos.flush();
bis.close();
bos.close();
}
}
4. 编程实现:读取磁盘上一个文本文件的内容并输出。
import java.io.*;
public class Test4{
public static void main(String[] args) {
try {
String encoding="GBK";
File file=new File("D:/t1.txt");
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
5. 有形如int [ ]a = {12,45,34,46,23}的数组定义,编写int getElement(int index)方法,返回数组下标为index的数组元素,考虑可能出现的异常。
import java.util.Scanner;
public class Test5 {
public static int getElement(int index)
{
int a[]= {12,45,34,46,23};
return a[index];
}
public static void main(String[] args)throws Exception {
System.out.println("请输入数组下标");
Scanner s=new Scanner(System.in);
int i=s.nextInt(); //获取键盘的输入
System.out.println("数组下标对应得值为"+getElement(i));
}
}
6. 编程实现:求出整数数组中最小元素的下标,考虑可能出现的异常
public class Test6 {
public static void main(String[] args)
throws Exception
{
int a[]={48,45,34,46,23};
System.out.println("最小元素下标为"+minIndex(a));
}
public static int minIndex(int a[])
{
int min=a[0];
int minIndex=0;
for(int i=1;i<a.length;i++)
{
if(min>a[i])
{
min=a[i];
minIndex=i;
}
}
return minIndex;
}
}
7. 自定义一个异常类NoThisSoundException和Player类,在Player的play()方法中使用自定义异常,要求如下:
(1)NoThisSongException继承Exception类,类中有一个无参和一个接收一个String类型参数的构造方法,构造方法中都使用super关键字调用父类的构造方法。
(2)Player类中定义一个play(int index)方法,方法接收一个int类型的参数,表示播放歌曲的索引,当index>10时,paly()方法用throw关键字抛出NoThisSongException异常,创建异常对象时,调用有参的构造方法,传入“您播放的歌曲不存在”。
(3)在测试类中创建Player对象,并调用play()方法测试自定义的NoThisSongException异常,使用try…catch语句捕获异常,调用NoThisSongException的getMessage()方法打印出异常信息。
class NoThisSongException extends Exception{
public NoThisSongException(){
super();
}
public NoThisSongException(String message){
super(message);
}
}
class Player{
public void play(int index)throws NoThisSongException{
if(index>10){
throw new NoThisSongException("您播放的歌曲不存在");
}
System.out.println("正在播放歌曲");
}
}
public class Test7 {
public static void main(String[] args) {
Player player = new Player();
try {
player.play(13);
} catch (NoThisSongException e) {
System.out.println("异常信息为: "+e.getMessage());
}
}
}
8. 编程实现:输入一个不超过5位的整数,逆序输出各位数字。
import java.util.Scanner;
public class Test8 {
public static void main(String args[]){
System.out.println("请输入一个数小于5位的正整数");
Scanner s=new Scanner(System.in);
long temp=s.nextLong();
if(temp>99999){
System.exit(1);
}
String str=String.valueOf(temp);
char c[]=str.toCharArray();
System.out.println("逆序输出为:");
for(int j=c.length-1;j>=0;j--){
System.out.print(c[j]);
}
}
}
9. 编程实现:将0~11的整数转换为十二个月份,假定数字0对应一月份。
import java.util.Scanner;
public class Test9{
public static void main(String[] args) {
int mouth;
do{
System.out.println("请输入月份:");
mouth=new Scanner(System.in).nextInt();
switch(mouth){
case 0:System.out.println("一月");break;
case 1:System.out.println("二月");break;
case 2:System.out.println("三月");break;
case 3:System.out.println("四月");break;
case 4:System.out.println("五月");break;
case 5:System.out.println("六月");break;
case 6:System.out.println("七月");break;
case 7:System.out.println("八月");break;
case 8:System.out.println("九月");break;
case 9:System.out.println("十月");break;
case 10:System.out.println("十一月");break;
case 11:System.out.println("十二月");break;
default:System.out.println("请重新输入月份");break;
}
}while (mouth>=0&&mouth<=11);
}
}
10. 按照以下要求设计一个学生类Student,并进行测试。
要求如下:
(1)Student类中包含姓名、成绩两个属性
(2)分别给这两个属性定义两个方法,一个方法用于设置值,另一个方法用于获取值
(3)Student类中定义一个无参的构造方法和一个接收两个参数的构造方法,两个参数分别为姓名和成绩属性赋值
(4)在测试类中创建两个Student对象,一个使用无参的构造方法,然后调用方法给姓名和成绩赋值,一个使用有参的构造方法,在构造方法中给姓名和成绩赋值
class Student{
String name;
double grade;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
public Student()
{
}
public Student(String name,double grade)
{
this.name=name;
this.grade=grade;
}
}
public class Test10 {
public static void main(String[] args)
{
Student s1=new Student();
s1.setName("陈");
s1.setGrade(80);
Student s2=new Student("chen",90);
}
}
11. 设计一个学生类Student和它的一个子类Undergraduate,要求如下:
(1)Student类有name和age属性,一个包含两个参数的构造方法,用于给name和age属性赋值,一个show方法打印Student的属性信息。
(2)本科生Undergraduate增加一个degree属性,有一个包含三个参数的构造方法,前两个参数用于给继承的name和age属性赋值,第三个给degree专业赋值,一个show方法用于打印Undergraduate的属性信息。
(3)在测试类中分别创建Student对象和Undergraduate对象,调用它们的show方法。
class Student1 {
public String name;
public int age;
public Student1(String name,int age){
this.name=name;
this.age=age;
}
public void show(){
System.out.println("name: "+name+" age: "+age);
}
}
class UnderGraduate extends Student1{
public String degree;
public UnderGraduate(String name,int age,String degree){
super(name, age);
this.degree=degree;
}
public void show(){
System.out.println("name: "+name+" age: "+age+" degree: "+degree);
}
}
public class Test11{
public static void main(String[] args) {
Student1 student = new Student1("chen", 16);
student.show();
UnderGraduate underGraduate = new UnderGraduate("hui", 20, "bechalor");
underGraduate.show();
}
}
12. 设计一个Shape接口和它的两个实现类Square和Circle,要求如下:
(1)Shape接口中有一个抽象方法area(),方法接收一个double类型的参数,返回一个double类型的结果。
(2)Square和Circle中实现了Shape接口的area()抽象方法,分别求正方形和圆形面积并返回。
(3)在测试类中创建Square和Circle对象,计算边长为2的正方形面积和半径为3的圆形面积。
interface Shape{
abstract double area(double x);
}
class Square implements Shape{
public double area(double x) {
return x*x;
}
}
class Circle implements Shape{
public double area(double r) {
return Math.PI*r*r;
}
}
public class Test12 {
public static void main(String[] args)
{
Shape s= new Square();
Shape circle = new Circle();
System.out.println("正方形面积是"+s.area(2));
System.out.println("圆形面积是"+circle.area(3));
}
}
13. 定义一个People类,具有姓名name和年龄age两个属性,一个构造方法,用来初始化这两个属性。
定义一个People类的子类Student,添加一个属性学号number,添加两个构造方法,一个有参构造方法,一个无参构造方法,他们都要调用父类的构造方法或本类的构造方法来完成对象的初始化。
(1)添加一个成员方法,选课choose(),用来输出“我没有选课”。
(2)添加一个成员方法,选课choose(String),用来选择由参数指定的课程。
创建Student类的对象并测试所有方法。
class People
{
String name;
int age;
public People()
{
this.name=name;
this.age=age;
}
}
class Student2 extends People
{
String number;
public Student2()
{
super();
}
public Student2(String name,int age,String number)
{
super();
this.number=number;
}
public void choose(){
System.out.println("我没有选课");
}
public void choose(String choose){
System.out.println("我所选课程为"+choose);
}
}
public class Test13 {
public static void main(String[]args){
Student2 s3 = new Student2();
s3.choose();
s3.choose("java");
}
}
14. 定义一个长方体类,该类拥有长、宽、高3个属性及计算体积的方法;定义一个子类继承该长方体类,增加属性重量,并增加计算长方体表面积的方法。
创建长方体类及其子类的对象并测试所有方法。
class Rectangle{
double length;
double width;
double height;
public Rectangle(double length,double width,double height)
{
this.length = length;
this.width = width;
this.height=height;
}
public double computeVolume()
{
return this.length*this.height*this.width;
}
}
class Cuboid extends Rectangle{
double weight;
public Cuboid(double length,double width,double height,double weight)
{
super(length,width,height);
this.weight=weight;
}
public double computeArea()
{
return (this.length*this.width+this.length*this.height+this.width*this.height)*2;
}
}
public class Test14 {
public static void main(String[] args)
{
Rectangle r1=new Rectangle(10,10,10);
System.out.println("长方体体积为"+r1.computeVolume());
Cuboid r2=new Cuboid(10,10,10,10);
System.out.println("长方体表面积为"+r2.computeArea());
}
}
15. 编程实现:将1到100以内能被3整除的打印出来。
要求:每行输出5个数字。
public class Test15 {
public static void main(String[] args) {
int n=0;
int i=0;
for(n=1;n<=100;n++){
if(n % 3!=0) //不能被 3整除,结束本次循环
continue;//跳出循环
i++;
System.out.print(n+" ");
if(i % 5==0)
{ //每五个数一行
System.out.println();
}
}
}
}
相关文章
- 暂无相关文章
用户点评