java期末代码题 java期末考试题及答案简答题
5道简单的JAVA编程题(高分悬赏)
很详细的帮你写下,呵呵,所以要给分哦!
专注于为中小企业提供网站制作、网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业绿春免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了千余家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
1、
(1)源程序如下:
public class One {
public static void main(String[] args) {
String name = "张三";
int age = 23;
char sex = '男';
String myclass = "某某专业2班";
System.out.println("姓名:" + name);
System.out.println("姓名:" + age);
System.out.println("姓名:" + sex);
System.out.println("姓名:" + myclass);
}
}
(2)
编写完程序的后缀名是.java,如本题,文件名就是One.java。
开始\运行\cmd,进入“命令提示符窗口”,然后用javac编译器编译.java文件,语句:javac One.java。
(3)
编译成功后,生成的文件名后缀是.class,叫做字节码文件。再用java解释器来运行改程序,语句:java One
2、编写程序,输出1到100间的所有偶数
(1)for语句
public class Two1 {
public static void main(String[] args) {
for(int i=2;i=100;i+=2)
System.out.println(i);
}
}
(2)while语句
public class Two2 {
public static void main(String[] args) {
int i = 2;
while (i = 100) {
System.out.println(i);
i += 2;
}
}
}
(3)do…while语句
public class Two3 {
public static void main(String[] args) {
int i = 2;
do {
System.out.println(i);
i += 2;
}while(i=100);
}
}
3、编写程序,从10个数当中找出最大值。
(1)for循环
import java.util.*;
public class Three1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
for (int i = 0; i 10; i++) {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max number)
max = number;
}
System.out.println("最大值:" + max);
}
}
(2)while语句
import java.util.*;
public class Three2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
while (i 10) {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max number)
max = number;
i++;
}
System.out.println("最大值:" + max);
}
}
(3)do…while语句
import java.util.*;
public class Three3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
do {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max number)
max = number;
i++;
}while(i10);
System.out.println("最大值:" + max);
}
}
4、编写程序,计算从1到100之间的奇数之和。
(1)for循环
public class Four1 {
public static void main(String[] args) {
int sum=0;
for(int i = 1;i=100;i+=2){
sum+=i;
}
System.out.println("1~100间奇数和:" + sum);
}
}
(2)while语句
public class Four2 {
public static void main(String[] args) {
int sum = 0;
int i = 1;
while (i = 100) {
sum += i;
i += 2;
}
System.out.println("1~100间奇数和:" + sum);
}
}
(3)do…while语句
public class Four3 {
public static void main(String[] args) {
int sum = 0;
int i = 1;
do {
sum += i;
i += 2;
} while (i = 100);
System.out.println("1~100间奇数和:" + sum);
}
}
5、
(1)什么是类的继承?什么是父类?什么是子类?举例说明。
继承:是面向对象软件技术当中的一个概念。如果一个类A继承自另一个类B,就把这个A称为"B的子类",而把B称为"A的父类"。继承可以使得子类具有父类的各种属性和方法,而不需要再次编写相同的代码。在令子类继承父类的同时,可以重新定义某些属性,并重写某些方法,即覆盖父类的原有属性和方法,使其获得与父类不同的功能。另外,为子类追加新的属性和方法也是常见的做法。继承需要关键字extends。举例:
class A{}
class B extends A{}
//成员我就不写了,本例中,A是父类,B是子类。
(2)编写一个继承的程序。
class Person {
public String name;
public int age;
public char sex;
public Person(String n, int a, char s) {
name = n;
age = a;
sex = s;
}
public void output1() {
System.out.println("姓名:" + name + "\n年龄:" + age + "\n性别:" + sex);
}
}
class StudentPerson extends Person {
String school, department, subject, myclass;
public StudentPerson(String sc, String d, String su, String m, String n,
int a, char s) {
super(n, a, s);
school = sc;
department = d;
subject = su;
myclass = m;
}
public void output2() {
super.output1();
System.out.println("学校:" + school + "\n系别:" + department + "\n专业:"
+ subject + "\n班级:" + myclass);
}
}
public class Five2 {
public static void main(String[] args) {
StudentPerson StudentPersonDemo = new StudentPerson("某某大学", "某某系别",
" 某专业", "某某班级", " 张三", 23, '男');
StudentPersonDemo.output2();
}
}
Java 期末编程题
import java.util.Arrays;
import java.util.Scanner;
public class Test
{
private static void p1 ()
{
Scanner sc = new Scanner (System.in);
System.out.println ("从键盘上输入一年份,判断是否为闰年");
int year = sc.nextInt ();
sc.close ();
if (year % 400 == 0 || year % 4 == 0 year % 100 != 0)
{
System.out.println ("是闰年");
}
else
{
System.out.println ("不是闰年");
}
}
private static long p2 ( int n )
{
if (n == 11)
{
return 0;
}
long sum = 1;
for ( int i = 1; i = n; i++ )
{
sum *= i;
}
return sum + p2 (n + 1);
}
private static int p3 ()
{
int sum = 0;
for ( int i = 100; i 201; i++ )
{
boolean yes = true;
for ( int j = 2; j i / 2; j++ )
{
if (i % j == 0)
{
yes = false;
break;
}
}
if (yes)
{
sum += i;
}
}
return sum;
}
private static int p4 ( int n )
{
if (n == 1 || n == 2)
{
return 1;
}
else
{
return p4 (n - 2) + p4 (n - 1);
}
}
private static void p5 ()
{
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 19 };
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for ( int i = 0; i arr.length; i++ )
{
int ai = arr[i];
if (ai max)
{
max = ai;
}
if (ai min)
{
min = ai;
}
}
System.out.println ("最大值:" + max + ", 最小值: " + min);
}
private static void p6 ( int n )
{
int[][] arr = new int[n][];
for ( int i = 0; i arr.length; i++ )
{
arr[i] = new int[i + 1];
for ( int j = 0; j i + 1; j++ )
{
if (j == 0 || j == i)
{
arr[i][j] = 1;
}
else if (i 0 j 0)
{
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
}
}
}
String result = Arrays.deepToString (arr).replaceAll ("[\\[]", "").replaceAll ("\\],\\s", "\r\n").replaceAll ("[,\\]]", "");
System.out.println (result);
}
class P7
{
public int add ( int a, int b )
{
return a + b;
}
public float add ( float a, float b )
{
return a + b;
}
public double add ( double a, double b )
{
return a + b;
}
}
static class Student
{
long id;
String name;
boolean sex;
String phone;
float score;
Student ( long i, String n, boolean s, String p, float sc )
{
this.id = i;
this.name = n;
this.sex = s;
this.phone = p;
this.score = sc;
}
boolean getSex ()
{
return sex;
}
String getPhone ()
{
return phone;
}
float getScore ()
{
return score;
}
@Override
public String toString ()
{
return String.format ("Student [id=%s, name=%s, sex=%s, phone=%s, score=%s]", id, name, sex, phone, score);
}
}
public static void main ( String[] args )
{
p1 ();
System.out.println (p2 (10));
p3 ();
p4 (30);
p5 ();
p6 (10);
Student student = new Student (1000, "YUGI", true, "15878787878", 100);
System.out.println (student.getSex ());
System.out.println (student.getPhone ());
System.out.println (student.getScore ());
System.out.println (student.toString ());
}
}
这两道题代码怎么写java?
创建一个名字为“ReportCard”的类,然后用下边的内容全部替换掉,你会成为全班最亮的仔。
import java.util.HashMap;
/**
* 学生成绩单
*/
public class ReportCard {
public static void main(String[] args) {
ReportCard reportCard = new ReportCard("张三", "070602213");
reportCard.set("语文", 80.0);
reportCard.set("数学", 59.5);
reportCard.set("英语", 66.0);
reportCard.set("java", 80, 99.0);
reportCard.set("数据库", 80, 66.0);
reportCard.set("毛概", null);
System.out.println(reportCard.getStudentName() + "语文分数:" + reportCard.get("语文"));
System.out.println(reportCard.getStudentName() + "数学考核结果:" + (reportCard.isPassed("数学") ? "合格" : "不合格"));
System.out.println(reportCard.getStudentName() + "期末是否挂科:" + (reportCard.isAllPassed() ? "否" : "是"));
}
// 学生姓名
private String studentName;
// 学生学号
private String studentNumber;
// 成绩单
private HashMapString, CourseResult cards = new HashMap();
public ReportCard() {
}
public ReportCard(String studentName, String studentNumber) {
this.studentName = studentName;
this.studentNumber = studentNumber;
}
public Double get(String courseName){
CourseResult courseResult = cards.get(courseName);
return courseResult == null ? Double.NaN : courseResult.getStudentScore();
}
public void set(String courseName, Double studentScore){
CourseResult courseResult = new CourseResult(courseName, studentScore);
cards.put(courseName, courseResult);
}
public void set(String courseName, double passMark, Double studentScore){
CourseResult courseResult = new CourseResult(courseName, passMark, studentScore);
cards.put(courseName, courseResult);
}
public boolean isPassed(String courseName){
return cards.get(courseName).isPassed();
}
public boolean isAllPassed(){
for(CourseResult cr : cards.values()){
if ( ! cr.isPassed()) {
return false;
}
}
return true;
}
public String getStudentName() {
return studentName;
}
public String getStudentNumber() {
return studentNumber;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
/**
* 课程
*/
class Course{
// 课程名称
protected String courseName;
// 及格分
protected double passMark = 60;
public Course(String courseName, Double passMark) {
this.courseName = courseName;
if ( passMark != null) {
this.passMark = passMark;
}
}
}
/**
* 课程成绩
*/
class CourseResult extends Course{
// 学生成绩
private Double studentScore;
public CourseResult(String courseName, Double studentScore) {
this(courseName, null, studentScore);
}
public CourseResult(String courseName, Double passMark, Double studentScore) {
super(courseName, passMark);
this.studentScore = studentScore == null ? Double.NaN : studentScore;
}
public boolean isPassed(){
return studentScore = passMark;
}
public String getCourseName() {
return courseName;
}
public double getPassMark() {
return passMark;
}
public Double getStudentScore() {
return studentScore;
}
}
本文题目:java期末代码题 java期末考试题及答案简答题
网站链接:http://azwzsj.com/article/doeddip.html