本文主要是介绍Java 编程基础及应用 主编 强彦 赵娟娟 高等教育出版社 课后习题部分代码实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
第一章 引论
略
第二章 Java语言基础
9 编写一个程序,输出你的姓名、年龄、所在学院、所在专业和所在班级
public class Student {
private String name;
private int age;
private String xueyuan;
private String zhuanye;
private String banji;
public Student(){
}
public Student(String name,int age,String xueyuan,String zhuanye,String banji){
this.name=name;
this.age=age;
this.xueyuan=xueyuan;
this.zhuanye=zhuanye;
this.banji=banji;
}
public void ShowInfo(){
System.out.println(this);
}
public String toString(){
return "[学生]\n姓名:" +name+"\n年龄: "+age+"\n所在学院:"+xueyuan+"\n所在专业:"+zhuanye+"\n所在班级:"+banji;
}
public String getName(){
return name;
}
public void setName(){
this.name=name;
}
public int geyAge(){
return age;
}
public void setAge(){
this.age=age;
}
public String getXueyuan(){
return xueyuan;
}
public void setXueyuan(){
this.xueyuan=xueyuan;
}
public String getZhuanye(){
return zhuanye;
}
public void setZhuanye(){
this.zhuanye=zhuanye;
}
public String getBanji(){
return banji;
}
public void setBanji(){
this.banji=banji;
}
}
public class Text {
public static void main(String age[]){
Student s=new Student("chen陈三,"基础科学与技术学院","计算机科学与技术","计科一班");
s.ShowInfo();
}
}
第三章 运算符、表达式和语句
7、分别用if、switch两种语句编写将0-11的整数转换为十二个月份的程序段,假定数字0对应一月份
package cpf;
import java.util.Scanner;
public class P53di7ti {
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);
}
}
第四章 数组
21、有一个整数数组,其中存放着1,3,5,7,9,11,13,15,17,19.将该序列倒叙存放输出
package cpf;
public class P7421 {
public static void main(String args[]) {
int [] arr= {1,3,5,7,9,11,13,15,17,19};
for(int i=9;i>=0;i--) {
System.out.println(arr[i]);
}
}
}
22、编写一个程序,提示用户输入学生的数量、姓名和他们的成绩,并按照成绩的降序打印学生姓名
package cpf;
import java.util.Scanner;
public class P7422 {
private String name;
private int score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public void sort(P7422[] stu,int count)
{
P7422 temp;
for(int i=0;i<count;i++)
{
for(int j=0;j<count-i-1;j++)
{
if (stu[j].getScore()<stu[j+1].getScore())
{
temp=stu[j];
stu[j]=stu[j+1];
stu[j+1]=temp;
}
}
}
}
public void print(P7422[] stu,int count){
for (int i=0; i<count;i++) {
System.out.println(stu[i].getName()+" "+stu[i].getScore());
}
}
}
package cpf;
import java.util.Scanner;
public class P742211 {
public static void main(String[] args){
P7422 s=new P7422();
P7422[] stu= new P7422[20];
Scanner input= new Scanner(System.in);
System.out.println("请输入学生个数:");
int count=input.nextInt();
int num=0;
while(num<count) {
System.out.println("请输入第"+(num+1)+"个学生的姓名和分数:");
Scanner scan=new Scanner(System.in);
String strLine=scan.nextLine();
String[] strLineArr = strLine.split(" ");
P7422 st = new P7422();
st.setName(strLineArr[0]);
st.setScore(Integer.parseInt(strLineArr[1]));
stu[num]=st;
num++;
}
s.sort(stu,count);
System.out.println("排序后的学生信息为:");
s.print(stu,count);
}
}
23、编写一个程序,求出整数数组中最小匀速的下标。如果这样的元素个数大于1的时候,则返回下标最小的数的下标
package cpf;
public class P7423zuixiaoshuxiabiao {
public static void main (String args[]){
int i,min;
int n=0;
int cpf[]={88,55,1,22,11,99,66,33,125,1};
min=cpf[0];
for(i=1;i<10;i++)
{
if(min>cpf[i])
{
min=cpf[i];
n=i;
}
}
System.out.println("最小数的下标为: "+n);
}
}
24、有两个数组:数组a,7,9,11,13,15,17,19,数组b为2.4.6.8.10.将两个数组组合并为数组c按升序排列
package cpf;
public class P7424paixu {
public static void main(String args[]){
int i,j,temp;
int a[]={1,7,9,11,13,15,17,19};
int b[]={2,4,6,8,10};
int c[]=new int[15];
for(i=0;i<a.length;i++){
c[i]=a[i];
}
for (i=a.length,j=0;j<b.length;j++,i++)
{
c[i]=b[j];
}
for(i=0;i<c.length-1;i++)
{
for(j=0;j<c.length-1-i;j++)
{
if(c[j]>c[j+1])
{
temp=c[j+1];
c[j+1]=c[j];
c[j]=temp;
}
}
}
for(i=0;i<c.length;i++)
{
System.out.print(c[i]+" ");
}
}
}
第五章 类和duix对象
10、构造一个银行账户类,类中包括如下内容:
(1) 成员变量:用户的账户名称、用户的账户余额(private类型)
(2)成员方法:开户(设置账户名称及余额),利用构造方法完成。
(3)查询余额
public class P95di10ti {
private String name;// 账户名称
private float balance;// 用户的账户余额
public P95di10ti(String name, float balance) { // 开户(设置账户名称及余额),
this.name = name;
this.balance = balance;
}
public String search() {// 查询余额
return "账户 名称:" + this.name + "\t\t " + "余额为 : " + this.balance+"元整";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getBalance() {
return balance;
}
public void setBalance(float balance) {
this.balance = balance;
}
}
package cpf;
public class Text2 {
public static void main(String[] args) {
P95di10ti acc1 = new P95di10ti("陈三", 99999999999f);// 利用构造方法开户
System.out.println(acc1.search());//查询余额
}
}
11、声明一个图书馆类,其数据成员为书名、编号(利用静态变量实现自动编号)、书价,并拥有静态数据成员册数,记录图书的总册数;在构造方法中,利用静态变量为对iangde编号赋值,在主方法中定义对象数组,并求出总册数。
package cpf;
public class P95di11ti {
private String name;
private int no = 000;
private float price;
private static int count = 0;
private static int sumCnt = 0;
public P95di11ti(String name, float price, int count) {
this.sumCnt = this.sumCnt + count;
this.no = sumCnt;
this.name = name;
this.price = price;
this.count = count;
}
public void print() {
System.out.println("书名: " + this.getName() + "\t" + "编号: "
+ this.getNo() + "\t" + "价格: " + this.getPrice() + "\t"
+ "册数: " + this.getCount());
System.out.println("\n" + "总册书为:" + sumCnt);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public static int getCount() {
return count;
}
public static void setCount(int count) {
P95di11ti.count = count;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public static int getSumCnt() {
return sumCnt;
}
public static void setSumCnt(int sumCnt) {
P95di11ti.sumCnt = sumCnt;
}
}
/*public class Book1 {
public static void main(String args[]) {
Book book1 = new Book("Java编程基础及应用", 36.0f, 10);
book1.print();
Book book2 = new Book("Java大学实用教程", 20.5f, 4);
book2.print();
Book book3 = new Book("名侦探柯南", 36.0f, 6);
book3.print();
Book book4 = new Book("JavaEE基础实用教程", 26.8f, 10);
book4.print();
}
*/
第六章 类的继承与多态
12、定义一个长方形类,该类拥有长、宽、高3个属性及计算体积的方法;定义一个子类继承该长方体类,增加成员变量重量,并增加计算长方体表面积的方法。
package cpf;
public class P106di12ti {
double length;
double width;
double height;
public P106di12ti(int i, int j, int k) {
// TODO Auto-generated constructor stub
}
public void P106di12ti(double length,double width,double height)
{
this.length= length;
this.width= width;
this.height=height;
}
public Object computeVolume()
{
return this.length*this.height*this.width;
}
}
package cpf;
public class Text3 {
public static void main(String args[]) {
P106di12ti r1=new P106di12ti(8,10,10);
System.out.println("长方体体积为"+r1.computeVolume());
Cuboid r2=new Cuboid(10,10,10,10);
System.out.println("长方体表面积为"+r2.computeArea());
}
}
第七章 内部类和异常
编写一个使用自定义异常的小程序
(1)定义mytest
Import java.io.*;
Public class mytest{
Privatestatic int quotient(int number,int denominator)throw
DivideByZeroException{
if(denominator==0)
thrownewDivideByZeroException();
return(number/denominator);
}
publicstaticvoidmain(Stringargs[]){
intnumber1=0,number2=0,result=0;
try{
System.out.println("输入第一个数字:");
number1=Integer.valueOf(Keyboard.getString()).intValue();
System.out.println("输入第二个数字:");
number2=Integer.valueOf(Keyboard.getString()).intValue();
result=quotient(number1,number2);
}
catch(NumberFormatExceptione){
System.out.println(e.toString());
System.exit(-1);
}
System.out.println(number1+"/"+number2+"="+result);
}
}
}
第八章 多线程
1)一个线程输出100个1-26,另一个线程输出100个A-Z
(2)一个行程使用继承Thread类的写法,另一个线程使用实现Runnable接口的写法
package cpf;
public class ThreadTest {
class T1 extends Thread {
public void run() {
for (int i = 0; i < 3; i++) {
for (int j = 1; j <= 26; j++)
{
System.out.println(j);
}
}
}
}
class T2 implements Runnable {
char[] c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
@Override
public void run() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < c.length; j++) {
System.out.println(c[j]);
}
}
}
}
public void test() {
Thread t1 = new T1();
Thread t2 = new Thread(new T2());
t1.start();
t2.start();
}
public static void main(String[] args) {
ThreadTest threadTest = new ThreadTest();
threadTest.test();
}
}
第九章 接口和实现
试编写一个People接口,定义一个抽象类Employee,定义一个具体类Mannagment继承Employee类,并编写测试运行类。
package cpf;
public interface People {
void people();
}
package cpf;
class Management implements People{
public void people() {
System.out.println("我是一名管理层的领导!");
}
}
package cpf;
class Employee implements People{
public void people() {
System.out.println("我是一名普通员工!");
}
}
package cpf;
public class InterfaceTest {
public static void main(String arge[]) {
People a=new Employee();
People b=new Management();
a.people();
b.people();
}
}
这篇关于Java 编程基础及应用 主编 强彦 赵娟娟 高等教育出版社 课后习题部分代码实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!