Wednesday, June 26, 2013

Graphics Examples

package com.spconger.GraphicsExamples;

import acm.graphics.*;
import acm.program.*;
import acm.io.*;

import java.awt.Color;
import java.awt.color.*;

@SuppressWarnings("serial")
public class Examples extends GraphicsProgram {
 /**This class will provide some
  * examples of graphics in acm
  * 6/26/2013
  */
 public void run(){
  //hello();
  println(getParent().getParent().getWidth());
  getParent().getParent().setBounds(400, 500,500,400);
  //setSize(400,500);
  //makeRectangle();
  createOval();
 }
 
 private void hello(){
  IODialog dialog = new IODialog();
  String name = dialog.readLine("Enter your name");
  GLabel label = new GLabel("hello, " + name,50, 20);
  label.setColor(Color.MAGENTA);
  add(label);
  GLabel label2 = new GLabel("the color  of label 1 is "
    + label.getColor(), 20, 100);
  add(label2);
 }
 
 private void makeRectangle(){
  GRect rect = new GRect(40,50, 60, 30);
  rect.setFillColor(Color.PINK);
  rect.setColor(Color.PINK);
  rect.setFilled(true);
  add(rect);
  GRect rect2 = new GRect(50,60, 60, 30);
  rect2.setFillColor(Color.BLUE);
  rect2.setFilled(true);
  add(rect2);
 }
 
 private void createOval(){
  GOval oval = new GOval(50, 60, 120,120);
  oval.setFillColor(Color.GREEN);
  oval.setFilled(true);
  add(oval);
  //take half the value of each height and width
  //and add it to the x y
  GOval oval2=new GOval(80,90,60,60);
  oval2.setFillColor(Color.BLUE);
  oval2.setFilled(true);
  add(oval2);
 }
 
}

Here is the infinite loop with the turtle

package com.spconger.TurtleOne;
import acm.graphics.*;
import acm.program.*;

public class Program extends GraphicsProgram{
 
 public void run(){
  setSize(500,600);
  turtleRun();
  
 }
 
 private void turtleRun(){
  GTurtle portia = new GTurtle(25,50);
  add(portia);
  waitForClick();
  
  while (true){
   if(portia.getX() >= getWidth()){
    portia.setDirection(180);
   }
   else if (portia.getX() <= 0){
    portia.setDirection(0);
   }
   portia.forward(20);
   
  }
 }
 
}

Here is the bouncing ball

package com.spconger.MovingGraphics;
import java.awt.Color;

import acm.graphics.*;
import acm.graphics.GOval;
import acm.program.*;

public class BouncingBall extends GraphicsProgram{
 // it is a good idea to set constants for any numbers
 private final int FORMWIDTH=400;
 private final int FORMHEIGHT=600;
 private final int BALLDIAMETER=30;
 
 
 public void run(){
  setSize(FORMWIDTH, FORMHEIGHT);
  movingBall();
  
 }
 
 private void movingBall(){
  //this places the ball in the center of the screen
  GOval ball = new GOval(FORMWIDTH/2-BALLDIAMETER/2,
    FORMHEIGHT/2-BALLDIAMETER/2,
    BALLDIAMETER,BALLDIAMETER);
  ball.setFillColor(Color.RED);
  ball.setFilled(true);
  add(ball);
  waitForClick();
  int posX = -15;
  int posY=10;
    
  while(true){
   //this makes it so when the ball hits the edges
   //it reverse direction
   if(ball.getX()>=FORMWIDTH-BALLDIAMETER || ball.getX() <=0){
    posX=-posX;
   }
   //same for top and bottom
   if (ball.getY()>=FORMHEIGHT-BALLDIAMETER || ball.getY() <=0){
    posY = -posY;
   }
   
   ball.move(posX, posY);
   //slight pause betwee positions
   pause(50);
  }
 }
 
 

}

Monday, June 24, 2013

For loop with 5 columns

package com.spconger.Quadratic;

import acm.program.*;

public class Primes extends ConsoleProgram {

 private final int MAXVALUES=41;
 private final int NUMBEROFCOLUMNS=5;
 
 public void run() {
  setSize(500,300);
  for (int i = 1; i <= MAXVALUES; i++) {
   int prime = i * i - i + MAXVALUES;
   if (i % NUMBEROFCOLUMNS != 0) {
    print(prime + "\t");
   } else {
    print(prime + "\n");
   }
  }
 }
}

Sunday, June 2, 2013

Integration Services

Overview

Assume that the Automart Web site records new registered customers into an xml file and each day they need to import the new customers into the database. (Unlikely for a company but not unusual for a non-profit or home run operation.)

We are going to make an integration Services package in SQL Server so that we can import the data automatically each day.

We must take the data from the XML file and import it into three tables, PERSON, CUSTOMER.VEHICLE, and CUSTOMER.REGISTEREDUSTOMER.

Here is the XML file and its Schema.


newcustomer.xml

<?xml version="1.0" encoding="utf-8"?>
<newcustomer>
  <customer>
    <firstname>Sally</firstname>
    <lastname>King</lastname>
    <email>sking@msn.com</email>
    <password>royalpass</password>
    <license>FMN2033</license>
    <make>Toyota corolla</make>
    <year>2011</year>
  </customer>
  <customer>
    <firstname>Edwin</firstname>
    <lastname>Laurence</lastname>
    <email>elaurence@gmail.com</email>
    <password>edpass</password>
    <license>WEB3445</license>
    <make>Volkswagen Beetle</make>
    <year>2012</year>
  </customer>
  <customer>
    <firstname>Sharon</firstname>
    <lastname>Malin</lastname>
    <email>sandm@gmail.com</email>
    <password>smpass</password>
    <license>TEN6545</license>
    <make>Nisson Leaf</make>
    <year>2013</year>
  </customer>
  <customer>
    <firstname>Bob</firstname>
    <lastname>Brown</lastname>
    <email>bbrown@gmail.com</email>
    <password>bbpass</password>
    <license>LTE0034</license>
    <make>Ford Mustang</make>
    <year>2002</year>
  </customer>
  <customer>
    <firstname>Melissa</firstname>
    <lastname>Bee</lastname>
    <email>melissa@gmail.com</email>
    <password>beepass</password>
    <license>MLN9007</license>
    <make>Kia Rio</make>
    <year>2010</year>
  </customer>
</newcustomer>

Copy this file into Notepad. Choose SAVE AS. Set the type to All Files(*.*) and change the encoding to UTF-8

Here is the schema for the above XML

newcustomer.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="newcustomer">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="customer">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="firstname" type="xs:string" />
              <xs:element name="lastname" type="xs:string" />
              <xs:element name="email" type="xs:string" />
              <xs:element name="password" type="xs:string" />
              <xs:element name="license" type="xs:string" />
              <xs:element name="make" type="xs:string" />
              <xs:element name="year" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Save this as newcustomer.xsd with the UTF-8 encoding


Creating the Integration Package

1. Open SQL Services Data Tools as Administrator. If you do not open it as administrator it will fail when you try to run it.
2. Start a new Project
3. Choose Integration Services Project
4. Name it AutomartNewCustomer

5. In the Designer's Control Flow tab add a new DataFlow Task and name it "Get Person Task"

6. Double click the DataFlow task to open the Data Flow tab
7. Add a new XML Source control. Rename it "Person Source" and double click it to set the file and schema properties

8.Click on COLUMNS, click through the warnings and view the columns

9. Click OK
10. Now we will add a Data Conversion control. This control lets us change the properties of the data to make it conform more with the database
11. Connect the arrow from the source to the Conversion

12. Double Click the Data Conversion control to open the properties dialog.
13. For now you can just select firstname, lastName

14. Click ok.
15. Now we will add a Destination control
16. Select a SQL Server Destination name it "Person Destination" and connect it to the Data Conversion Control

17. Double Click the Person Destination control to open the properties
18. First we add a new Connection manager and a new connection. We connect to the server and the automart database

19. Then we select the person table

20. Next we go to the table mappings and match our columns. Use the copy of the xml fields for the match

21. Return to the Control Flow tab
22. Add another Data Task control and name it "Get Vehicle Task".
23. Hook it to the Person Task Control

24. Double Click the data flow task to get the data flow designer
25. We will start with an xml source. It is configured exactly the same as the last one.
26. Next we will add another Data configuration Control and configure the fields as in the picture below

27. It might also be a good idea to turn off the error controls

28. Now we are going to add a lookup control. We need to look up the person keys from our previous inserts

29. In the connection tab of the dialog select the Person table

30. Now in the mapping tab. Drag firstname to firstname and lastname to lastname and check the PersonKey box. this will enable the program to look up the person key that matches the first and last names

31. Next add a SQL Server Destination. When you connect it to the lookup this dialog will pop up

32. Select "Lookup Match Output" and OK
33. In the connection manager use the same manager and choose the table Vehicle
34. then go to mappings and set the mappings as in the picture below

35. No go back to the Control Flow Tab
36. We are going to add one more Data Flow task to populate the RegisteredCustomer table

37. Double click it
38. We will need an xml source configured just like the other two.
39. We will need a Data Conversion object in which we select email and password

40. And we will also need a lookup control configured exactly like the last one.
41. Finally we will need a SQL Server Destination Control, using the Customer.RegisteredCustomer table.

Running and Debugging

With luck now we can run the package. Just click the Green triangle to start the package. If all goes well the controls should turn green

If they fail. Check in the Data Flow for where the error occurred. Try turning off all the errors in the various controls. Make sure you are running as admin.