㈠ java程序設計基礎題
選c;
第一個輸出的是(5, 3),應該沒有問題吧,
第二個輸出的是(3, 5) ,是witchCoords()函數里的輸出語句,在witchCoords()函數里的x和y實現了交換,所以是3和5;
第三個輸出的是(5, 3),因為傳遞給switchCoords()函數的值是數據值。switchCoords()里x和y的值改變不會影響實參!
也就是說,傳數據值形參的改變不會影響實參;而傳地址值形參的改變會影響實參。
如果傳遞的是數組,實參會隨著形參而改變。
㈡ JAVA基礎編程題~
public class student{
private String name;
private int age;
public student(String name,int age){ //student的帶有String,int帶參數的構造函數
this.name = name;
this.age = age; //把傳進來的參數賦值給創建出來的類的對象
}
public static void main(String[] args){
student s = new student("zhangsan",18); //調用student的構造函數創 造一個對象s
System.out.println("姓名:"+s.name+"年齡:"+s.age);//調用對象的屬性就用s.name s.age!
}
}
----------------------------------------------------------------------
public class Student {
static String name;//設定String類型的靜態變數name
static int age;//設定int類型的靜態變數age
public static void main(String[] args){//main為主方法,運行從main開始
name="zhangsan";//給name賦值zhangsan
age=18;//給age賦值18
System.out.println("該學生的名字為"+name+"年齡為:"+age);//輸出name和age
}
}
static 修飾的屬性和方法只屬於這個類本身,而不屬於這個類所創建出來的對象,所以可以直接寫name,age 而不用像上面那種要先創建個類的對象出來 !
㈢ java編程題,基礎
packagecom.sunpx.test;
publicclassSong{
protectedStringname;
protectedStringcomposer;
protectedintyear;
publicSong(Stringname,Stringcomposer,intyear){
this.name=name;
this.composer=composer;
this.year=year;
}
publicSong(Stringname){
this.name=name;
}
@Override
publicStringtoString(){
return"Song{"+
"name='"+name+'''+
",composer='"+composer+'''+
",year="+year+
'}';
}
publicStringtoString(intyear){
StringyearN=String.valueOf(year);
return"Song{"+
"name='"+name+'''+
",composer='"+composer+'''+
",year="+yearN+
'}';
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetComposer(){
returncomposer;
}
publicvoidsetComposer(Stringcomposer){
this.composer=composer;
}
publicintgetYear(){
returnyear;
}
publicvoidsetYear(intyear){
this.year=year;
}
}
㈣ JAVA程序設計試題
DCCBBAAAD
㈤ 用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());
}} 寫的不是很好,希望對你有幫助噶
㈥ 幫忙做一個Java程序設計題
//介面
publicinterfaceShape{
();
publicabstractdoublegetArea();
}
//矩形類
{
privatedoublewidth=0;//寬
privatedoubleheight=0;//長
privatedoublearc=0;//對角線
publicMyRect(doublewidth,doubleheight){
this.width=width;
this.height=height;
}
@Override
publicdoublegetPeremeter(){
return(width+height)*2;
}
@Override
publicdoublegetArea(){
returnwidth*height;
}
publicvoidshow(){
arc=Math.sqrt((width*width)+(height*height))
System.out.println("長:"+height+"寬:"+width+"面積:"+getArea()+"對角線:"+arc);
}
}
publicclasstest{
publicstaticvoidmain(String[]args){
MyRectmyRect=newMyRect(20,30);
System.out.println("周長:"+myRect.getPeremeter());
System.out.println("面積:"+myRect.getArea());
myRect.show();
}
}
㈦ JAVA語言程序設計兩道練習題。謝謝!
第一題有問題:1、創建Person介面(即「人」),它有setData()和getData()方法對「人」屬性name、sex和birthday賦值和獲得這些屬性組成的字元串信息。
問題是:你說要創建一個人(介面),然後裡面有方法對人的屬性進行賦值?這怎麼可能呢,介面是沒有成員變數(屬性)的,怎麼能賦值?介面里只能有常量。
第二題可以答一下:
package pillar;
public class Pillar { private Geometry buttom;
private double height;
public Pillar() {
// TODO Auto-generated constructor stub
}
public Pillar(Geometry button,double height){
this.buttom = button;
this.height = height;
}
public double getVolume(){
return this.buttom.getArea()*height;
}
public Geometry getButtom() {
return buttom;
}
public void setButtom(Geometry buttom) {
this.buttom = buttom;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
------------------------------------------------類分割線---------------------------------------------------------
package pillar;
public interface Geometry { double getArea();
}
------------------------------------------------類分割線---------------------------------------------------------
package pillar;
public class Circle implements Geometry { private double r;
public Circle() {
// TODO Auto-generated constructor stub
}
public Circle(double r) {
this.r = r;
}
public double getArea() { return Math.PI*r*r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
}
------------------------------------------------類分割線---------------------------------------------------------
package pillar;
public class Rectangle implements Geometry { private double width;
private double height;
public Rectangle() {
// TODO Auto-generated constructor stub
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getArea() { return this.width*this.height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
------------------------------------------------類分割線---------------------------------------------------------
package pillar;
public class TestPillar {
/** * @param args
*/
public static void main(String[] args) {
Circle c = new Circle(5);
Rectangle r = new Rectangle(3,4);
Pillar p1 = new Pillar(c,6);
Pillar p2 = new Pillar(r,6);
System.out.println("圓的體積:"+p1.getVolume()+"\t矩形的體積:"+p2.getVolume());
}
}
㈧ java基礎編程題求解,請寫的基礎一點不然根本看不懂啊
importjava.util.Scanner;
/*
*@authorww
*
*/
publicclassTest{
publicstaticvoidmain(String[]args){
Scanners=newScanner(System.in);
System.out.println("輸入一個正整數n(n>=2)");
intn=s.nextInt();
StringBuildersb=newStringBuilder();
booleanhas=false;
for(inti=1;i<n;i++){
inttemp=i;
sb.append(i).append("");
for(intj=i+1;j<n;j++){
temp+=j;
sb.append(j).append("");
if(temp==n){
System.out.println(sb.toString());
has=true;
}
}
sb.delete(0,sb.length());
}
if(!has){
System.out.println("NONE");
}
}
}
㈨ 求Java基礎,練習題。
選擇題 (25道)
1. 下列選項中,不屬於Java語言特點的一項是( C )。
A:分布式 B:安全性 C:編譯執行 D:面向對象
2. Java語言的特點與 C/C+ +語言的比較中說法錯誤的是:( D )
A:簡單性Java繼承了 C/C+ +的語法 ,丟棄了其中不常用又容易引起混淆的功能。
B:Java是一種純面向對象的語言 ,具有封裝、繼承 ( Inheritance)和多態( Polymorphism)的特點。
C:Java應用程序可憑借URL打開並訪問網路上的對象。
D:解釋型Java寫成的源代碼需要被編譯成高階的位元組碼 ,它們與機器架構有關。
3. 閱讀下列代碼,選出該代碼段正確的文件名( C )。
class A{
void method1(){
System.out.println("Method1 in class A");
}
}
public class B{
void method2(){
System.out.println("Method2 in class B");
}
public static void main(String[] args){
System.out.println("main() in class B");
}
}
A: A.java B:A.class C: B.java D: B.class
4. 如果一個類的文件名為Student.java,但是類的代碼為:
public class Student {
public static void main(String[] args) {
System.out.println(8>>2);
}}
那麼下列說法正確的是:( B )
A:程序運行結果為8; B:程序運行結果為2;
C:程序運行結果為0; D:程序編譯錯誤,不能運行;
5. 符合對象和類的關系的是( D )。
A:教師和學生 B:書和房子
C:狗和貓 D:飛機和交通工具
6. 關於垃圾回收機制描述不正確的是( B )
A:垃圾回收機制不須通過程序員調用相應方法,也能自動啟動。
B:java程序員用System.gc()方法一定能進行垃圾回收;
C:垃圾回收機制屬於jvm自動操作,java程序員可以不進行垃圾回收操作。
D:垃圾回收機制並不是由操作系統自動執行。
7. 編譯下面源程序會得到哪些文件( D )?
class A1{
}
class A2 exdends A1{
}
public class B{
public static void main(String[] args){
}
}
A: 只有B.class文件 B:只有A1.class和A2.class文件
C: 編譯不成功 D:A1.class、A2.class和B.class文件
8. 下列關於基本數據類型的說法中,不正確的一項是( D )。
(A)boolean類型變數的值只能取真或假
(B)float是帶符號的32位浮點數
(C)double是帶符號的64位浮點數
(D)char是8位Unicode字元
9. 下列(D )是合法的標識符?
A:12class B:void C:-5 D:_blank
10. 在編寫Java程序時,如果不為類的成員變數定義初始值,Java會給出它們的默認值,下列說法中不正確的一個是( D )。
A:byte的默認值是0 B:boolean的默認值是false
C: char類型的默認值是』\0』 D: long類型的默認值是0.0L
11. 下列程序執行的結果是:( B )
public class News {
public static void main(String[] args) {
System.out.println(1+2+ "aa"+3);
}}
A: "12aa3" B: "3aa3 " C: "12aa" D: "aa3"
12. 表達式(12==0) && (1/0 < 1)的值為( B )。
A: true B: false C: 0 D: 運行時拋出異常
13. 下列循環體執行的次數是( C )。
int y=2, x=4;
while(--x != x/y){ }
A : 1 B: 2 C : 3 D : 4
14. 已知如下代碼:
switch(m){
case 0: System.out.println("Condition 0");
case 1: System.out.println("Condition 1");
case 2: System.out.println("Condition 2");
case 3: System.out.println("Condition 3");break;
default:System.out.println("Other Condition");
}
當m的值為( D )時,輸出「Condition 3」
(A)2 (B)0、1 (C)0、1、2 (D)0、1、2、3
15. 下列語句輸出的結果是:( C )
public class X3 {
public static void main(String[] args) {
for(int i=0; i<10; i++){
if(i==5) break;
System.out.print(i);
}
}
}
A:編譯錯誤; B:1234;C:01234;D:12345;
16. 下列語句輸出的結果是:( D )
public class Lx1 {
public static void main(String[] args) {
for(int i=0;i<5;i++){
switch(i){
case 0:System.out.print("B");
case 1:System.out.print("e");break;
case 2:System.out.print("g");
case 3:System.out.print("!");break;
case 4:System.out.print("!");break;
default:System.out.print("!");
}
}
}
}
A:Beg!!! B:Beeg! C:Beg! D:Beeg!!!
17. 下面foreach循環的程序輸出結果是( D )。
public class Lx1{
public static void main(String[] args) {
String s1[]={"歡迎您","3","G","同","學",};
Arrays.sort(s1);
for(String s0:s1)
System.out.print (s0);
}
}
A:歡迎您3G同學 B:3G歡迎您同學 C:同學歡迎您3G D:3G同學歡迎您
18. 閱讀以下程序,選擇正確的運行結果:( B )
public class Lx1 {
public static void main(String[] args) {
byte d[]="YOUIHE你我他".getBytes();
String s=new String(d,6,2);
System.out.println(s);
}
}
A:HE; B:你; C:我; D:他
19. 設有下列數組定義語句:
int a[][]= {{1, 2}, {3}};
則對此語句的敘述正確的是( D )。
A: 定義了一個名為a的一維數組 B: a數組 a[1][1]為0
C: a數組元素的下標為1~3 D: 數組中每個元素的類型都是整數
20. 下列程序輸出的結果是:( B )
public class Lx1 {
public static void main(String[] args) {
String a[][] ={{"","","",""},{""},{"",""}};
System.out.println(a[2].length);
}
}
A:1 B:2 C:3 D:4
21. 關於以下程序的說明,正確的是( C )
1. class StaticStuff
2. {
3. static int x=10;
4. static { x+=5;}
5. public static void main(String args[ ])
6. {
7. System.out.println(「x=」 + x);
8. }
9. static { x/=3;}
10. }
A、4行與9行不能通過編譯,因為缺少方法名和返回類型
B、9行不能通過編譯,因為只能有一個靜態初始化器
C、編譯通過,執行結果為:x=5
D、編譯通過,執行結果為:x=3
22. 給出下面代碼,關於該程序以下哪個說法是正確的?( C )
public class Person{
static int arr[] = new int[5];
public static void main(String a[]) {
for(int i=0;i
System.out.print(arr[0]);
}
}
A、編譯時將產生錯誤 B、編譯時正確,運行時將產生錯誤 C、輸出零 D、輸出空
23. 下面程序中類ClassDemo中定義了一個靜態變數sum,分析程序段的輸出結果。( C )
class ClassDemo {
public static int sum=1;
public ClassDemo() {
sum = sum + 5;}
}
public class ClassDemoTest{
public static void main(String args[]) {
ClassDemo demo1=new ClassDemo();
ClassDemo demo2=new ClassDemo();
System.out.println(demo1.sum);}
}
A: 0 B: 6 C: 11 D: 2
24. 下面關於方法的說法,不正確的是( C )。
A: Java中的構造方法名必須和類名相同
B: 方法體是對方法的實現,包括變數聲明和合法語句
C: 如果一個類定義了構造方法,也可以用該類的默認構造方法
D: 類的私有方法不能被其他類直接訪問
25. 在Java中下列說法正確的是( C )
A) 一個子類可以有多個父類,一個父類也可以有多個子類
B) 一個子類可以有多個父類,但一個父類只可以有一個子類
C) 一個子類可以有一個父類,但一個父類可以有多個子類
D) 上述說法都不對