资源描述
一 JAVA上机实验编程题答案
(红色字体为重要知识点)
JAVA上机题(0)
1 某应用软件的登录密码是123(整数),用户从键盘输入密码,如果正确则输出显示“欢迎使用本软件”的字样,如果密码错误则输出显示“密码错,请重新输入!”的字样。(用==运算符实现)
import java.util.*;
class mima{
void f(){
System.out.println("请输入密码:");
}
}
public class sj0_1 {
public static void main(String args[]){
Scanner reader=new Scanner(System.in);
mima m=new mima();
m.f();
int s = 0;
s=reader.nextInt();
while(s!=123)
{
System.out.println("密码错误,请重新输入:");
s=reader.nextInt();
}
System.out.printf("欢迎使用本软件!");
}
}
2 5个学生(每个学生学习了3门课),输入5个学生的学号(整数)和3门课程的成绩(整数),统计并输出3门课程总分最高的学生的学号和总分。(二维数组实现)
import java.util.*;
public class sj0_2 {
public static void main(String args[]){
int Stuscore[][]=new int[5][5];
Scanner reader=new Scanner(System.in);
int xuehao=0,Maxsum=0;
System.out.printf("请输入学生的学号和各科的成绩:\n");
for(int i=0;i<5;i++){
for(int j=0;j<4;j++)
Stuscore[i][j]=reader.nextInt();
System.out.printf("\n");
}
for(int i=0;i<5;i++){
for(int j=1;j<4;j++)
Stuscore[i][4] +=Stuscore[i][j];
if(Stuscore[i][4]>Maxsum)
Maxsum=Stuscore[i][4];
if(Maxsum==Stuscore[i][4])
xuehao=i;
}
System.out.printf("总分最高的学生的学号和总分为:%d,%d",Stuscore[xuehao][0],Maxsum);
}
}
3 从键盘上输入若干成绩(正整数),输入的数以0为中止标记,输出最高成绩、最低成绩、90分以上人数。(循环实现)
import java.util.*;
public class sj0_3 {
public static void main(String args[]){
int[] grade = new int[100];
int i=0,r,max=0,min=100,count=0;
Scanner result = new Scanner(System.in);
System.out.printf("输入成绩:%n");
r=result.nextInt();
while(r!=0){
grade[i]=r;
if(max<grade[i]){
max=grade[i];
}
if(min>grade[i]){
min=grade[i];
}
if(grade[i]>=90){
count=count+1;
}
i=i+1;
System.out.printf("输入下一个成绩:%n");
r=result.nextInt();
}
System.out.printf("最高成绩:%d%n", max);
System.out.printf("最低成绩:%d%n", min);
System.out.printf("90分以上人数:%d%n", count);
}
}
JAVA上机题(一)
1 设计一个描述二维平面上点的类position, 该类有2个成员变量x,y ,在另一主类的main方法中使用position类创建两个对象变量source , target , 通过position类的有参构造方法对两个对象变量的x,y坐标赋初值,最后输出它们的距离与它们的坐标。 参考教材P44 4-3
import java.util.*;
class Source {
double x;
double y;
public double getX() {
return x; }
public double getY() {
return y; }
public Source(double x,double y){
this.x=x;
this.y=y;
}
public double Target(Source p){
double rrr = (this.x-p.getX())*(this.x-p.getX())+(this.y-p.getY())*(this.y-p.getY());
double res = Math.sqrt(rrr);
return res;
}
}
public class sj1_1 {
public static void main(String[] args) {
Source p1 = new Source(3,2);
Source p2 = new Source(5,8);
double distance = p1.Target(p2);
System.out.println("p1的坐标是:("+p1.getX()+","+p1.getY()+")");
System.out.println("p2的坐标是:("+p2.getX()+","+p2.getY()+")");
System.out.println("p1,p2之间的距离:"+distance); }}
2 定义圆柱体类Cylinder,该类有2个成员变量r , h , 有分别计算面积、体积的成员方法, 在main方法中创建圆柱体类Cylinder的对象变量,通过Cylinder类的方法对r , h赋值,最后输出它的面积与体积。
class Cylinder{
double r,h;
Cylinder(){};
Cylinder(double a,double b){
r=a;
h=b;
}
public void setr(double a){
r=a;
}
public void seth(double b){
h=b;
}
double Area(){
return (2*3.14*r*h+2*3.14*r*r);
}
double Volume(){
return (3.14*r*r*h);
}
}
public class sj1_2 {
public static void main(String[] args){
double area,volume;
Cylinder circle = new Cylinder(2,3);
area=circle.Area();
volume=circle.Volume();
System.out.println("圆柱体的面积是:"+area);
System.out.println("圆柱体的体积是:"+volume);
}
}
3定义一个学生类(Student),成员变量有private的名字(name)和年龄(age),在另外的主类main方法中创建一个学生对象,给成员name和age赋值(要通过成员方法赋值与带出值),最后输出学生的名字name和年龄age
class Student{
private String name;
private int age;
Student(){}
Student(String a,int b){
name=a;
age=b;
}
public void setname(String a){
name=a;
}
public void setage(int b){
age=b;
}
String getname(){
return name;
}
int getage(){
return age;
}
}
public class sj1_3 {
public static void main(String[] args){
Student student=new Student();
student.setname("张三");
student.setage(21);
System.out.println("该学生的姓名和年龄是:"+student.getname()+","+student.getage());
}
}
4 编写一个fc类,该类封装了一元二次方程共有的成员变量与成员方法,方程的三个系数为3个成员变量,其中最后一个常量系数为该类所有对象共享项,成员方法为计算实根。在另一主类main方法中创建fc类的2个对象,最后输出它们的实根。
class FC{
double a,b;
static double c=1;
FC(double a,double b)
{
this.a=a;
this.b=b;
}
public void getGen()
{
if(b*b-4*a*c<0){
System.out.println("实根不存在");
}
else if(b*b-4*a*c==0){
System.out.println("只有一个根:"+((-b)+Math.sqrt(b*b-4*a*c))/(2*a));
}
else{
System.out.println("x1="+((-b)-Math.sqrt(b*b-4*a*c))/(2*a)+
" x2="+((-b)+Math.sqrt(b*b-4*a*c))/(2*a));
}
}
}
public class sj1_4 {
public static void main(String[] args)
{
FC f1 = new FC(2,3);
FC f2 = new FC(1,1);
f1.getGen();
f2.getGen();
}
}
JAVA上机题(二)
第5章内容
1 掌握类的继承并应用,要求:
(1)定义一个People类, 该类具有类型为double,访问权限是protected的成员变量:height和weight; 以及方法:public void speakHello(), public void averageHeight()和public void averageWeight()。
(2)定义一个ChinaPeople类, ,它是People的子类,新增了public void chinaGongfu()方法,并要求ChinaPeople类重写父类的public void speakHello(), public void averageHeight()和public void averageWeight()方法。
(3)定义一个BeijingPeople类, ,它是ChinaPeople的子类,新增了public void beijingOpera()方法,并要求BeijingPeople类重写父类的public void speakHello(), public void averageHeight()和public void averageWeight()方法。
(4)定义一个主类,在主类中用上述类生成对象以及使用对象。
class People{
protected double height,weight;
People(double height,double weight){
this.height = height;
this.weight = weight;
}
public void speakHello(){
System.out.println("Hello");
}
public void averageHeight(){
System.out.println("人类的平均身高:"+height);
}
public void averageWeight(){
System.out.println("人类的平均体重:"+weight);
}
}
class ChinaPeople extends People{
ChinaPeople(double height,double weight){
super(height,weight);
}
public void chinaGongfu(){
System.out.println("太极");
}
public void speakHello(){
System.out.println("你好");
}
public void averageHeight(){
System.out.println("中国人的平均身高:"+height);
}
public void averageWeight(){
System.out.println("中国人的平均体重:"+weight);
}
}
class BeijingPeople extends ChinaPeople{
BeijingPeople(double height,double weight){
super(height,weight);
}
public void beijingOpera(){
System.out.println("京剧");
}
public void speakHello(){
System.out.println("您好");
}
public void averageHeight(){
System.out.println("北京人的平均身高:"+height);
}
public void averageWeight(){
System.out.println("北京人的平均体重:"+weight);
}
}
public class sj2_1 {
public static void main(String args[]){
People p = new People(160,120);
p.speakHello();
p.averageHeight();
p.averageWeight();
ChinaPeople cp = new ChinaPeople(165,115);
cp.chinaGongfu();
cp.speakHello();
cp.averageHeight();
cp.averageWeight();
BeijingPeople bp = new BeijingPeople(170,110);
bp.beijingOpera();
bp.speakHello();
bp.averageHeight();
bp.averageWeight();
}
}
2掌握抽象类的应用,要求:
定义抽象类Shape,有2个,抽象方法为showArea()、showzc(),分别求出面积、周长并显示,定义矩形类Rectangle,正方形类Square,圆类 Circle,定义各自类的属性,用showArea方法、showzc()方法求出各自的面积、周长。定义主类,并在main方法中构造3个对象,调用showArea方法、showzc()方法。
abstract class Shape{
abstract double showArea();
abstract double showzc();
}
class Rectangle extends Shape{
double weight,height;
Rectangle(double weight,double height){
this.weight = weight;
this.height = height;
}
double showArea() {
return weight*height;
}
double showzc() {
return (weight+height)*2;
}
}
class Square extends Shape{
double bian;
Square(double bian){
this.bian = bian;
}
double showArea() {
return bian*bian;
}
double showzc() {
return 4*bian;
}
}
class Circle extends Shape{
double r;
Circle(double r){
this.r = r;
}
double showArea() {
return 3.14*r*r;
}
double showzc() {
return 2*3.14*r;
}
}
public class sj2_2 {
public static void main(String args[]){
Rectangle re = new Rectangle(2,4);
Square sq = new Square(3);
Circle ci = new Circle(1);
System.out.println("长方形的面积:"+re.showArea());
System.out.println("长方形的周长:"+re.showzc());
System.out.println("正方形的面积:"+sq.showArea());
System.out.println("正方形的周长:"+sq.showzc());
System.out.println("圆的面积:"+ci.showArea());
System.out.println("圆的周长:"+ci.showzc());
}
}
3掌握接口的应用,要求:
设计一个程序可以计算平面图形的面积。
(1)使用interface关键字定义Shape接口,接口中包含一个求解面积的方法定义;
(2)使用extends从接口Shape派生出接口Shape2D,并为接口Shape2D添加一个求解周长的方法定义;
(3)编写一个Circle类,该类实现Shape2D接口;
(4)定义一个Scaleable接口,该接口中定义一个scale(double proportion)方法;
(5) 编写一个可缩放的圆形CircleScaleable类,该类继承于Circle类同时实现Scaleable接口;
(6)定义主类,在主类中生成CircleScaleable类的若干对象,并计算其缩放前后的面积和周长。
interface Shape{
double showArea();
}
interface Shape2D extends Shape{
double showzc();
}
class Circle implements Shape2D{
double r;
Circle(double r){
this.r = r;
}
public double showArea() {
return 3.14*r*r;
}
public double showzc() {
return 2*3.14*r;
}
}
interface Scaleable{
void scale(double proportion);
}
class CircleScaleable extends Circle implements Scaleable{
CircleScaleable(double r) {
super(r);
}
public void scale(double proportion) {
r=r*proportion;
}
}
public class sj2_3 {
public static void main(String args[]){
CircleScaleable cs1 = new CircleScaleable(4);
CircleScaleable cs2 = new CircleScaleable(6);
System.out.println("cs1缩放前面积:"+cs1.showArea());
System.out.println("cs1缩放前周长:"+cs1.showzc());
cs1.scale(0.5);
System.out.println("cs1缩放后面积:"+cs1.showArea());
System.out.println("cs1缩放后周长:"+cs1.showzc());
System.out.println();
System.out.println("cs2缩放前面积:"+cs2.showArea());
System.out.println("cs2缩放前周长:"+cs2.showzc());
cs2.scale(2);
System.out.println("cs2缩放后面积:"+cs2.showArea());
System.out.println("cs2缩放后周长:"+cs2.showzc());
}
}
4掌握抽象类、接口的应用与区别,要求(2种方法实现):
(1)定义抽象类DiagArea,其中包含方法double getDiagonal()求对角线长, double getArea()求面积,定义一个矩形类,定义成员变量(矩形有长w和宽h),定义带参数的构造函数,定义一个方法(用于一次直接显示边长、面积和对角线长),定义一个正方形类继承矩形类(正方形有边x),定义方法用于输出参数、另一方法输出参数。在主类中的主方法里定义矩形类、正方形类的对象变量并使用。
abstract class DiagArea{
abstract double getDiagonal();
abstract double getArea();
}
class Rectangle extends DiagArea{
double w,h;
Rectangle(double x){
w = x;
h = x;
}
Rectangle(double w,double h){
this.w = w;
this.h = h;
}
double getDiagonal() {
return Math.sqrt(w*w+h*h);
}
double getArea() {
return w*h;
}
void shuchu(){
System.out.println("长:"+w);
System.out.println("宽:"+h);
System.out.println("对角线:"+getDiagonal());
System.out.println("面积:"+getArea());
}
}
class Square extends Rectangle{
double x;
Square(double x){
super(x);
this.x = x;
}
void setx(double x){
super.h = x;
super.w = x;
}
double getx(){
return x;
}
}
public class sj2_4_1 {
public static void main(String args[]){
Rectangle re = new Rectangle(6,8);
Square sq = new Square(4);
System.out.println("长方形");
re.shuchu();
System.out.println("正方形");
sq.shuchu();
}
}
(2)定义接口DiagArea,其中包含方法double getDiagonal()求对角线长, double getArea()求面积,定义一个矩形类,实现此接口,定义成员变量(矩形有长w和宽h),定义带参数的构造函数,定义一个方法(用于一次直接显示边长、面积和对角线长),定义一个正方形类继承矩形类(正方形有边x),定义方法用于输出参数、另一方法输出参数。在主类中的主方法里定义矩形类、正方形类的对象变量并使用。
interface DiagArea{
double getDiagonal();
double getArea();
}
class Rectangle implements DiagArea{
double w,h;
Rectangle(double x){
w = x;
h = x;
}
Rectangle(double w,double h){
this.w = w;
this.h = h;
}
public double getDiagonal() {
return Math.sqrt(w*w+h*h);
}
public double getArea() {
return w*h;
}
void show(){
System.out.println("长:"+w);
System.out.println("宽:"+h);
System.out.println("对角线:"+getDiagonal());
System.out.println("面积:"+getArea());
}
}
class Square extends Rectangle{
double x;
Square(double x){
super(x);
this.x = x;
}
void setx(double x){
super.h = x;
super.w = x;
}
double getx(){
return x;
}
}
public class sj2_4_2 {
public static void main(String args[]){
Rectangle re = new Rectangle(6,8);
Square sq = new Square(4);
System.out.println("长方形");
re.show();
System.out.println("正方形");
sq.show();
}
}
5 掌握JAVA中字符串、字符数组的使用
用JAVA字符串与字符数组知识判断从键盘输入的字符串是否为回文,并计算回文的长度,并将回文的后面一半加密。(例“ABC CBA”是回文,长度为4)
import java.util.Scanner;
class HuiWen{
char c[];
String s;
HuiWen(){
Scanner reader = new Scanner(System.in);
s = reader.nextLine();
c = s.toCharArray();
if(isHuiWen()){
System.out.println("是回文");
System.out.println("长度为:"+c.length/2);
jiaMi();
String code = new String(c);
System.out.println("密文:"+code);
jiaMi();
String code1 = new String(c);
System.out.println("原文:"+code1);
}
else
System.out.println("不是回文");
}
boolean isHuiWen(){
for(int i=0;i<c.length;i++){
if(c[i]!=c[c.length-i-1])
return false;
}
return true;
}
int len(){
return c.length+1;
}
void jiaMi(){
for(int i=(c.length/2);i<c.length;i++){
c[i] = (char)(c[i]^'w');
}
}
}
public class sj2_5 {
public static void main(String args[]){
new HuiWen();
}
}
6 输入多个人的11位移动电话号码串,统计“189”开头的电话号码有几人(用JAVA字符串/字符数组)
import java.util.Scanner;
public class sj2_6 {
public static void main(String args[]){
int sum=0;
Scanner r=new Scanner(System.in);
System.out.println("请输入人数:");
int n=r.nextInt();
String s[]=new String[n];
Scanner reader=new Scanner(System.in);
for(int i=0;i<n;i++)
{
System.out.println("请输入第"+(i+1)+"个数");
s[i]=reader.nextLine();
if(s[i].startsWith("189"))
sum++;
}
System.out.println("“189”开头的电话号码的个数为:"+sum);
}
}
Java 上机题 ( 三)
1实现对文章中单词的个数的统计并输出,以空隔为分割符,并将文章中相同单词出现的个数最多的前三个单词输出。文章字符串从键盘输入,回车结束。分别用三种方法程序实现(stringTokenize类、正则表达式、Scanner类)
用stringTokenize类实现:
import java.util.*;
public class sj3_1_1 {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
System.out.println("请输入文章字符串:");
String s=reader.nextLine();
StringTokenizer word=new StringTokenizer(s);
String ws[]=new String[word.countTokens()]; //存放不同单词
int wnum[]=new int[word.countTokens()]; //存放单词出现次数
int count=0; //数组中不同单词个数
for (int i=0;word.hasMoreTokens();i++)
{
String str=word.nextToken();
int same=0; //标志位,用于2个单词的比较,相同值为1
if (i==0)
{
ws[i]=str;
wnum[i]=1;
count=1;
}
else
{
for (int j=0; j<=count-1;j++) //从第2个单词开始要与数组内单词比较
{
if (pareTo(ws[j])==0) //单词比较相同,计数加1
{
wnum[j]=wnum[j]+1;
same=1;
break;
}
}
if (same==0) //单词比较不相同,新单词插入数组尾部,计数加1
展开阅读全文