




已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、輸入三角形的三條邊a,b,c.如果能構(gòu)成一個三角形,輸出面積area和周長perimeter;否則,輸出These sides do not correspond to a valid triangle.import java.util.Scanner;public class Trangle public static void main(String args) float a,b,c;float s = 0;Scanner input = new Scanner(System.in) ;System.out.println(請輸入三邊邊長:);a = input.nextFloat();b = input.nextFloat();c= input.nextFloat();if (a+bc)&(a+cb&(b+ca) s = (a+b+c)/2;float area = (float) Math.sqrt(s);float l = a+b+c;System.out.println(面積是:+area);System.out.println(周長是:+l);else System.out.println(These sides do not correspond to a valid triangle);請編寫方法int countNum(String str),該方法的功能是統(tǒng)計已知字符串str中數(shù)字的個數(shù)。例如,countNum(A42B83C2D)的返回值是5。public class Count public static void main(String args) Count count= new Count();System.out.println(count.countNum(777);int countNum(String str)char b = str.toCharArray();int s = 0;for (int i = 0; i b.length; i+) if (bi=9&0gz*12) System.out.println(fare-gz*12);return fare-gz*12;else System.out.println(gz*12-fare);return gz*12-fare ;假設(shè)有下列事實:所有的動物都能吃,但人除了吃外,還能奔跑和游泳,而狗除了吃外,只會奔跑,卻不會游泳,試利用抽象類和接口編寫一個程序,分別演示人與狗所具有的能力。package animal;public abstract class Animal implements Eneat,Enrun,EnswimString name;public void eat()System.out.println(name+可以吃);public void swim()System.out.println(name+可以游泳);public void run()System.out.println(name+可以跑);public static void main(String args) Person person = new Person(person);Dog dog = new Dog(dog);dog.Dogable();person.Personable();class Person extends Animal String name;public Person(String name) = name;public void Personable() super.eat();super.swim();super.run();class Dog extends Animal String name;public Dog(String name) = name;public void Dogable() super.eat();super.run();interface Eneatvoid eat(); interface Enswimvoid swim(); interface Enrunvoid run(); 編寫Complex類并測試其功能:(1).復(fù)數(shù)包括實部(Real)和虛部(Imaginary)兩部分;(2).定義構(gòu)造函數(shù),在構(gòu)造對象的同時為對象賦值;(3).提供add方法計算兩個復(fù)數(shù)的和;(4).重載toString,使得Complex類的對象能夠顯示自身信息;(5).并用main方法測試程序。import java.util.Scanner;public class Complex int r,i;public Complex(int r, int i) this.r = r;this.i = i;public String toString() System.out.println(Complex + r + + + i + i);return Complex r= + r + , i= + i + ;public String add() Scanner input = new Scanner(System.in);System.out.println(請輸入第一個復(fù)數(shù)的實部和虛部);int r1 = input.nextInt();int i1 = input.nextInt();System.out.println(請輸入第er個復(fù)數(shù)的實部和虛部);int r2 = input.nextInt();int i2 = input.nextInt();r = r1+r2;i = i1+i2;return toString();public static void main(String args) Complex complex = new Complex(2, 3); complex.toString(); complex.add();編寫一個程序,在當(dāng)前目錄下創(chuàng)建一個子目錄test,在這個目錄下創(chuàng)建一個文件,并把這個文件設(shè)置成只讀。import java.io.*;public class FileManager public static void main(String args) throws IOException String filename = test1;File myfile = new File(filename);if (myfile.exists()&myfile.isDirectory() System.out.println(filename+已存在);return;else myfile.mkdir();System.out.println(myfile.getAbsolutePath()+創(chuàng)建成功);File ds = new File(myfile,text.txt);ds.createNewFile();ds.setReadOnly();System.out.println(ds.getAbsolutePath()+創(chuàng)建成功);return;3、編寫一個程序,實現(xiàn)復(fù)制文件test.txt中所有內(nèi)容到test1.txt中。import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class CopyFile public static void main(String args) String file1,file2;int ch = 0;file1 = E:javafilejavalianximytesttest1text.txt;file2 = test3.txt;try FileInputStream fis = new FileInputStream(file1);FileOutputStream fos = new FileOutputStream(file2);int size = fis.available();while (ch=fis.read()!=-1) System.out.write(ch);fos.write(ch);System.out.println(copy success);fis.close();fos.close(); catch (FileNotFoundException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();3、從網(wǎng)站獲取該網(wǎng)站首頁的HTML文本。package second;importjava.io.BufferedInputStream;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;.MalformedURLException;import .URL;public class OpenURL public String getContent (String strURL)try URL url = new URL(strURL);InputStream stl = url.openStream(); /創(chuàng)建輸入流對象InputStreamReader inputStreamReader = new InputStreamReader(stl,utf-8);BufferedReader bufferedInputStream = new BufferedReader(inputStreamReader);String s = ;StringBuffer sb = new StringBuffer();while (s = bufferedInputStream.readLine()!=null) /從獲得信息sb.append(s+rn);bufferedInputStream.close();returnsb.toString(); catch (MalformedURLException e) / TODO Auto-generated catch blockreturn 是錯誤的+strURL; catch (IOException e) / TODO Auto-generated catch blockreturne.getMessage();public static void main(String args) OpenURL ou = new OpenURL();System.out.println(ou.getContent(/main.html);編寫一個Java程序?qū)?00,101,102,103,104,105這5個數(shù)以數(shù)組的形式寫入到當(dāng)前目錄的Dest.txt文件中,并以相反的順序讀出顯示在屏幕上。import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class Testpublic static void main(String args)int data = 100, 101, 102, 103, 104, 105 ;int t;String content=;tryDataOutputStream out = new DataOutputStream(new FileOutputStream(C:Dest.txt);for (int i = 0; i data.length; i+)out.writeInt(datai);out.close();DataInputStream in = new DataInputStream(new FileInputStream(C:Dest.txt);for (int i = 0; i data.length; i+)t = in.readInt();content=t+ +content;System.out.println(content);in.close();catch (IOException e)System.out.println(e.getMessage();、實現(xiàn)程序:用字符流實現(xiàn)不斷從鍵盤輸入數(shù)據(jù),并顯示在屏幕上,直到用戶輸入e退出程序。import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年路政執(zhí)法證考試題及答案
- 2025春國開《法制思想概論》形考作業(yè)1-4答案
- 河南省駐馬店市2024-2025學(xué)年高二上學(xué)期期末語文試題(原卷版+解析版)
- 2025-2030年中國自耦減壓起動器數(shù)據(jù)監(jiān)測研究報告
- 2025-2030年中國半高領(lǐng)印花套衫數(shù)據(jù)監(jiān)測研究報告
- 2025年幼兒大班詩歌語言標(biāo)準教案
- 部編版一年級下冊語文 語文要素專項卷之18. 看圖寫話(含答案)
- Unit 10 You're supposed to shake hands. Section B(1a-1d)教案 人教版英語九年級全冊
- 信息化管理咨詢服務(wù)合同
- 收購法律服務(wù)合同
- 上海市疾控中心招聘信息考試試題及答案
- 消防安全技術(shù)綜合能力要點概述
- 2024年河南交通職業(yè)技術(shù)學(xué)院單招職業(yè)技能測試題庫及答案解析
- DL-T 5148-2021水工建筑物水泥灌漿施工技術(shù)條件-PDF解密
- 第8版精神病學(xué)
- 旅游經(jīng)濟發(fā)展背景下廣西民宿客棧的發(fā)展現(xiàn)狀、問題和對策分析-基于桂林市調(diào)查分析研究 旅游管理專業(yè)
- 道路施工安全隱患及防范措施
- 新生兒魚鱗病個案護理
- 生物質(zhì)燃料的資源開發(fā)與利用
- 《積極心理學(xué)》課件
- 食管胃底靜脈曲張出血的診治指南解讀
評論
0/150
提交評論