1,076,459 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Posts by jinglylime

Thanks guys, that so much simpler.

jinglylime
Newbie Poster
18 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi guys,
I wrote some code for finding the three longest words in a string. The code I wrote seem too complicated, is there a better way to appoarch this?

public class Size {
    public String[] threeLongest(String word){
        String[] three = new String[3];
        String[] words = word.split(" ");
        three[0] = longest(word);
        three[1] = find(words, three[0]);
        three[2] = find(words, three[0], three[1]);
        return three;
    }

    public String longest(String word){
        String[] words = word.split(" ");
        int largestLength = -1;
        String largestWord ="";

        for(String x:words)
            if(x.length()>=largestLength){
                largestWord = x;
                largestLength = x.length();
            }   
        return largestWord;
    }

    private String find(String[] words, String longest){
        String l1 ="";
        int lnum = -1;
        for(String x: words){
            if(!x.equals(longest)){
                if(x.length()>= lnum){
                        l1=x;
                        lnum=x.length();
                }   
            }   
        }
        return l1;
    }

    private String find(String[] words, String l1, String l2){
        String longest ="";
        int lnum = -1;
        for(String x: words){
            if(!x.equals(l1)){
                if(!x.equals(l2))
                    if(x.length()>= lnum){
                            longest=x;
                            lnum=x.length();
                    }   
            }   
        }
        return longest;
    }
}
jinglylime
Newbie Poster
18 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I kinda understand what you are saying.So I did this but it only shows one shape and not all of it. Sorry, I've just started learning opengl.

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <GL/glut.h>
#include <windows.h>


int frame=0;

void myinit(void)
{
    int i;
    //glEnable(GL_BLEND);
    glClearColor(1.0, 1.0, 1.0, 0.0); /* white background */
    /* set up viewing */
    glColor3f(0.0,0.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-120.0, 120.0, -120.0, 120.0);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    /* define a point data type */
        typedef GLfloat point2[2];
    point2 vertices[4]={{20,20},{20,-20},{-20,-20},{-20,20}}; // A square

        glColor3f(((float)rand())/RAND_MAX, ((float)rand())/RAND_MAX,((float)rand())/RAND_MAX); // Random color

        glBegin(GL_LINE_LOOP);
            glVertex2f(vertices[0][0],vertices[0][1]);
            glVertex2f(vertices[1][0],vertices[1][1]);
            glVertex2f(vertices[2][0],vertices[2][1]);
            glVertex2f(vertices[3][0],vertices[3][1]);
        glEnd();

        glRotatef(9,0,0, 1);
        glScalef(1.09,1.09,1);

    glutSwapBuffers();
}

void animationFunc(void){
    if(frame++<=30){
        Sleep(1000);
    }
    glutPostRedisplay();
}

int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); /* default, not needed */
    glutInitWindowSize(500,500); /* 650 x 650 pixel window */
    glutInitWindowPosition(100,100); /* place window top left on display */
    glutCreateWindow("Images"); /* window title */
    srand(time(NULL));
        myinit(); /* set attributes */
    glutDisplayFunc(display);/* display callback invoked when window opened */
    glutIdleFunc(animationFunc);
    glutMainLoop(); /* enter event loop */
}
jinglylime
Newbie Poster
18 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Thanks Emil, I've tried using the glutIdleFunc function. The animation works, but when it gets to the end its start flicking. Is is how i've used the glutSwapBuffers?

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <GL/glut.h>
#include <windows.h>

int frame=0;

void myinit(void)
{
    int i;
    //glEnable(GL_BLEND);
    glClearColor(1.0, 1.0, 1.0, 0.0); /* white background */
    /* set up viewing */
    glColor3f(0.0,0.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-120.0, 120.0, -120.0, 120.0);
}

void clear(void)
{glClear(GL_COLOR_BUFFER_BIT);  /*clear the window */}



void AnimateImg1(void)
{
    /* define a point data type */
        typedef GLfloat point2[2];
    point2 vertices[4]={{20,20},{20,-20},{-20,-20},{-20,20}}; // A square

    if(frame++<=30)
        {
        glMatrixMode(GL_MODELVIEW);
        glColor3f(((float)rand())/RAND_MAX, ((float)rand())/RAND_MAX,((float)rand())/RAND_MAX); // Random color
        glBegin(GL_LINE_LOOP);
                glVertex2f(vertices[0][0],vertices[0][1]);
                glVertex2f(vertices[1][0],vertices[1][1]);
                glVertex2f(vertices[2][0],vertices[2][1]);
                glVertex2f(vertices[3][0],vertices[3][1]);
            glEnd();
        glRotatef(9,0,0, 1);
        glScalef(1.09,1.09,1);
        glMatrixMode(GL_PROJECTION);
        glutSwapBuffers();
        Sleep(1000);
    }
    glutSwapBuffers();
    glFlush();
}

void display( void )
{
    glClear(GL_COLOR_BUFFER_BIT);  /*clear the window */
    frame=0;
    glutIdleFunc(AnimateImg1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    frame=0;
 }

int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); /* default, not needed */
    glutInitWindowSize(500,500); /* 650 x 650 pixel window */
    glutInitWindowPosition(100,100); /* place window top left on display */
    glutCreateWindow("Images"); /* window title */
    glutDisplayFunc(display);/* display callback invoked when window opened */
    srand(time(NULL));
        myinit(); /* set attributes */

    glutMainLoop(); /* enter event loop */
}
jinglylime
Newbie Poster
18 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi,I have encountered a problem while doing the animation for my program. I wanted keep adding a another shape on top of another. But for evry second image the background changes to black. Can you point out what I've done wrong. Thanks.

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <GL/glut.h>
#include <windows.h>



void myinit(void)
{
    //glEnable(GL_BLEND);
    glClearColor(1.0, 1.0, 1.0, 0.0); /* white background */
    /* set up viewing */
    glColor3f(0.0,0.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-110.0, 110.0, -110.0, 110.0);
}

void display( void )
{
    glutSwapBuffers();
        glFlush(); /* clear buffers */
}


void displayAnim( void )
{
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT);  /*clear the window */
    int i = 0;
    /* define a point data type */
        typedef GLfloat point2[2];
    point2 vertices[4]={{20,20},{20,-20},{-20,-20},{-20,20}}; // A square

    glMatrixMode(GL_MODELVIEW);
    for(i=1;i<=30;i++)
            {
        glMatrixMode(GL_MODELVIEW);
        glColor3f(((float)rand())/RAND_MAX, ((float)rand())/RAND_MAX,((float)rand())/RAND_MAX); // Random color
        glBegin(GL_LINE_LOOP);
                glVertex2f(vertices[0][0],vertices[0][1]);
                glVertex2f(vertices[1][0],vertices[1][1]);
                glVertex2f(vertices[2][0],vertices[2][1]);
                glVertex2f(vertices[3][0],vertices[3][1]);
            glEnd();
        glRotatef(9,0,0, 1);
        glScalef(1.09,1.09,1);
        glMatrixMode(GL_PROJECTION);
        glutSwapBuffers();
        Sleep(500);
        }
    glMatrixMode(GL_PROJECTION);
    glutSwapBuffers();
        glFlush(); /* clear buffers */
 }

void displayNoAnim( void )
{
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT);  /*clear the window */
    int i = 0;
    /* define a point data type */

        typedef GLfloat point2[2];
    point2 vertices[4]={{20,20},{20,-20},{-20,-20},{-20,20}}; // A square

    glMatrixMode(GL_MODELVIEW);
    for(i=1;i<=30;i++)
            {
        glColor3f(((float)rand())/RAND_MAX, ((float)rand())/RAND_MAX,((float)rand())/RAND_MAX); // Random color
        glBegin(GL_LINE_LOOP);
                glVertex2f(vertices[0][0],vertices[0][1]);
                glVertex2f(vertices[1][0],vertices[1][1]);
                glVertex2f(vertices[2][0],vertices[2][1]);
                glVertex2f(vertices[3][0],vertices[3][1]);
            glEnd();
        glRotatef(9,0,0, 1);
        glScalef(1.09,1.09,1);
        }
    glMatrixMode(GL_PROJECTION);
    glutSwapBuffers();
        glFlush(); /* clear buffers */
 }

void popupmenu(int id)
{
    glMatrixMode(GL_MODELVIEW);
    if(id==3){
        exit(0);
    }
    else if(id==2){
        displayNoAnim();
    }
    else if(id==1){
        displayAnim();
    }
}

int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); /* default, not needed */
    glutInitWindowSize(500,500); /* 650 x 650 pixel window */
    glutInitWindowPosition(100,100); /* place window top left on display */
    glutCreateWindow("Images"); /* window title */
    glutDisplayFunc(display);/* display callback invoked when window opened */
    srand(time(NULL));
        myinit(); /* set attributes */
    glutCreateMenu(popupmenu);
    glutAddMenuEntry("animated display mode",1);
    glutAddMenuEntry("non-animated display mode",2);
    glutAddMenuEntry("exit",3);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
    glutMainLoop(); /* enter event loop */
    return 1;
}
jinglylime
Newbie Poster
18 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi, I'm having trouble with setting up my table. How do i get the header to show initially. I want "code", "Number" and "Value" as the header. And I want to know if the way i added the table is correct? Can you help me please. Thank you!

import model.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;

public class Shares extends JPanel
{
    private JButton btnFind = new JButton();
    private JTextField txtId = new JTextField(10);
    private JTable tableShare = new JTable();
    Customers customers = null;
    
    public Shares(Trader trader)
    {
        customers = trader.customers();
        setup();
        build();
    }
    
    private void setup()
    {
        setLayout(new FlowLayout());
        setBorder(BorderFactory.createLineBorder(Color.blue));
    }
    
    private void build()
    {
        Box box1 = Box.createHorizontalBox();
        box1.add(new JLabel("Your id"));
        box1.add(txtId);
        btnFind.setText("Find");
        box1.add(btnFind);
        Box box2 = Box.createVerticalBox();
        box2.add(box1);
        box2.add(Box.createVerticalStrut(20));
        setTable();
        box2.add(tableShare);
        add(box2);
        setBtnFind();
    }
    
    private void setBtnFind()
    {
        btnFind.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnFindActionPerformed(evt);
            }
        });
    }
    
    private void btnFindActionPerformed(java.awt.event.ActionEvent evt){       
        Customer customer = customer();
        LinkedList<Share> shares= customer.shares();
        int j =0;
        for(Share share: shares){       
            tableShare.setValueAt(share.code(), j, 0);
            tableShare.setValueAt(share.number()+"", j, 1);
            tableShare.setValueAt(share.cost()+"", j, 2);
            j++;
        }   
    }
    
    private Customer customer(){
        int custId = Integer.parseInt(txtId.getText());
        Customer customer = customers.customer(custId);
        return customer;
    }
    
    private void setTable(){
        tableShare.setModel(new DefaultTableModel(
            new String[][]{
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
            },
            new String[]{
                "Code","Number","Value"
            }
        ));
    }
}
jinglylime
Newbie Poster
18 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

oh thanks. i get it now. =D

jinglylime
Newbie Poster
18 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi how do you delete the input on the textfield after a button is pressed. Thanks

jinglylime
Newbie Poster
18 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi I'm new to GUI. Ive created a calculator but i dont know how to get the number from the textfield and calculate the answer. Can u suggest ways i can do this. Thankyou.

jinglylime
Newbie Poster
18 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi, I need to write a system that simulates buying and selling on the stock market with just text outputs. I need to create a menu so the user can register and delete thier account. Also be able to buy and sell shares.

So far I have my classes and attibuttes

public class User
{
    private int Id;
    private String name;
    private double trade;
}
public class Share
{
    private String code;
    private int quantity;
    private double price;
}
public class Trade 
{
    private LinkedList<Share> shares = new LinkedList<Share>();
    private LinkedList<User> users = new LinkedList<User>();
}

My plan is to create the users and shares in the Trade class, and write the methods for register, delete, sell and buy in the trade class as well. I will then call these methods in a Menu class.

I'm not sure if this is the right appoarch in writing this program. Can you guys help me if my classes and attributes are right thanks.

jinglylime
Newbie Poster
18 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Yes, Thankyou.

I realise it prints all the cards in a line.

jinglylime
Newbie Poster
18 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Thanks, I made a silly mistake

jinglylime
Newbie Poster
18 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi, I need to write a pontoon game. So far i wrote the Deck and Card classes. When i try to print the toString in my deck class i get "Deck@6612fc02". What i wanted is to print 13 cards on each row. May you help me thanks.

public class Deck
{
    private LinkedList<Card> card = new LinkedList <Card>();
    private Iterator <Card> it;
    private char[] suit = {'H','D','C','S'};

    
    public Deck()
    {
        for(int i = 0; i< 4; i++) 
        {
            for(int j = 1; j <= 13; j++)
                card.add(new Card(j,suit[i]));                             
        }  
    }
    
    public void show()
    {
        System.out.println(toString());
    }

    
    public String toSring()
    {
        String s = " ";
        
        for(int i =0; i < 13; i++)    
        {
            s = card.toString() + "\n";
        }        
        return s;
    }
    
}
public class Card
{
    private int value;
    private char suit;
    
    public Card(int value, char suit)
    {
        this.value = value;
        this.suit = suit;
    }
    
    public String toString()
    {
        String s= suit + " " + value;
        return s;
    }
}
jinglylime
Newbie Poster
18 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Thanks guys, it helped alot.

jinglylime
Newbie Poster
18 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Thanks i've solved that. I still have problem with

Rectangle biggest = findBiggest();

i get an error "findBiggest(Rectangle[]) in BiggestRectangleProgram cannot be applied to ()" Thanks

jinglylime
Newbie Poster
18 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi I've fixed it but still having problems with the line Rectangle biggest = findBiggest();, and the line return Rectangle[]; in readRectangles() method. Can you help me please. thanks

import javax.swing.*;
import java.util.*;

public class BiggestRectangleProgram
{
    private static final Scanner keyboard = new Scanner(System.in);
    
    public static void main(String[] args)
    {
        Rectangle[] rects = readRectangles();
        Rectangle biggest = findBiggest();
                                System.out.println("The biggest rectangle is " + biggest);
    }

    private static Rectangle findBiggest(Rectangle[] rects)
    {
        Rectangle biggestSoFar = rects[0];
        for (int i = 1; i < rects.length; i++)
        {
            //if (this one is bigger than biggestSoFar)
            if(rects[i].getArea() > biggestSoFar.getArea())
            //    biggestSoFar = this one
                biggestSoFar = rects[i];
        }
        return biggestSoFar;
    }

    private static Rectangle[] readRectangles()
    {
        // This method contains both syntax errors *and* logic errors
        //
        int howMany = getInt("How many rectangles?");
        Rectangle[] rects = new Rectangle[howMany];
        for (int i = 0; i < rects.length; i++)
        {
            int width = getInt("Width of rectangle #"+i+":");
            int height = getInt("Height of rectangle #"+i+":");
            rects[i] = new Rectangle(width, height);
        }
        return Rectangle[]; // problem here
    }

    private static int getInt(String prompt)
    {
        System.out.print(prompt + " ");
                                return keyboard.nextInt();
    }
}
jinglylime
Newbie Poster
18 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Solved: Arrays help in Java

Hi guys, Im trying to write a program that find the biggest rectangle by its area. I've tried and it doesnt seem to compile. Can you help me please, thanks.

public class Rectangle
{
    private int width;
    private int height;

    public Rectangle(int width, int height)
    {
        this.width = width;
        this.height = height;
    }

    // How big is this rectangle?
    public int getArea()
    {
        return width * height;
    }

    public void setWidth(int width)
    {
        this.width = width;
    }

    public int getWidth()
    {
        return width;
    }

    public void setHeight(int height)
    {
        this.height = height;
    }

    public int getHeight()
    {
        return height;
    }

    public String toString()
    {
        return width + "x" + height;
    }
}
import javax.swing.*;
import java.util.*;

public class BiggestRectangleProgram
{
    private static final Scanner keyboard = new Scanner(System.in);
    
    public static void main(String[] args)
    {
        Rectangle[] rects = readRectangles();
        Rectangle[] biggest = findBiggest();
                                System.out.println("The biggest rectangle is " + biggest);
    }

    private static Rectangle findBiggest(Rectangle[] rects)
    {
        Rectangle biggestSoFar = rects[0];
        for (int i = 1; i < rects.length; i++)
        {
            //if (this one is bigger than biggestSoFar)
            if(rects[i] > biggestSoFar)
            //    biggestSoFar = this one
                biggestSoFar = rects[i];
        }
        return biggestSoFar;
    }

    private static Rectangle[] readRectangles()
    {
        // This method contains both syntax errors *and* logic errors
        //
        int howMany = getInt("How many rectangles?");
        Rectangle[] rects = new Rectangle();
        for (int i = 0; i < rects[i]; i++)
        {
            int width = getInt("Width of rectangle #"+i+":");
            int height = getInt("Height of rectangle #"+i+":");
            rects[i] = new Rectangle(width, height);
        }
        return Rectangle();
    }

    private static int getInt(String prompt)
    {
        System.out.print(prompt + " ");
                                return keyboard.nextInt();
    }
}
jinglylime
Newbie Poster
18 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

hi, im trying to make a bike move by using the pedal() method, the method will move the bike forward a distance of half the circumference of a wheel. ive tried creating the pedal() method however when the method is invoke the position of the bike is still the same. please help me. thanks.

import java.lang.Math;

public class Wheel
{
    private double position;
    private double radius;

    public Wheel()
    {
        position = 0.0;
        radius = 50.0;
    }

    public void setRadius(double newRadius)
    {
        radius = newRadius;
    }
    
    public void setPosition(double newPosition)
    {
        position = newPosition;
    }

    public void rollForward(double distance)
    {
        position = position + distance;
    }

    public double getPosition()
    {
        return position;
    }
    
    public double getRadius()
    {
        return radius;
    }
    
    public double getCircumference()
    {
        double circumference;
        circumference = 2*radius*Math.PI;
        return circumference;
    }
     
}
public class Bike
{
    private Wheel frontWheel;
    private Wheel backWheel;

    public Bike()
    {
        
        frontWheel = new Wheel();
        backWheel = new Wheel();
        
        frontWheel.setPosition(50);
        backWheel.setPosition(-50);
    }
    
    public double getPosition()
    {
        double position;
        position = (frontWheel.getPosition() + backWheel.getPosition())/2;
        return position;
    }
    public double pedal() // here's my problem
    {
        
        double position;
        position = getPosition() + frontWheel.getCircumference()/2;
        return position;
    }
}
jinglylime
Newbie Poster
18 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
 
© 2013 DaniWeb® LLC
Page rendered in 0.1156 seconds using 2.62MB