//圓類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房,一切都跟這寒酸氣十足而暗裡蹲著冒險家的飯廳調和。她聞著室內暖烘烘的臭味,一點不覺得難受。她的面貌象秋季初霜一樣新鮮,眼睛四周布滿皺紋,表情可以從舞女那樣的滿面笑容,一變而為債主那樣的豎起眉毛,板起臉孔。總之她整個的人品足以說明公寓的內容,正如公寓可以暗示她的人品。監獄少不了牢頭禁卒,你想像中決不能有此無彼。這個小婦人的沒有血色的肥胖,便是這種生活的結果,好象傳染病是醫院氣息的產物。罩裙底下露出毛線編成的襯裙,罩裙又是用舊衣衫改的,棉絮從開裂的布縫中鑽出來;這些衣衫就是客室,飯廳,和小園的縮影,同時也泄露了廚房的內容與房客的流品。她一出場,舞檯面就完全了。五十歲左右的伏蓋太太跟一切經過憂患的女人一樣。無精打採的眼睛,假惺惺的神氣象一個會假裝惱怒,以便敲竹杠的媒婆,而且她也存心不擇手段的討便宜,倘若世界上還有什麼喬治或畢希葛呂可以出賣,她是決計要出賣的。②房客們卻說她骨子裡是個好人,他們聽見她同他們一樣咳嗽,哼哼,便相信她真窮。伏蓋先生當初是怎麼樣的人,她從無一宇提及。他怎樣丟了家私的呢?她回答說是遭了惡運。他對她不好,只留給她一雙眼睛好落眼淚,這所屋子好過活,還有給了她不必同情別人災禍的權利,因為她說,她什麼苦難都受盡了聖-瑪梭城關之間的聖-日內維新街上。大家稱為伏蓋家的這所寄宿舍,男女老少,一律招留,從來沒有為了風化問題受過飛短流長的攻擊,可是三十年間也不曾有姑娘們寄宿;而且非要家庭給麴生活費少得可憐,才能使一個青年男子住到這兒來。話雖如此,一八一九年上,正當這幕慘劇開場的時候,公寓里的確住著一個可憐的少女。雖然摻劇這個字眼被近來多愁善感,頌贊痛苦的文學用得那麼濫,那麼歪曲,以致無人相信;這兒可是不得不用。並非在真正的宇義上說,這個故事有什麼戲劇意味;但我這部書完成之後,京城內外也許有人會掉幾滴眼淚。出了巴黎是不是還有人懂得這件作品,確是疑問;書中有許多考證與本地風光,只有錢在蒙瑪脫崗和蒙羅越高地中間的人能夠領會。這個著名的盆地,牆上的石灰老是在剝落,陽溝內全是漆黑的泥漿;到處是真苦難,空歡喜,而且那麼忙亂,不知要怎麼重大的事故才能在那兒轟動一下。然而也有些東零西碎的痛苦,因為罪惡與德行混在一塊面變得偉大慶嚴,使自私自利的人也要定一定神,生出一點同情心;可是他們的感觸不過是一剎那的事,象匆匆忙忙吞下的一顆美果。文明好比一輛大車,和印度的神車一樣,①碰到一顆比較不容易粉碎的心,略微耽擱了一下,馬上把它壓碎了,又浩浩盪盪的繼續前進。你們讀者大概也是如此:雪白的手捧了這本書,埋在軟綿綿的安樂椅里,想道:也許這部小說能夠讓我消遣一下。讀完了高老頭隱秘的痛史以後,你依舊胃口很好的用晚餐,把你的無動於衷推給作者負責,說作者誇張,瞳染過分。殊不知這慘劇既非杜撰,亦非小說。一切都是真情實事,②真實到每個人都能在自己身上或者心裡發現劇中的要素。