A. 幾道java題目,求好心人給下答案,感激不盡
1、public static void main(String[] args)
2、public finall static
3、3
4、抽象類
5、implements
6、類:一個包含屬性、方法,使用class關鍵字定義
對象:創建對象必須使用new關鍵字,對象其實在內存中存儲的是類的引用地址。
7、try是可能發生異常的內容;
catch是發生異常後要處理的代碼;
finally是始終執行的,比如關閉資源等。
8、
publicclassDemo{
//小明今年12歲,他母親比他大20歲。編程計算多少年後他母親的年齡是小明的2倍。
publicstaticvoidmain(String[]args){
System.out.println(getYeah());
}
publicstaticintgetYeah(){
intxmAge=12,mqAge=20,yeah=0;
while(true){
if(mqAge/2==xmAge){
returnyeah;
}
mqAge++;
yeah++;
}
}
}
9、
publicclassDemo{
publicstaticvoidmain(String[]args){
newThread(newPiao()).start();
newThread(newPiao()).start();
newThread(newPiao()).start();
newThread(newPiao()).start();
}
}
classPiaoimplementsRunnable{
privatestaticintpiao=1;
publicvoidrun(){
while(true){
synchronized(Piao.class){
if(piao>100){
System.exit(0);
}
System.out.println(Thread.currentThread().getName()+"出票:"+piao+"號");
piao++;
}
}
}
}
B. (Java類和對象練習).
importjava.util.Arrays;
importjava.util.Comparator;
classStudent
{
publicStringstuNo;
publicStringstuName;
publicintstuAge;
publicStudent(StringstuNo,StringstuName,intstuAge)
{
this.stuNo=stuNo;
this.stuName=stuName;
this.stuAge=stuAge;
}
publicvoidaddAge()
{
stuAge++;
}
publicStringtoString()
{
return"No:"+讓咐stuNo+"Name:"+stuName+"Age:"+stuAge;
}
}
<稿衫Student>
{
@Override
publicintcompare(Studento1,Studento2)
{
//TODOAuto-generatedmethodstub
returno1.stuNo.compareTo(o2.stuNo);
}
}
publicclassTest3
{
publicstaticvoidmain(String[]args)
{
Student[]students=newStudent[5];
students[0]=newStudent("1003","a",12);
students[1]=newStudent("1002","b",13);
students[2]=newStudent("1004","c",14);
students[3]=newStudent("1001","d",15);
students[4]鍵滑腔=newStudent("1005","e",16);
Arrays.sort(students,newStudentOrder());
intcount=0;
for(inti=0;i<5;i++)
{
System.out.println(students[i]);
students[i].addAge();
if(students[i].stuAge>20)
count++;
}
System.out.println("大於20歲的人數:"+count);
}
}
C. Java面向對象,編程題,謝謝!
第一種:
publicstaticvoidmain(Stringargs[]){
Scannerscn=newScanner(System.in);
System.out.print("第一個數:");
inta=scn.nextInt();
System.out.print("操作(+,-,*,/):");
Stringoption=scn.next();
System.out.print("第二個數:");
intb=scn.nextInt();
intresult=0;
if("+".equals(option)){
result=a+b;
}
if("-".equals(option)){
result=a-b;
}
if("*".equals(option)){
result=a*b;
}
if("/".equals(option)){
result=a/b;
}
System.out.println("結果:"+result);
}
第二種:
publicstaticvoidmain(Stringargs[]){
Scannerscn=newScanner(System.in);
System.out.print("請輸入第一個數:");
intoNumFirst=scn.nextInt();
System.out.print("請輸入操作方式(+,-,*,/):");
Stringoption=scn.next();
System.out.print("請輸入第二個數:");
intoNumSecond=scn.nextInt();
intresult=0;
switch(option){
case"+":
result=oNumFirst+oNumSecond;
break;
case"-":
result=oNumFirst-oNumSecond;
break;
case"*":
result=oNumFirst*oNumSecond;
break;
case"/":
try{
result=oNumFirst/oNumSecond;
}catch(ArithmeticExceptione){
e.printStackTrace();
System.out.println("除數不能為0");
}
break;
}
System.out.println("結果為:"+result);
}
第三種:
/**
*計算器類
*CreatedbyHDLon2016/11/30.
*/
publicclassOptioner{
privateintoNumFirst;//第一個數
privateintoNumSecond;//第二個數
publicintgetoNumFirst(){
returnoNumFirst;
}
publicvoidsetoNumFirst(intoNumFirst){
this.oNumFirst=oNumFirst;
}
publicintgetoNumSecond(){
returnoNumSecond;
}
publicvoidsetoNumSecond(intoNumSecond){
this.oNumSecond=oNumSecond;
}
publicOptioner(){
}
publicOptioner(intoNumFirst,intoNumSecond){
this.oNumFirst=oNumFirst;
this.oNumSecond=oNumSecond;
}
publicintgetReuslt(){
return0;
}
}1617181920212223242526272829
/**
*加法運算
*CreatedbyHDLon2016/11/30.
*/
{
@Override
publicintgetReuslt(){
returngetoNumFirst()+getoNumSecond();
}
}
/**
*減法運算
*CreatedbyHDLon2016/11/30.
*/
{
@Override
publicintgetReuslt(){
returngetoNumFirst()-getoNumSecond();
}
}
/**
*乘法運算
*CreatedbyHDLon2016/11/30.
*/
{
@Override
publicintgetReuslt(){
returngetoNumFirst()*getoNumSecond();
}
}
/**
*除法運算
*CreatedbyHDLon2016/11/30.
*/
{
@Override
publicintgetReuslt(){
if(getoNumSecond()==0){
System.out.println("除數不能為0");
return-1;
}else{
returngetoNumFirst()/getoNumSecond();
}
}
}
測試
publicstaticvoidmain(Stringargs[]){
Scannerscn=newScanner(System.in);
System.out.print("請輸入第一個數:");
intoNumFirst=scn.nextInt();
System.out.print("請輸入操作方式(+,-,*,/):");
Stringopt=scn.next();
System.out.print("請輸入第二個數:");
intoNumSecond=scn.nextInt();
Optioneroptioner=null;
switch(opt){
case"+":
optioner=newAddOptioner();
break;
case"-":
optioner=newSubOptioner();
break;
case"*":
optioner=newMulOptioner();
break;
case"/":
optioner=newDivOptioner();
break;
}
optioner.setoNumFirst(oNumFirst);
optioner.setoNumSecond(oNumSecond);
System.out.println("結果為:"+optioner.getReuslt());
}
D. Java面向對象程序設計考試題目 類的定義 繼承 創建對象 構造方法
public class Geometry {
public Geometry(int w, int h) {
width = w;
height = h;
}
public int area() {
return width * height;
}
private int width, height;
}
public class Cube extends Geometry {
public Cube(int w, int h) {
super(w, h);
}
public Cube(int a, int b, int c) {
super(a, b);
height = c;
}
public void setHeight(int h) {
height = h;
}
public int volumn() {
return area() * height;
}
private int height;
}
public class User {
public static void main(String []args) {
Cube cube1 = new Cube(1,2,3);
Cube cube2 = new Cube(4, 5);
cube2.setHeight((int) (Math.random() * 10) + 1);//avoid zero height
System.out.println("Cube 1 area: " + cube1.area() + " volumn: " + cube1.volumn());
System.out.println("Cube 2 area: " + cube2.area() + " volumn: " + cube2.volumn());
}
}
E. 有關java編程題目
按照題目要求編寫的圓,圓錐和測試類的Java程序如下
Test.java文件內容如下
class Circle{
private double r;
private String color;
public Circle(double r){
this.r=r;
}
public double area(){
return Math.PI*r*r;
}
public double perimeter(){
return Math.PI*2*r;
}
public double getR(){
return this.r;
}
public void setR(double r){
this.r=r;
}
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color=color;
}
public String toString(){
return "圓的半徑為"+r+",顏色為"+color;
}
}
class Cone{
private Circle c;
private double h;
private String color;
public Cone(Circle c,double h){
this.c=c;
this.h=h;
}
public double volume(){
return 1.0/3*c.area()*h;
}
public Circle getCircle(){
return this.c;
}
public void setCircle(Circle c){
this.c=c;
}
public double getH(){
return this.h;
}
public void setH(double h){
this.h=h;
}
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color=color;
}
public String toString(){
return "圓錐的底面積為"+c.area()+",高為"+h+",顏色為"+color;
}
}
public class Test{
public static void main(String[] args){
Circle circle1=new Circle(2.5);
circle1.setColor("紅色");
System.out.println(circle1.toString());
System.out.println("圓的面積為"+circle1.area());
System.out.println("圓的周長為"+circle1.perimeter());
Cone circlar1=new Cone(circle1,2.7);
circlar1.setColor("藍色");
System.out.println(circlar1.toString());
System.out.println("圓錐的體積為"+circlar1.volume());
}
}
F. 用JAVA編程 類與對象的基礎題
class Phone{
private String phonenumber;
public void setPhonenumber(String phonenumber){
this.phonenumber=phonenumber;
}
public String getPhonenumber(){
return phonenumber;
}
public void recCall(){
System.out.println("接到一個電話");
}
public void telCall(){
System.out.println("撥出一個電話");
}
}class Fixedphone extends Phone{
private String phonenumber;//號碼是私有,設置為private,不可繼承
public void recCall(){
System.out.println("以"+this.phonenumber+"呼出了一個電話"); //重載了父類的recCall
}
}class Cordlessphone extends Fixedphone{
private String phonenumber;
public void info(){
System.out.println("這是無繩電話的信息");
}
}interface Moveable{
public void moveinfo();
}class Mobilephone extends Phone implements Moveable{
private String phonenumber;
public void moveinfo(){
System.out.println("我實現了可移動性");
}
}public class PhoneTest{
public static void main(String a[]){
Phone[] p=new Phone[5];
Phone p1=new Phone();
p1.setPhonenumber("123456789");
p[0]=p1;
Phone p2=new Phone();
p2.setPhonenumber("987654321");
p[1]=p2;
Mobilephone mp=new Mobilephone();
mp.setPhonenumber("11111");
p[2]=mp;
Fixedphone fp=new Fixedphone();
fp.setPhonenumber("22222");
p[3]=fp;
Cordlessphone cp=new Cordlessphone();
cp.setPhonenumber("33333");
p[4]=cp;
for(int i=0;i<p.length;i++){
System.out.println(p[i].getPhonenumber());
} p[4]=p[1];
System.out.println(p[4].getPhonenumber());
}} 寫的不是很好,希望對你有幫助噶
G. java自學,涉及java的String類對象的棧池堆模型的編程題
圖1,s5和s6的標識相同,說明引用的是同一個對象,又由源碼可知,此對象("ab")分配在常量池中。如果s3引用的對象是分配在常量池中的,那s5和s6的標識應該與s3相同,都等於29,但事實上並非如此,所以s3引用的對象是分配在堆中的,即s1+s2這一操作沒有涉及到對池的操作。
而圖2,s,s5,s6的標識相同,說明它們引用的是同一個位於常量池中的對象,而s3與它們3個的標識都不同,再一次說明s3引用的是分配在堆中的對象。