导航:首页 > 编程语言 > java程序设计编程题

java程序设计编程题

发布时间:2022-12-28 12:54:15

java编程

//圆类Circle
import java.util.Scanner;
public class Circle {
private double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
//无参构造函数
public Circle(){
this.radius=0;
}
//带参构造函数
public Circle(double r ){
this.radius=r;
}
//获取面积
public double getArea(){
double r=this.radius;
double area=r*r*3.14;
return area;
}
//获取周长
public double getPerimeter(){
double perimeter=this.radius*2*3.14;
return perimeter;
}
//打印圆的信息
public void show(){
System.out.println("请输入圆的半径");
Scanner sc=new Scanner(System.in);
this.setRadius(sc.nextInt());
System.out.println("圆的半径"+this.getRadius());
System.out.println("圆的面积"+this.getArea());
System.out.println("圆的周长"+this.getPerimeter());
}
}
//圆柱类
public class Cylinder extends Circle {
//圆柱高
private double height;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
//构造方法
public Cylinder (double r, double h){
super();
this.height=h;
this.setRadius(r);
}
public double getVolume( ) {
double volume=this.getArea()*this.height;
return volume;
}
//求体积
public void showVolume(){
System.out.println("圆柱的体积"+this.getVolume());
}
}
//主程序入口
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Circle circle=new Circle();
circle.show();
Cylinder cylinder=new Cylinder(2.0,8.5);
cylinder.showVolume();
}
}
PS:注释写的很详尽了,求采纳

② java编程题

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Demo {

public static void main(String[] args) {
System.out.println(求和(1000));
保存姓名和住址();
}

/**
* 求从0开始连续整数之和
* @param max 从0开始,一直加到max为止
* @return 和
*/
private static int 求和(int max){
int result = 0;
for(int i=0;i<=max;i++)
result += i;
return result;
}

private static void 保存姓名和住址(){
System.out.println("请输入姓名和住址,用逗号隔开。输入‘quit’退出。");
String strInput = null;
File file = new File("姓名住址.txt"); //建立文件
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //接收控制台输入
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))); //输出到文件
bw.append("姓名\t住址");
bw.newLine();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
while(true){
System.out.print("_>");
try {
strInput = br.readLine();
if(strInput.equalsIgnoreCase("quit")){ //不论quit大小写,退出程序
bw.flush();
break;
}else{
if(bw != null){
//不论姓名和住址的分隔符是英文逗号还是中文逗号,均替换为制表符,主要是为了输出的美观
bw.append(strInput.replaceFirst("[,,]", "\t"));
bw.newLine();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

try{
//关闭输入和输出
if(br != null)
br.close();
if(bw != null)
bw.close();
}catch(IOException e){
e.printStackTrace();
}
}
}

③ java程序设计题

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
BufferedOutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(new File("d:/info.txt")));
String line = "第一行文本\n第二行文本";
out.write(line.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream("d:/info.txt"));
StringBuffer buffer = new StringBuffer();
byte[] buff = new byte[in.available()];
while (in.read(buff) != -1) {
buffer.append(new String(buff));
}
System.out.println(buffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
in = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

④ java编程题 本人新手,求详解。

先看下最终的结果吧,是不是你想要的?

其中,Student是父类,PostGraate是子类,继承自父类Student,Main是主类,用于创建对象以及把这些对象的功能调用起来。

---------------------------Student代码如下:------------------------------

/**

* 学生类

* @author 逍遥

*

*/

public class Student {

//学号

private int sId;

//姓名

private String sName;

//数学成绩

private double mathScore;

//计算机成绩

private double computerScore;

/**

* 获取学号

* @return

*/

public int getsId() {

return sId;

}

/**

* 设置学号

* @param sId

*/

public void setsId(int sId) {

this.sId = sId;

}

/**

* 获取姓名

* @return

*/

public String getsName() {

return sName;

}

/**

* 设置姓名

* @param sName

*/

public void setsName(String sName) {

this.sName = sName;

}

/**

* 获取数学成绩

* @return

*/

public double getMathScore() {

return mathScore;

}

/**

* 设置数学成绩

* @param mathScore

*/

public void setMathScore(double mathScore) {

this.mathScore = mathScore;

}

/**

* 获取计算机成绩

* @return

*/

public double getComputerScore() {

return computerScore;

}

/**

* 设置计算机成绩

* @param computerScore

*/

public void setComputerScore(double computerScore) {

this.computerScore = computerScore;

}

/**

* 输出成员变量(4个成员变量)的信息。

*/

public void print(){

System.out.println("学号:"+sId);

System.out.println("姓名:"+sName);

System.out.println("计算机成绩:"+mathScore);

System.out.println("数学成绩:"+computerScore);

}

}

---------------------------Student代码结束------------------------------

---------------------------PostGraate代码如下:------------------------------

/**

* 研究生类

* @author 逍遥

*

*/

public class PostGraate extends Student{

//导师姓名

private String tName;

//研究方向

private String ResearchDirection;

/**

* 获取导师姓名

* @return

*/

public String gettName() {

return tName;

}

/**

* 设置导师姓名

* @param tName

*/

public void settName(String tName) {

this.tName = tName;

}

/**

* 获取研究方向

* @return

*/

public String getResearchDirection() {

return ResearchDirection;

}

/**

* 设置研究方向

* @param researchDirection

*/

public void setResearchDirection(String researchDirection) {

ResearchDirection = researchDirection;

}

/**

* 研究生类重写父类的void print()方法,功能是输出成员变量(6个成员变量)的信息

*/

@Override

public void print() {

// TODO Auto-generated method stub

super.print();

System.out.println("导师姓名:"+tName);

System.out.println("研究方向:"+ResearchDirection);

}

}

---------------------------PostGraate代码结束------------------------------

---------------------------Main代码如下:------------------------------

import java.util.Scanner;


/**

* 主类

* @author 逍遥

*

*/

public class Main {


/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

//用于获取从键盘上输入的信息

Scanner input=new Scanner(System.in);

//创建一个Student类的对象

Student student=new Student();

//从键盘上输入其属性信息

System.out.print("请输入学生的学号:");

student.setsId(input.nextInt());

System.out.print("请输入学生的姓名:");

student.setsName(input.next());

System.out.print("请输入学生的数学成绩:");

student.setMathScore(input.nextDouble());

System.out.print("请输入学生的计算机成绩:");

student.setComputerScore(input.nextDouble());

//并且通过其print方法输出这些信息;

student.print();

//创建一个PostGraate类的对象

PostGraate postGraate=new PostGraate();

//从键盘上输入其属性信息

System.out.print("请输入研究生的学号:");

postGraate.setsId(input.nextInt());

System.out.print("请输入研究生的姓名:");

postGraate.setsName(input.next());

System.out.print("请输入研究生的数学成绩:");

postGraate.setMathScore(input.nextDouble());

System.out.print("请输入研究生的计算机成绩:");

postGraate.setComputerScore(input.nextDouble());

System.out.print("请输入研究生的导师姓名:");

postGraate.settName(input.next());

System.out.print("请输入研究生的研究方向:");

postGraate.setResearchDirection(input.next());

//并且通过其print方法输出这些信息。

postGraate.print();

}


}

---------------------------Main代码结束------------------------------

=================知识点的简单总结=================

本题考察的知识点是面向对象的三大特性之一:继承。

Student为父类,包含了学号、姓名、数学成绩和计算机成绩4个属性,以及一个print()方法。

PostGraate 继承父类的时候,继承了父类中的所有方法,因为方法我都是用的public,而属性继承不了,因为我在父类中用了封装,所有属性都用private修饰了,想访问属性的话,必须通过get、set方法,这里,我重写了父类中的print方法,通过super.print();调用了父类中的print()方法。

最后就是Main类,提供了main方法作为入口函数,用于按要求声明这些对象以及去调用对象中的方法。

⑤ 3道java编程题,求解

packageTestPerson;
/**
*(1)编写程序实现如下功能:已知Person类包含三个公共成员变量(姓名、性别、年龄)和一个构造方法,
*Student类是Person类的派生类,包含两个新的公共成员变量(学号、班号)、两个公共方法(修改年龄、显示基本信息)及一个构造方法。
*在测试类Test1中,定义一组学生对象,并初始化他们的基本信息,然后依次输出。
*/
publicclassTest1{
publicstaticvoidmain(String[]args){
Student[]student=newStudent[3];
student[0]=newStudent("小李","男",12,20181101,01);
student[1]=newStudent("小南","女",13,20001102,01);
student[2]=newStudent("小李","男",12,20181103,01);

for(Studentstu:student){
stu.showInformation();
}
}
}

classPerson{
publicStringname;
publicStringsex;
publicintage;
publicPerson(Stringname,Stringsex,intage){
super();
this.name=name;
this.sex=sex;
this.age=age;
}
}

classStudentextendsPerson{
publiclongstudentId;
publiclongclassId;
publicvoidsetAge(intage){
age=this.age;
}
publicvoidshowInformation(){
System.out.println("我的姓名是"+name+","+"我的性别是"+sex+","+"我的年龄是"+age
+"岁,"+"我的学号是"+studentId+","+"我的班号是"+classId+"班");
}
publicStudent(Stringname,Stringsex,intage,longstudentId,
longclassId){
super(name,sex,age);
this.studentId=studentId;
this.classId=classId;
}
}

不可否认,我现在是有点闲,所以我就帮你写第一个吧,至于后面两个,我就不写了,看看还有没有其他人有点闲时间,看缘分吧

运行结果:

我的姓名是小李,我的性别是男,我的年龄是12岁,我的学号是20181101,我的班号是1班

我的姓名是小南,我的性别是女,我的年龄是13岁,我的学号是20001102,我的班号是1班

我的姓名是小李,我的性别是男,我的年龄是12岁,我的学号是20181103,我的班号是1班

⑥ 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(i<10);
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程序设计题目

3, 文件名:Three.java
public class Three {

public static void main(String[] args) {
Student stu = new Student("Zhang San", true, (short)12);
System.out.println("Student name: " + stu.name);
System.out.println("Student is a male?: " + stu.sex);
System.out.println("Student's age: " + stu.age);
stu.work();
stu.study();

Teacher teacher = new Teacher();
teacher.learnMoney();
}

}

abstract class Person{//抽象类Person
protected String name;
protected boolean sex;
protected short age;

protected abstract void work(); //work抽象方法
}

interface Learnmoney{//Learnmoney接口
public void learnMoney();
}

interface Study{//Study接口
public void study();
}

class Student extends Person implements Study{//Student类

public void work() {
System.out.println("学生的工作是努力学习");
}

public Student(String name, boolean sex, short age){
super.name = name;
super.sex = sex;
super.age = age;
}

public void study() {
System.out.println("学生正在学习");
}

}

class Teacher extends Person implements Learnmoney{

public void work() {
System.out.println("教师的工作是教书育人");;
}

public void learnMoney() {
System.out.println("教师正在赚钱");
}

}
class Docotor extends Person implements Learnmoney{

public void work() {
System.out.println("医生的职责是救死扶伤");
}

public void learnMoney() {
System.out.println("医生正在赚钱");
}

}
------------------------------------
4文件名:Four.java
public class Four {

public static void main(String[] args) {
Rectangle r = new Rectangle(3, 4);
System.out.println("Area is : " + r.area());
System.out.println("Circle is: " + r.circle());
}
}

class Rectangle{
private double width;
private double height;

public Rectangle(double width, double height){
this.width = width;
this.height = height;
}

public double circle(){//求周长
return (width + height) * 2;
}

public double area(){//求面积
return width * height;
}
}

--------------------
5Five.java
public class Five {

public static void main(String[] args) {
AImpl a = new AImpl();
a.paint();
}

}

interface A {
public int method1(int x);
public int method2(int x, int y);
}

class AImpl implements A{

public int method1(int x) {
return (int)Math.pow(x, 5);
}

public int method2(int x, int y) {
return x > y? x: y;
}

public void paint(){
int result1 = method1(2);
int result2 = method2(2, 8);

System.out.println("method1(2) = " + result1);
System.out.println("method2(2, 8) = " + result2);
}

}

-----------------------------测试
method1(2) = 32
method2(2, 8) = 8

⑧ 有几个java编程的题各位好心人有时间的能帮忙写下吗

没那么多时间,帮着写个第1题吧

//编写求一个整数数组A[10,15,12,9,7]中最小元素min和元素之和sum的

int[]a={10,15,15,9,7};

//最小元素

intmin=0;

//数组和

intsum=0;


for(inti=0;i<a.length;i++){

sum+=a[i];

if(i==0){

min=a[i];

}else{

if(a[i]<min){

min=a[i];

}

}

}


System.out.println("当前数组中最小的元素值是:"+min);

System.out.println("当前数组和是:"+sum);

⑨ java编程题 求助

一个夫家姓伏盖,娘家姓龚弗冷的老妇人,四十年来在巴黎开着一所兼包容饭的公寓,坐落在拉丁区与这间屋子最有光彩的时间是早上七点左右,伏盖太太的猫赶在主人之前,先行出现,它跳上食器柜,把好几罐盖着碟子的牛奶闻嗅一番,呼啊呼啊的做它的早课。不久寡妇出现了,网纱做的便帽下面,露出一圈歪歪斜斜的假头发,懒洋洋的级着愁眉苦脸的软鞋。她的憔悴而多肉的脸,中央耸超一个鹦鹉嘴般的鼻子,滚圆的小手,象教堂的耗子①一般胖胖的身材,膨亨饱满面颊颠耸耸的Rx房,一切都跟这寒酸气十足而暗里蹲着冒险家的饭厅调和。她闻着室内暖烘烘的臭味,一点不觉得难受。她的面貌象秋季初霜一样新鲜,眼睛四周布满皱纹,表情可以从舞女那样的满面笑容,一变而为债主那样的竖起眉毛,板起脸孔。总之她整个的人品足以说明公寓的内容,正如公寓可以暗示她的人品。监狱少不了牢头禁卒,你想象中决不能有此无彼。这个小妇人的没有血色的肥胖,便是这种生活的结果,好象传染病是医院气息的产物。罩裙底下露出毛线编成的衬裙,罩裙又是用旧衣衫改的,棉絮从开裂的布缝中钻出来;这些衣衫就是客室,饭厅,和小园的缩影,同时也泄露了厨房的内容与房客的流品。她一出场,舞台面就完全了。五十岁左右的伏盖太太跟一切经过忧患的女人一样。无精打采的眼睛,假惺惺的神气象一个会假装恼怒,以便敲竹杠的媒婆,而且她也存心不择手段的讨便宜,倘若世界上还有什么乔治或毕希葛吕可以出卖,她是决计要出卖的。②房客们却说她骨子里是个好人,他们听见她同他们一样咳嗽,哼哼,便相信她真穷。伏盖先生当初是怎么样的人,她从无一宇提及。他怎样丢了家私的呢?她回答说是遭了恶运。他对她不好,只留给她一双眼睛好落眼泪,这所屋子好过活,还有给了她不必同情别人灾祸的权利,因为她说,她什么苦难都受尽了圣-玛梭城关之间的圣-日内维新街上。大家称为伏盖家的这所寄宿舍,男女老少,一律招留,从来没有为了风化问题受过飞短流长的攻击,可是三十年间也不曾有姑娘们寄宿;而且非要家庭给曲生活费少得可怜,才能使一个青年男子住到这儿来。话虽如此,一八一九年上,正当这幕惨剧开场的时候,公寓里的确住着一个可怜的少女。虽然掺剧这个字眼被近来多愁善感,颂赞痛苦的文学用得那么滥,那么歪曲,以致无人相信;这儿可是不得不用。并非在真正的宇义上说,这个故事有什么戏剧意味;但我这部书完成之后,京城内外也许有人会掉几滴眼泪。出了巴黎是不是还有人懂得这件作品,确是疑问;书中有许多考证与本地风光,只有钱在蒙玛脱岗和蒙罗越高地中间的人能够领会。这个着名的盆地,墙上的石灰老是在剥落,阳沟内全是漆黑的泥浆;到处是真苦难,空欢喜,而且那么忙乱,不知要怎么重大的事故才能在那儿轰动一下。然而也有些东零西碎的痛苦,因为罪恶与德行混在一块面变得伟大庆严,使自私自利的人也要定一定神,生出一点同情心;可是他们的感触不过是一刹那的事,象匆匆忙忙吞下的一颗美果。文明好比一辆大车,和印度的神车一样,①碰到一颗比较不容易粉碎的心,略微耽搁了一下,马上把它压碎了,又浩浩荡荡的继续前进。你们读者大概也是如此:雪白的手捧了这本书,埋在软绵绵的安乐椅里,想道:也许这部小说能够让我消遣一下。读完了高老头隐秘的痛史以后,你依旧胃口很好的用晚餐,把你的无动于衷推给作者负责,说作者夸张,瞳染过分。殊不知这惨剧既非杜撰,亦非小说。一切都是真情实事,②真实到每个人都能在自己身上或者心里发现剧中的要素。

阅读全文

与java程序设计编程题相关的资料

热点内容
dvd光盘存储汉子算法 浏览:757
苹果邮件无法连接服务器地址 浏览:962
phpffmpeg转码 浏览:671
长沙好玩的解压项目 浏览:144
专属学情分析报告是什么app 浏览:564
php工程部署 浏览:833
android全屏透明 浏览:736
阿里云服务器已开通怎么办 浏览:803
光遇为什么登录时服务器已满 浏览:302
PDF分析 浏览:484
h3c光纤全工半全工设置命令 浏览:143
公司法pdf下载 浏览:381
linuxmarkdown 浏览:350
华为手机怎么多选文件夹 浏览:683
如何取消命令方块指令 浏览:349
风翼app为什么进不去了 浏览:778
im4java压缩图片 浏览:362
数据查询网站源码 浏览:150
伊克塞尔文档怎么进行加密 浏览:892
app转账是什么 浏览:163