Hmm I was lookin through a recursion program in this book i have here and the only thing I'm having trouble with is the modulus operator. I know that it returns the remainder of 2 numbers. But, suppose I have this: x % y If x > y then the result is x. Am I tripping out o_O?

Member Avatar
Member Avatar
-1 forum 10

Let me explain what I'm supposed to do first. My assignment is to take text from a dictionary entry and display it in my client area. This dictionary entry contains characters like \ " ' and text that's written in Greek. There is an image in the upper left hand corner of the word that I'm defining and the text is supposed to flow around this image. Also, when I hover my mouse over any single word, it changes from black to red and returns to black when my mouse does not hover over it. The way we were told …

Member Avatar
Member Avatar
+0 forum 1

Hi, In on of the lecture notes that was given to me by my prof, I was told that setting the ResizeRedraw value to true would allow whatever I drew on the form to be, well, resized. However, it does not seem to work. Here is my code: [CODE]using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Test1 { public partial class Form1 : Form { private Rectangle m_Rect; public Form1() { Point p = new Point(ClientRectangle.Width / 2 - 100 / 2, ClientRectangle.Height / 2 - 100 / 2); Size s …

Member Avatar
Member Avatar
+0 forum 5

When I add buttons to a panel, the buttons seem to take a huge amount of space. How do I get rid of the space? For example: [CODE] import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Tester { public static void main(String[] args) { JButton dayButton = new JButton("Day"); JButton weekButton = new JButton("Week"); JButton monthButton = new JButton("Month"); JButton leftButton = new JButton("<<"); JButton rightButton = new JButton(">>"); JPanel topButtonPanel = new JPanel(); topButtonPanel.add(dayButton); topButtonPanel.add(weekButton); topButtonPanel.add(monthButton); JPanel bottomButtonPanel = new JPanel(); bottomButtonPanel.add(leftButton); bottomButtonPanel.add(rightButton); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS)); buttonPanel.add(topButtonPanel); buttonPanel.add(bottomButtonPanel); JFrame frame = new JFrame(); frame.add(buttonPanel); …

Member Avatar
Member Avatar
+0 forum 6

Ok, I looked in the API and was able to create the scroll bars. Problem is, I can't make them scroll, even though the parameters I passed in explicitly said to have the bars around. What I'm trying to do is to create a scrollpane around a JComponent object that draws something with paintComponent(). (The object runs off the panel).

Member Avatar
+0 forum 0

Suppose I have a week that begins with Sunday and ends with Saturday (so UMTWThFS). How do I get the date for each day of the week if, lets say, we use this month and year? I looked in the API but I wasn't sure what to extract to make it work.

Member Avatar
Member Avatar
+0 forum 5

I'm having a little trouble trying to nest two component classes (both extend JComponent) together. When I create the frame in my tester file, instantiate each class, and try to add both of them to the frame, it doesn't come out correctly. For instance, I want my frame to display the TimeComponent right next to the DayComponent in this program: Tester.java [CODE]import java.awt.*; import javax.swing.*; public class DayTester { public static void main(String[] args) { JFrame frame = new JFrame(); TimeComponent time = new TimeComponent(); DayComponent day = new DayComponent(); frame.add(time); frame.add(day); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setVisible(true); } private static final …

Member Avatar
Member Avatar
+0 forum 5

1) How do I tell if I have java 5 or java 6 installed? 2) I have my program commented using javadoc syntax. However, when I run javadoc from the command prompt and then open the html files, I do not see any documentation. What am I doing wrong? For instance, my comments look like this in the source file: [CODE] /** * Determines if two APoint objects are equivalent * @param b the APoint object being compared with 'this' * @return a boolean value which determines if two points are equal */[/CODE] I type in the following into the …

Member Avatar
Member Avatar
+0 forum 2

I'm almost finished with this program but the problem I'm having is with freeing the memory I allocated. Particularly in the area where I have pointers to strings. [CODE]#include <stdio.h> #include <string.h> #include <stdlib.h> /////////////// user defined data ///////////////////// struct State { char * name; // state name int year; // year of entry into the union char * cap; // state capital int popRank; // population ranking }; struct Database { struct State * list; // array of states int count; // total count of states char * filename; // input filename }; /////////////// function prototypes //////////////////// struct Database …

Member Avatar
Member Avatar
+0 forum 5

The problem is that I have an ArrayList which stores objects that contain a Shape (actually a Composite shape like a Car) and (x,y) coordinates. Suppose I'm supposed to retrieve each object and the coordinates for drawing. My question is, how do I draw an Shape on the screen at position (x,y)? I looked in the API but didn't find any set() or overloaded draw() methods.

Member Avatar
Member Avatar
+0 forum 1

I'm trying to put all the buttons (and their icons) into a single class. However, when I try to draw just one button in a frame, it doesn't show up. I can get it to be displayed if I instantiate the button in the same class as the Frame, but not in a separate class. Not sure what I'm doing wrong here :| [CODE]import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import javax.swing.*; public class ShapeDisplayer { public static void main(String[] args) { ShapeFrame frame = new ShapeFrame(); // frame.addShape(new SnowMan(0,0,20)); frame.addShape(new Car(0,0, 50)); // frame.addShape(create your composite shape here); frame.setSize(300, 400); …

Member Avatar
Member Avatar
+0 forum 3

I'm stuck trying to figure out how to update the text field after I click on the button in the GUI. The number of roaches is supposed to be updated each time I click, but I can't seem to find the right tool for the job in the APIs. RoachPopulation.java [CODE]public class RoachPopulation { public RoachPopulation(int initialPopulation) { pop = initialPopulation; } public void doublePopulation() { pop = pop * 2; } public int getPopulation() { return pop; } private int pop; } [/CODE] RoachPopulationViewer.java [CODE]import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; /** …

Member Avatar
Member Avatar
+0 forum 1

This is a relatively simple program but for some reason, I can't compile it. I'm pretty sure the code is correct and working. My compiler keeps saying that it cannot find a constructor matching a HourlyWorker constructor when I instantiate a HourlyWorker object. I've checked and rechecked the code (in WorkerTester and HourlyWorker classes) to make sure everything matches, but I can't find anything wrong. Worker.java [CODE]public class Worker { public Worker(String n, int r) { name = n; rate = r; } public double computePay(int hours) { return (rate * hours); } public int getRate() { return rate; } …

Member Avatar
Member Avatar
+0 forum 8

I did a quick search on the forums and there was another problem similar to the one i'm trying to solve. I'm supposed to use the first word of a sentence as a search parameter and check the rest of the string for occurences of the word. If the word appears again, increment a counter. The problem I'm having is that when I use the function strstr() in a while loop, the console hangs :rolleyes: . Correct me if i'm wrong, but the strstr() function returns a NULL if the substring isn't found in the main string. So, if my …

Member Avatar
Member Avatar
+0 forum 5

So we're supposed to modify this class, which implements Comparable, so that it accepts any type of object. The Comparable interface contains the method signature for compareTo, which I need to implement. What I'm uncertain of is how to implement this method. I know that compareTo is supposed to return -1, 0, or 1 but I'm not sure what goes inside the if statements (-1 if object1 < object 2, 0 if object 1 == object 2, and 1 if object1 > object2) Also, assume that we can only have the instance variable minimum and maximum and that the rest …

Member Avatar
Member Avatar
+0 forum 5

So here's the problem [QUOTE]Write a program that asks the user to input a set of floating-point values. When the user enters a value that is not a number, give the user a second chance to enter the value. After two chances, quit reading input. Add all correctly specified values and print the sum when the user is done entering data. Use a sentinel value to signal the end of input. Use exception handling to detect improper inputs. Here is a sample program run: Value: 1 Value: 2 Value: three Input error. Try again. Value: 3 Value: four Input error. …

Member Avatar
Member Avatar
+0 forum 9

I'm supposed to design a method that takes in a double value and translates the value into the closest letter grade. 4,3,2,1,0 denote A, B,C,D,F, respectively. + increases a grade's numerical value by 0.3 and - decreases a grade's numerical value by 0.3. There is no F+ or F-. A+ is 4.0 Here is the problem: How do I change my method so that when I enter 2.8, the grade returned will be a B- and when I enter 2.85, the grade returned will be a B? Here's whaT I got: [CODE]public class Grade { public Grade(double numGrade) { num …

Member Avatar
Member Avatar
+0 forum 2

Ok, so my goal is to draw a rectangular spiral in java. So far, I've created the Spiral Viewer class and the SpiralComponent but I'm not quite sure how I would implement the SpiralGenerator class. Here's what I have: SpiralViewer: [CODE]import javax.swing.JFrame; /** Test driver for Spiral class. */ public class SpiralViewer { public static void main(String[] args) { JFrame frame = new JFrame(); final int FRAME_WIDTH = 400; final int FRAME_HEIGHT = 400; frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("SpiralViewer"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); SpiralComponent component = new SpiralComponent(); frame.add(component); frame.setVisible(true); } }[/CODE] SpiralComponent: [CODE]import javax.swing.JComponent; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Point2D; import java.awt.geom.Line2D; public class …

Member Avatar
Member Avatar
+0 forum 2

I'm trying to get this ISBN checker to work. I've used the formula from this [URL="http://mathworld.wolfram.com/ISBN.html"]site[/URL] and also this [URL="http://en.wikipedia.org/wiki/International_Standard_Book_Number"]site[/URL] and it seems to work fine [B]except[/B] for the ISBN of my Java book. When I compute my book's ISBN (0131496980), it is apparently invalid. I have tried the other codes on both of those sites as well as other books I have at home and there is no problem. Here is my code: [CODE]public class DigitExtractor { public DigitExtractor(String code) { isbn = code; } public int nextDigit() { // convert to int digit = isbn.charAt(count) - 48; count …

Member Avatar
Member Avatar
+0 forum 3

I was a little confused by the brief explanation that my Assembly teacher gave on pipelining. I don't think I really understand the process that well... 1) In a 4-stage non-pipelined processor, how many clock cycles does it take to execute 3 instructions? Assume each stage takes 1 clock cycle. 2) In a 4-stage pipelined processor, how many clock cycles does it take to execute 3 instructions? Assume each stage takes 2 clock cycle.

Member Avatar
Member Avatar
+0 forum 1

One of the things that's been confusing me so far is why we have to override the paintcomponent() function after extending it. For example, this program draws an ellipse: [CODE]import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import javax.swing.JComponent; public class DrawEllipseComponent extends JComponent { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; Ellipse2D.Double ellipse = new Ellipse2D.Double(0,0,this.getWidth(),this.getHeight()); g2.draw(ellipse); g2.setColor(Color.GREEN); g2.fill(ellipse); } }[/CODE] [CODE] import javax.swing.JFrame; public class DrawEllipse { public static void main(String[] args) { DrawEllipseComponent ellipse = new DrawEllipseComponent(); JFrame frame = new JFrame(); frame.setSize(300, 400); frame.setTitle("An Ellipse"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(ellipse); frame.setVisible(true); } }[/CODE] I can see that …

Member Avatar
Member Avatar
+0 forum 4

Hi, I need help with drawing an ellipse that is bounded by the window it is drawn in. I have to use the Ellipse2D API. Maybe I'm overlooking something but I can't seem to get the circle to fit the window. This is what i have so far: [CODE]import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import javax.swing.JComponent; public class DrawEllipseComponent extends JComponent { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; Ellipse2D.Double ellipse = new Ellipse2D.Double(0,0,200,300); g2.draw(ellipse); g2.setColor(Color.GREEN); g2.fill(ellipse); } }[/CODE] [CODE]import javax.swing.JFrame; public class DrawEllipse { public static void main(String[] args) { DrawEllipseComponent ellipse = new DrawEllipseComponent(); …

Member Avatar
Member Avatar
+0 forum 1

If I do a public inheritance and the member data in the base class I'm deriving from is protected, is the only way to access the data in the base class via member/friend functions of the derived class?

Member Avatar
Member Avatar
+0 forum 3

Hi, I can't get the following code to work. I have my converting constructor (second line) to change an int type to a car type but my compiler says there's something wrong with the overloaded + operator. It says here in my book that if I want to do an implicit type conversion: 1) The class must have an accessible converting constructor 2) The overloaded function must be nonmember (with or without friendship) Let's suppose that the code given in the class is correct (b/c my teacher went over it two days ago and it was supposed to be correct. …

Member Avatar
Member Avatar
+0 forum 1

How do i dereference a pointer to something in a class without having to write another function to dereference it for me? For instance in this code: [CODE]class Fraction { public: Fraction(int left = 0, int right = 1); Fraction(Fraction & obj); ~Fraction() { delete fl; } double getFraction() { return *fl; } void operator=(Fraction & obj); friend ostream &operator<<(ostream & out, Fraction & obj); private: int n; // numerator int d; // denominator double * fl; // pointer to double value which is the decimal equivalence // of the fraction }; ostream &operator<<(ostream & out, Fraction & obj) { …

Member Avatar
Member Avatar
+0 forum 3

The problem I'm having is with overloading the >> operator so that I could read data from a file directly into a class's member data. Everytime I run through the program, it just crashes. when I look through the debugger, it brings me to some page of crazy code. So far, I've managed to make everything in my program work except just that. I have no clue why this segment of code won't work. [CODE]Atlas::Atlas(char * file) { ifstream inputFile(file); db = new Database(file); inputFile >> *db; // gives me problems :\ // db->fillDatabase(); inputFile.close(); }[/CODE] Here's the complete code: …

Member Avatar
Member Avatar
+0 forum 2

I'm having a bit of a problem with function style cast with classes. When I exit the function I expect an instance of Database to be returned. This instance would then be copied into the db declared in main. I've writtien a copy constructor and it works fine (tested by ordinary initialization) but what I can't seem to copy the data over. [CODE]// main Database const db = makeDb(); // makeDb() Database makeDb() { int const size = 100; char file[size]; cout << "Enter a file name <press ENTER to use the default name>: "; cin.getline( file, size ); // …

Member Avatar
Member Avatar
+0 forum 7

This might seem like a silly question, but how do I read in a newline char, by itself, as input? For example, if I gave the user a choice to enter a string for a new filename, or let them just press ENTER for some other choice. forgot to add that this is in C++.

Member Avatar
Member Avatar
+0 forum 8

If I have a dynimcally allocated array of objects and each of these objects contains a pointer to a string, when I use the 'delete' function to return memory, do I have to go through each array element and free each string individually or does the 'delete' take care of that for me in C++?

Member Avatar
Member Avatar
+0 forum 6

So in my class we just finished covering the topic of constructors and destructors. Now, my teacher has given us a lab where we have to create these classes and each of them must have a ctor/dtor. The problem I'm running into is..well.. it would be easier to describe in code :mrgreen: here's what my main looks like: [CODE]class Database { public: Database(char * file = "states.txt"); ~Database(void); void deleteDatabase (); void fillDatabase (); void insertDatabase (int index, State & tempState); void printDatabase (); private: State * list; // array of states int count; // total count of states char …

Member Avatar
Member Avatar
+0 forum 2

The End.