1、Java上机实验报告Java上机实验报告练习21、WORKING WITH NUMERIC VALUESDemoVariables.javapublic class DemoVariables public static void main(String args) int entry = 315; System.out.print(The entry is ); System.out.println(entry); 2、ACCEPTING USER DATADemoVariables2.javaimport java.util.Scanner;public class DemoVariabl
2、es2 public static void main(String args) int entry; Scanner keyBoard = new Scanner(System.in); System.out.print(Enter an integer ); entry = keyBoard.nextInt(); System.out.print(The entry is ); System.out.println(entry); 3、PERFORMING ARITHMETICDemoVariables3.javaimport java.util.Scanner;public class
3、DemoVariables3 public static void main(String args) int entry; int anotherEntry; Scanner keyBoard = new Scanner(System.in); System.out.print(Enter an integer ); entry = keyBoard.nextInt(); System.out.print(Enter another integer ); anotherEntry = keyBoard.nextInt(); System.out.println(entry + plus +
4、anotherEntry + is + (entry + anotherEntry); System.out.println(entry + minus + anotherEntry + is + (entry - anotherEntry); System.out.println(entry + times + anotherEntry + is + (entry * anotherEntry); System.out.println(entry + divided by + anotherEntry + is + (entry / anotherEntry); System.out.pri
5、ntln(The remainder is + (entry % anotherEntry); 练习41、CREATING A STATIC METHOD THAT REQUIRES NO ARGUMENTS AND RETURNS NO VALUESSetUpSite.javapublic class SetUpSite public static void statementOfPhilosophy() System.out.println(Event Handlers Incorporated is); System.out.println(dedicated to making you
6、r event); System.out.println(a most memorable one.); public static void main(String args) statementOfPhilosophy(); 2、CALLING A STATIC METHOD FROM ANOTHER CLASSTestStatement.javapublic class TestStatement public static void main(String args) System.out.println(Calling method from another class); SetU
7、pSite.statementOfPhilosophy(); 3、CREATING A STATIC METHOD THAT ACCEPTS ARGUMENTS AND RETURNS VALUESSetUpSite2.javaimport java.util.Scanner;public class SetUpSite2 public static void statementOfPhilosophy() System.out.println(Event Handlers Incorporated is); System.out.println(dedicated to making you
8、r event); System.out.println(a most memorable one.); public static int calculateAge(int originYear, int currDate) int years; years = currDate - originYear; return years; public static void main(String args) final int FOUNDED_YEAR = 1977; int currentYear; int age; Scanner input = new Scanner(System.i
9、n); System.out.print(Enter the current year as a four-digit number ); currentYear = input.nextInt(); age = calculateAge(FOUNDED_YEAR, currentYear); System.out.println(Founded in + FOUNDED_YEAR); System.out.println(Serving you for + age + years ); 4、CREATING A CLASS THAT CONTAINS INSTANCE FIELDS AND
10、METHODSEventSite.javapublic class EventSite private int siteNumber; public int getSiteNumber() return siteNumber; public void setSiteNumber(int n) siteNumber = n; 5、CREATING A CLASS THAT INSTANTIATES OBJECTS OF ANOTHER CLASSSetUpSite3.javaimport java.util.Scanner;public class SetUpSite3 public stati
11、c void statementOfPhilosophy() System.out.println(Event Handlers Incorporated is); System.out.println(dedicated to making your event); System.out.println(a most memorable one.); public static int calculateAge(int originYear, int currDate) int years; years = currDate - originYear; return years; publi
12、c static void main(String args) statementOfPhilosophy(); final int FOUNDED_YEAR = 1977; int currentYear; int age; EventSite oneSite = new EventSite(); int siteNum; Scanner input = new Scanner(System.in); System.out.print(Enter the current year as a four-digit number ); currentYear = input.nextInt();
13、 System.out.print(Enter the event site number ); siteNum = input.nextInt(); age = calculateAge(FOUNDED_YEAR, currentYear); System.out.println(Founded in + FOUNDED_YEAR); System.out.println(Serving you for + age + years ); oneSite.setSiteNumber(siteNum); System.out.println(The site number is + oneSit
14、e.getSiteNumber(); 6、ADDING A CONSTRUCTOR TO A CLASSEventSite.javapublic class EventSite private int siteNumber; public EventSite() siteNumber = 999; public int getSiteNumber() return siteNumber; public void setSiteNumber(int n) siteNumber = n; TestConstructor.javapublic class TestConstructor public
15、 static void main(String args) EventSite oneSite = new EventSite(); System.out.println(Site number + oneSite.getSiteNumber(); 练习51、DEMONSTRATING SCOPEDemoBlock.javapublic class DemoBlock public static void main(String args) System.out.println(Demonstrating block scope); int x = 1111; System.out.prin
16、tln(In first block x is + x); int y = 2222; System.out.println(In second block x is + x); System.out.println(In second block y is + y); int y = 3333; System.out.println(In third block x is + x); System.out.println(In third block y is + y); demoMethod(); System.out.println(After method x is + x); Sys
17、tem.out.println(After method block y is + y); System.out.println(At the end x is + x); public static void demoMethod() int x = 8888, y = 9999; System.out.println(In demoMethod x is + x); System.out.println(In demoMethod block y is + y); 2、OVERLOADING METHODSpublic class DemoOverload public static vo
18、id overloadDate(int mm) System.out.println(Event date + mm + /1/2011); public static void overloadDate(int mm, int dd) System.out.println(Event date + mm + / + dd + /2011); public static void overloadDate(int mm, int dd, int yy) System.out.println(Event date + mm + / + dd + / + yy); public static vo
19、id main(String args) int month = 6, day = 24, year = 2010; overloadDate(month); overloadDate(month, day); overloadDate(month, day, year); 3、CREATING A CONSTRUCTOR THAT REQUIRES AN ARGUMENTEventSite.javapublic class EventSite private int siteNumber; private double fee; private String name; public Eve
20、ntSite() siteNumber = 999; fee = 0; name = XXX; public EventSite(int siteNum) this(); siteNumber = siteNum; public int getSiteNumber() return siteNumber; public void setSiteNumber(int n) siteNumber = n; public double getFee() return fee; public void setFee(double n) fee = n; public String getName()
21、return name; public void setName(String n) name = n; DemoConstructor.javapublic class DemoConstructor public static void display(EventSite site) System.out.println(nEvent site # + site.getSiteNumber() + nFee is $ + site.getFee() + nName of site is + site.getName(); public static void main(String arg
22、s) EventSite site1 = new EventSite(); EventSite site2 = new EventSite(678); display(site1); display(site2); 4、USING AN EXPLICITLY IMPORTED PREWRITTEN CLASSCalendarDemo.javaimport java.util.*;public class CalendarDemo public static void main(String args) GregorianCalendar now = new GregorianCalendar(
23、); System.out.println(YEAR: + now.get(Calendar.YEAR); System.out.println(MONTH: + now.get(Calendar.MONTH); System.out.println(WEEK_OF_YEAR: + now.get(Calendar.WEEK_OF_YEAR); System.out.println(WEEK_OF_MONTH: + now.get(Calendar.WEEK_OF_MONTH); System.out.println(DATE: + now.get(Calendar.DATE); System
24、.out.println(DAY_OF_MONTH: + now.get(Calendar.DAY_OF_MONTH); System.out.println(DAY_OF_YEAR: + now.get(Calendar.DAY_OF_YEAR); System.out.println(DAY_OF_WEEK: + now.get(Calendar.DAY_OF_WEEK); System.out.println(AM_PM: + now.get(Calendar.AM_PM); System.out.println(HOUR: + now.get(Calendar.HOUR); Syste
25、m.out.println(HOUR_OF_DAY: + now.get(Calendar.HOUR_OF_DAY); System.out.println(MINUTE: + now.get(Calendar.MINUTE); System.out.println(SECOND: + now.get(Calendar.SECOND); System.out.println(MILLISECOND: + now.get(Calendar.MILLISECOND); 5、CREATING AN INTERACTIVE APPLICATION WITH A TIMERDialogTimer.jav
26、aimport javax.swing.JOptionPane;import java.util.*;public class DialogTimer public static void main(String args) int time1, time2, milli1, milli2, sec1, sec2, timeDifference; final int MILLISECSINSECOND = 1000; JOptionPane.showConfirmDialog(null, Is stealing ever justified? ); GregorianCalendar befo
27、re = new GregorianCalendar(); milli1 = before.get(GregorianCalendar.MILLISECOND); sec1 = before.get(GregorianCalendar.SECOND); time1 = MILLISECSINSECOND * sec1 + milli1; GregorianCalendar after = new GregorianCalendar(); milli2 = after.get(GregorianCalendar.MILLISECOND); sec2 = after.get(GregorianCa
28、lendar.SECOND); time2 = MILLISECSINSECOND * sec2 + milli2; timeDifference = time2 - time1; JOptionPane.showMessageDialog(null,It took + timeDifference + milliseconds for you to answer); 练习71、To create the general Event class:Event.javaimport javax.swing.*;public class Event private int eventGuests; public void displayEventGuests() JOptionPane.showMessageDialog(null, Event guests: + eventGuests); public void inputEventGuests() char inChar; String guestsString = new String(); guestsString = JOptionPane.s
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1