link to intellimation

Intellimation - Your Gateway to Intelligent Information
DD2 Java Examples



Art
Birds
Butterflies
Books
Cats
China
Countries
Cosmetology
Education
English Studies
Flight & Space
France
Gifts
Health

Hebrew
Historical Events
Holocaust
Iraq
Ireland

Israel

Italy
Japan
Japanese
>>>Java
Jewish, Judaism
Law
Love Lyrics
Love Poems
Musicology
Nature
New York
People
Photography
Professions
Spain
Weather


(1) The Valentine Shop:

link to Valentine Shop #1

(2) The Rainbow Valentine Shop:

Click Here for Rainbow Valenine Shop

Link to the Irish Shop

Search With Google

go shopping

Advertise on our site - learn how to
Contact us at iidesune@netvision.net.il
(ExY1)
DD1
   EM1    Y1   Y1b    EM-Phonebook  
DD2    EM2     Y2   Yv_Examples
DD3    EM3     Y3    
DD4    EM4     Y4
DD5    EM5     Y5

****  Index of Examples
****  Back to Java Cheat

//dir: homework

//ASSIGNMENT

Implement class Rectangle and class Point.
Class rectangle will hold reference to two points.
Class Point will contain two int (x and y).
Skeleton of class rectangle:

Class Rectangle
{
    Point p1, p2;
    void move(int x, int y);
    void print()
    void resize(int nNewWidth, int nNewHeight);
    void resize(float nPrecentage);
}


Skeleton of class Point
Class Point
{
    int x, y
void move(int x, int y)
    void print();
}


// hw/main.java

import java.util.*;
class Main
{
    public static void main(String args[])
    {
        Rectangle r1 = new Rectangle(new Point(10, 10), new Point(20, 20));
        Rectangle r2 = new Rectangle(10, 10, 10, 10);
       
        LinkedList l = new LinkedList();
        Iterator iter = l.iterator();

        System.out.println("r1 = ");
        r1.print();
        System.out.println("r2 = ");
        r2.print();

        r1.move(10, 10);
        r2.resize(200f);

        System.out.println("r1 after move(10) = ");
        r1.print();
        System.out.println("r2 after resize 200f = ");
        r2.print();

        System.out.println("Total point instantiated = " + Point.getNumOfPointInstantiated());
    }
};

Point
//hw/Point.java

class Point
{
    private int m_x, m_y;
    private static int counter;
    public Point(int x, int y)
    {
        counter++;
        m_x = x; m_y = y;
    }

    public static int getNumOfPointInstantiated()
    {
        return counter;
    }

    public int getX()
    {
        return m_x;
    }
    public int getY()
    {
        return m_y;
    }

    public void print()
    {
        System.out.println("(" + m_x + ", " + m_y + ")");
    }

    public boolean set(int x, int y)
    {
        if ((x < 0) || (y < 0))
            return false;
        m_x = x;
        m_y = y;
        return true;
    }

    public boolean move(int xBy, int yBy)
    {
        return set(m_x + xBy, m_y + yBy);
    }
};




Rectangle
// hw/Rectangle.java

class Rectangle
{
    private Point m_pTopLeft, m_pBottomRight;

    public Rectangle(Point pTopLeft, Point pBottomRight)
    {
        m_pTopLeft = pTopLeft;
        m_pBottomRight = pBottomRight;
    }

    public Rectangle(int x, int y, int Width, int Height)
    {
        this(new Point(x, y), new Point(x + Width, y + Height));
        /*
        m_pTopLeft = new Point(x, y);
        m_pBottomRight = new Point(x + Width, y + Height);
        */
    }

    public void print()
    {
        System.out.println("This is a rectangle, Point info");
        m_pTopLeft.print();
        m_pBottomRight.print();
    }

    public void move(int xBy, int yBy)
    {
        m_pTopLeft.move(xBy, yBy);
        m_pBottomRight.move(xBy, yBy);
    }

    public void resize(int Width, int Height)
    {
        m_pBottomRight.set(m_pTopLeft.getX() + Width, m_pTopLeft.getY() + Height);
    }

    public int getWidth()
    {
        return m_pBottomRight.getX() - m_pTopLeft.getX();
    }

    public int getHeight()
    {
        return m_pBottomRight.getY() - m_pTopLeft.getY();
    }

    public void resize(float ratio)
    {
        float NewWidth = getWidth() * ratio / 100f;
        float NewHeight = getHeight() * ratio / 100f;
        resize((int)NewWidth, (int)NewHeight);
    }
};





dir //hw/inheritance/
//hw/inheritance/Person.java

class Person //extends Object
{
    String m_sName;

    Person(String sName)
    {
        super();
        m_sName = sName;
    }

    public boolean equals(Person p)
    {
            return m_sName.equals(p.m_sName);
    }

    public String toString()
    {
        return "This is a person, name = " + m_sName;
    }

    public int hashCode()
    {
        return m_sName.hashCode();
    }
    public boolean equals(Object other)
    {
        //Observe the argument as Person
        //other can point to chair
        if (other.getClass() == this.getClass())
        {
            Person p = (Person)other;
            //Check if name == name
            return equals(p);
        }
        else
            return false;
    }

    public static void main(String args[])
    {
        Person p1 = new Person("Person 1");
        System.out.println(p1);
    }
};

super
//hw/inheritance/Employee.java
class Employee extends Person
{
    int m_nSalary;
    Employee(String sName, int nSalary)
    {
        super(sName.toUpperCase());
        m_nSalary = nSalary;   
    }

    public boolean equals(Object other)
    {
        if (other.getClass() != this.getClass())
            return false;
        Employee e = (Employee) other;
        return ((m_sName.equals(e.m_sName)) &&
                (m_nSalary == e.m_nSalary));
    }
};

dir day2\packages\com\etc.
packages.com.ydc.youtools.app
Main.java
package com.ydc.youtools.app;
//2. import com.ydc.youtools.io.File / import com.ydc.youtools.io.*
class Main
{
    public static void main(String args[])
    {
        System.out.println("In main, creating file");
        new com.ydc.youtools.io.File();
        //2. new File()
    }

};

dir this
class ThermAnalyzer
{
    ThermAnalyzer(int nNumOfThems)
    {
        for (int i = 0 ; i < nNumOfThems ; ++i)
        {
            new Thermometer(this);
        }
    }

    void tempChanged(int nNewTemperature)
    {
        System.out.println("In Analyzer, new Temp = " + nNewTemperature);
    }

    public static void main(String args[])
    {
        new ThermAnalyzer(5);
    }
};


class Thermometer
{
    ThermAnalyzer m_Analyzer;
    Thermometer(ThermAnalyzer Analyzer)
    {
        m_Analyzer = Analyzer;
        temperatureChanged((int)(Math.random() * 1000));
    }

    public void temperatureChanged(int nNewTemperature)
    {
        m_Analyzer.tempChanged(nNewTemperature);
    }

};


class Ex1
{
    private int x;
    public void set(int x)
    {
//        this.m_x = x;
//        m_x = x;
        this.x = x;
    }
};

LinkedList & ListIterator
import java.util.*;
import java.io.*;

public class itersInputedList
{
public static void main (String[] args) throws IOException
{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
LinkedList mylist = new LinkedList();
ListIterator iter;

System.out.println("enter strings for list, ^D to end");
String line = stdin.readLine();
while (line != null)
{    mylist.addLast(line);
    line = stdin.readLine();
}

iter = mylist.listIterator();
while (iter.hasNext())
{    System.out.println(((String) iter.next()).toUpperCase());
}
System.out.println();
// iter is at the tail

iter = mylist.listIterator();
String word = (String) iter.next();

while (word != null && iter.hasNext() && !word.equals("something"))
    word = (String) iter.next();

iter.remove();    // delete & return the object most recently passed

System.out.println("after deleting \"something\" or the last word");
iter = mylist.listIterator();
while (iter.hasNext())
{    System.out.println(((String) iter.next()).toUpperCase());
}
System.out.println();

iter = mylist.listIterator();
while (iter.hasNext() && ! ((String) iter.next()).equals("add after"))
    ;    // empty loop on purpose because next advances

iter.add("new word"); // add after most recently passed item and move past new one

while (iter.hasNext()) iter.next();    // get to tail
System.out.println("list in reverse after adding ");
while (iter.hasPrevious())
    System.out.println(iter.previous());
System.out.println();
}
}


MyLinkedListEx
import java.util.LinkedList;
import java.io.*;

class MyLinkedListEx
    {
  public static void main(String[] args)
    {                    LinkedList ll = new LinkedList();

                    System.out.println("Adding 3 names to a linked list");
                    ll.add("Gita");
                    ll.add("Brigitte");
                    ll.add("Brenda");
                    for (int i = 0; i < ll.size(); i++) {
                          String s = (String)ll.get(i);//Down-cast to String
                          System.out.println(s + " ");
                    }
    System.out.println("");
    }
}


MyMouseListener implements MouseListener
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
class MyMouseListener implements MouseListener
{
    Ex2 m_Applet;
    MyMouseListener(Ex2 app)
    {
        m_Applet = app;
    }
    public void mouseClicked(MouseEvent e)
    {
        System.out.println("mouseClicked");
        m_Applet.incValue();

    }
    public void mouseEntered(MouseEvent e)
    {
        System.out.println("mouseEntered");
    }
    public void mouseExited(MouseEvent e)
    {
        System.out.println("mouseExited");
    }
    public void mousePressed(MouseEvent e)
    {
        System.out.println("mousePressed");
    }
    public void mouseReleased(MouseEvent e)
    {
        System.out.println("mouseReleased");
    }          
};

Another Classic MouseListener from the Internet

import java.awt.*;
import java.applet.*;
// import an extra class for the MouseListener
import java.awt.event.*;

// Tells the applet you will be using the MouseListener methods.

public class MouseClickExample extends Applet implements MouseListener
{
 // The X-coordinate and Y-coordinate of the last click.
 int xpos;
 int ypos;

 // The coordinates of the rectangle we will draw.
 // It is easier to specify this here so that we can later
 // use it to see if the mouse is in that area.
 int rect1xco,rect1yco,rect1width,rect1height;

 // The variable that will tell whether or not the mouse
 // is in the applet area.
 boolean mouseEntered;

 // variable that will be true when the user clicked i the rectangle 
 // the we will draw.
 boolean rect1Clicked;

 public void init() 
 {
  // Assign values to the rectanagle coordinates.
  rect1xco = 20;
  rect1yco = 20;
  rect1width = 100;
  rect1height = 50;

  // Add the MouseListener to your applet
  addMouseListener(this);
 }

 public void paint(Graphics g) 
 {
  // Rectangle's color
  g.setColor(Color.green);

  g.fillRect(rect1xco,rect1yco,rect1width,rect1height);

  g.setColor(Color.red);

  // When the user clicks this will show the coordinates of the click
  // at the place of the click.
  g.drawString("("+xpos+","+ypos+")",xpos,ypos);

  // If the click was in the rectangle show this message
  if (rect1Clicked) g.drawString("You clicked in the Rectangle",20,120);
  // else this one
  else g.drawString("You clicked outside of the rectangle",20,120);

  if (mouseEntered) g.drawString("Mouse is in the applet area",20,160);
  else g.drawString("Mouse is outside the Applet area",20,160);
 }

/* These methods always have to present when you implement MouseListener

 public void mouseClicked (MouseEvent me) {}
 public void mouseEntered (MouseEvent me) {}
 public void mousePressed (MouseEvent me) {}
 public void mouseReleased (MouseEvent me) {} 
 public void mouseExited (MouseEvent me) {} 
*/

 // This method will be called when the mouse has been clicked.
 public void mouseClicked (MouseEvent me) {

  // Save the coordinates of the click lke this.
  xpos = me.getX();
  ypos = me.getY();

  // Check if the click was inside the rectangle area.
  if (xpos > rect1xco && xpos < rect1xco+rect1width && ypos >rect1yco && 
    ypos < rect1yco+rect1height)  rect1Clicked = true;
  // if it was not then rect1Clicked is false;
  else 
   rect1Clicked = false;
  //show the results of the click
  repaint();

 }

 // This is called when the mous has been pressed
 public void mousePressed (MouseEvent me) {}

 // When it has been released
 // not that a click also calls these Mouse-Pressed and Released.
 // since they are empty nothing hapens here.
 public void mouseReleased (MouseEvent me) {} 

 // This is executed when the mouse enters the applet. it will only
 // be executed again when the mouse has left and then re-entered.
 public void mouseEntered (MouseEvent me) {
  // Will draw the "inside applet message"
  mouseEntered = true;
  repaint();
 }

 // When the Mouse leaves the applet.
 public void mouseExited (MouseEvent me) {
  // will draw the "outside applet message"
  mouseEntered = false;
  repaint();
 } 

/* So now you can use the MouseListener instead of Buttons. These methods will be ones that you will
often use. These methods are good for mouseClicks, but when you need mouseOvers like in Javascript
then you'll need the MouseMotionListener.
Go to MouseMotionExample.java
*/



MouseListener Ex44 extends Applet implements MouseListener
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Ex44 extends Applet implements MouseListener
{
    int nCurrentValue = 1111;
    boolean mouseClicked;
    boolean mouseReleased;
    public void init()
   
    {
        //Register Listener
        //In the documentation:
        //Applet.addMouseListener(MouseListener)
        //this.addMouseListener(new MyInnerMouseListener());
         this.addMouseListener(this);

       
    }
    Font f = new Font("TimesRoman", Font.BOLD, 10);

    void incValue()
    {
        ++nCurrentValue;
        //Paint();
    }
    public void paint(Graphics g)
    {
        g.setFont(f);
        g.setColor(Color.red);

        //g.drawString("in paint .... why isn't this working" + nCurrentValue, 100, 100);
        //g.drawString(" " + nCurrentValue, 100, 100);
        //for (int ix=1;ix <100 ;ix++ )
   
        g.drawString("IIIIIIIIII ", 1, 100);
        if (mouseClicked)
        {
            g.drawString("mouseClicked" +nCurrentValue, 1,150);
        }
        if (mouseReleased)
        {
            g.drawString("mouseReleased" +nCurrentValue, 1,200);
        }
       

    //    g.drawString("In public void ex3 ", 100, 100);
    }
/*    public void repaint(Graphics g)
    {
    g.setFont(f);
    g.drawString("in repaint" + nCurrentValue, 100, 100);
    g.drawString("In public void ex3 ", 100, 100);
    }*/

    /* These methods always have to present when you implement MouseListener

 public void mouseClicked (MouseEvent me) {}
 public void mouseEntered (MouseEvent me) {}
 public void mousePressed (MouseEvent me) {}
 public void mouseReleased (MouseEvent me) {} 
 public void mouseExited (MouseEvent me) {} 
*/
   
        public void mouseClicked(MouseEvent e)
        {
           
            incValue();
            mouseClicked = true;
            repaint();
       
       


        }
        public void mouseEntered(MouseEvent e)
        {
            System.out.println("mouseEntered");
        }
        public void mouseExited(MouseEvent e)
        {
            System.out.println("mouseExited");
        }
        public void mousePressed(MouseEvent e)
        {
            System.out.println("mousePressed");
        }
        public void mouseReleased(MouseEvent e)
        {
            System.out.println("mouseReleased");
            incValue();
            mouseReleased = true;
            repaint();
        }          
    }




























ASDF

Warning and Disclaimer

We try here to provide simple, non-professional advice and links to sites that seem to be trustworthy for more details.
This is no substitute for qualified medical advice.

Always consult a qualified medical practioner to confirm any self diagnosis and rule out other causes. Always consult a qualified medical practioner regarding medical problems or questions.

This site takes no responsibility for any loss or claim arising from the use or misuse of information/"advice"  on the site or information or advice on linked sites, nor from the failure of any reader to seek or take medical, psychological, legal or any other professionally qualified advice. This warning/disclaimer applies to every page of this website, whether it appears there or not.