Computer Science I
Computer Science I CMPU 101
Popular in Course
Popular in ComputerScienence
CHEM 2311 P1
verified elite notetaker
This 8 page Class Notes was uploaded by Reuben Rohan on Wednesday October 28, 2015. The Class Notes belongs to CMPU 101 at Vassar College taught by Marc Smith in Fall. Since its upload, it has received 13 views. For similar materials see /class/230535/cmpu-101-vassar-college in ComputerScienence at Vassar College.
Reviews for Computer Science I
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 10/28/15
Plan Chapter 3 Chapter 3 Use predefined classes amp objects Using Prede ned Classes amp Objects Chapters 4 and 5 Define new instances of predefined classes CMPU101 ProblemSolving and Abstraction Chapter 6 Marc Smith sect 52 Define new classes Vassar College Spring 2007 Anatomy of Statement to Print to the Screen A Sampling of Prede ned Classes System out println quothelloquot Classes for 0 Reading amp writing Systemout 0 Files amp directories an instance of class Print St ream 0 Graphics amp GUI the receiver println Specific classes with intuitive purposes method applicable to Print St reams 0 Date quothelloquot 0 Time actual parameterargument to println 0 Calendar A First Java Program Filename First java Compile amp run source code in DrJava Compiling produces the file First class If class has a main method run it by typing java First in the Interactions Pane If class has no main method instantiate class and send messages to the object from the Interactions Pane FirstBadjava Program FirstBad class First I void mainstring throws Exceptionl println quothelloquotll public static args System but this code would workjust as well Firstjava Written by Barry Soroka A first Java program class Firs public static void main string args throws Exception System outpr1ntln quothelloquot l Comments are used to separate sections of code and to introduce program author and purpose White space and indentation make your program more readable DrJava Compiling amp Executing 1 Open file First java 2 Compile by clicking on button Compile Allquot gt compiling produces file First class 3 Execute by typing the following in the Interactions Pane java First gt this works since class First has a main method Blankjava A template file Contains the boilerplate skeleton of a program Always compiles Advice Start with code that works compiles and runs Add code for one new feature at a time If there39s a problem then it39s due to the new code Warning The more code you add between compiles the more places you must look to find a problems MemodsfordassPrintstream println parameter String example printin quothelloquot prints its argument amp goes to the next line println parameter none example printin terminates the current line amp goes to the next Imagine an invisible print head capable of moving only left to right on a line or advancing to beginning of next line Blankjava Your name here description of this program lank l Blank i l MemodsfordassPrintstream con nued print parameter String example print quothelloquot prints its argument but doesn39t move to the next line For this method the imaginary print head remains on the same line it was just printing on its position immediately following the last character it printed Signatures amp Overloading signature method name parameter types println String println pri ntln is overloaded because there are two forms differing only in parameters gt it is the name of the method that is overloaded gt two or more methods may share the same name within a class provided they have different signatures Exercise Brainstorm different ways to print out abc using print and println Printing abc Prog0301 ertten by Barry Soroka erte out abc class Prog0301 l Prog0301 l Systemout prlntlnquotabcquot l end class Prog0301 GUI Visible objects Pixel quotpicture elementquot the smallest dot on a computer screen Java s graphics libraries awt amp swing overlap in classes Applet a graphics program which requires a browser Application a standalone graphics program Coordinates o o X This is not the coordinate system we re accustomed to Each color is defined by a triple of numbers specifying intensity of red green and blue RGB Colors Onscreen image is specified by assigning a color to each pixel in the image RGB Red Green Blue color channels a triple Each color channel has an intensity in the range 0255 RGB Color 000 black 255255255 white 16 17 JFrame Color Java sBuilt In Color Name RGB Values black CDIDLBLACK 000 A JFrame is a rectangular graphical object With blue ColorBLUE 0 0255 Width cyan ColorCYAN 0255255 gray ColorGRAY 128 128 128 uppeHeft comer dark gray Color DARKGRAY 54 54 64 lightgmy ColorLIGHTGRAY 192 192 192 C r green ColorGREEN 02550 magma CDIDLMAGENTA 2550255 Java graphics work by placing elements onto JFrames orange ColoLORANGE 955200 that are visible to the user pink ColorPINK 255175175 red ColorRED 25500 Progo302 White ColorWHITE 255255255 yellow ColorYELLoW 2552550 19 MyFrame has a class MyFrame extends JFrame Conta39ner data eld that displays color and quotholdsquot elements public MyFrameO thissetTitlequotOur First JFramequot thissetSize200400 thissetLocation200100 thissetDefaultCloseOperationEXlTONCLOSE Container c thisgetContentPane csetBackgroundColorCYAN end class MyFrame Driver MyFrame f new MyFrame f setVisibletrue Above statements create a new MyFrame object and send it a message to make itself visible MyFrame extends JFrame meaning class MyFrame inherits all the data fields and methods of the JFrame class class MyFrame extends JFrame quotthisquot enables an object to send a public MyFrameo message to itself thissetTitlequotOur First JFramequot thissetSize200400 thissetLocation200100 thissetDefaultCloseOperationEXlTONCLOSE Container c thisgetContentPane csetBackgroundColorCYAN end class MyFrame Prog0303 Geometrical Shapes JFrame frame new JFrame quotRace CarII frame setDefaultC loseOperation JFrame EXIT70N7CLOSE MyPanel mp new MyPanel frame getContentPane addmp frame pack frame setVisibletrue Visible elements are added to JFrame Container One such element is a JPanel class extended by MyPanel an object that provides a surface for drawing painting or displaying images public MyPanelO thissetBackgroundColorWHITE thissetPreferredSizenew Dimension700450 III public void paintComponent Graphics page superpaintComponentpage pagedrawRect100150500150 pagefillOval150250100100 pagefillOval450250100100 Prog0304 Display a JPEG Image class MyPanel extends JPanel n I public MyPanelO thissetBackgroundColorWHITE thissetPreferredSizenew Dimension400300 II n Common geometric objects populate the JPanel drawRectxywidthheight fillRectxywidthheight fillOvalxywidthheight oval bounding box drawOvalxywidthheight setColorColorRED drawLinexlylx2y2 public void paintComponent Graphics page superpaintComponentpage Image image new lmagelconquotbaconjpgquotgetlmage pagedrawlmageimage5050this III end class MyPanel ProgO305 Diagonals class MyPaneI extends JPaneI public void paintComponent Graphics page superpaintComponentpage int width getV dth int height getHeightO pagesetCoIorCoorYELLOV pagedrawLine00widthheight pagedrawLineOheightwidth0
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'