Make A Java Program That Calls Object Value Initialized
Feb 23, 2014
i have this following codes and im trying to make a java program that calls an object value initialized.
public class TestClass
{
public String name;
public int age;
public void myMessage()
{
System.out.println("Hello I'm" + name + "and i'm" + age + "years old");
}
}
what i want is to put the following codes of the object.. so once i compile and run the project it executes the value of an object from the method i defined above.
TestClass myName = new TestClass();
myName.name = "Jefferson";
myName.age = 18;
my question is, it is possible to declare or define it in the same project? or i need to do this separately?
View Replies
ADVERTISEMENT
Mar 24, 2014
I don't remember where but I faintly recall one of the oracle docs examples (dealing with GUI basics I think) having something similar to
button.add(lbl).addIcon().addActionListener(al);
but only in the aspect of using .method.method.method
The reason I'm asking is because I want to shorten this
int j = 0;
for(int i = 0; i <= 6; i+=3){
pnl[j+2].add(lbl[i+5]);
pnl[j+2].add(lbl[i+6]);
pnl[j+2].add(drop[j]);
pnl[j+2].add(lbl[i+7]);
pnl[j+2].add(btn[j]);
j++;
}
Into something like this
int j = 0;
for(int i = 0; i <= 6; i+=3){
pnl[j+2].add(lbl[i+5])
.add(lbl[i+6])
.add(drop[j])
.add(lbl[i+7])
.add(btn[j]);
j++;
}
I might be way off though... but I'm just wondering under what circumstances would this be possible
When I try it in Eclipse a little notification appears that says...
"The method add(PopupMenu) in the type Component is not applicable for the arguments (JLabel)"
When I google 'Component' I see it appears to have something to do with Serialization (which I'll need to look into)
View Replies
View Related
Mar 27, 2015
can a keyevent in java make the space ship and the laser too ?
View Replies
View Related
Mar 1, 2015
Any tips on how i can improve this simple resource calculator for this game called Clan of Clans.
It might even look more functional than OOP.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ClashOfClan {
private static String[] units = {"Barbarians ", "Archers ",
"Giants ", "Goblins ", "Wall Breakers ", "Balloons ",
[Code] .....
View Replies
View Related
Apr 1, 2015
Consider the two simple Java classes below:
Java Code:
class Computer {
Computer() {
System.out.println("Constructor of Computer class.");
}
void On() {
System.out.println("PC turning on...");
}
void working() {
[Code] ....
After the program run, how do I trace (1) which object call which method (2) and how many times?
View Replies
View Related
Jun 26, 2014
So i have a problem i want to make a menu that allows you to press 1 and then make lets say a Car,
The code would look like
Car car = new Car();
And then add to my list
cars.add(car);
If in my menu the person presses 1 again, will they add another car or will it override it
Also: how can i make a menu that doesnt allow you to press option 5 (something that requires you something that should be registered in other options ), until you registered them?
View Replies
View Related
Jun 25, 2013
I want to make a simply game,actualy not a game but I want to moving object curve just like angry bird. Where I can move an object with curve motion. But I don't know how. I just can make it move straight(left,right,down,up).
View Replies
View Related
May 13, 2014
I have a Die class that extends Label. It has a SimpleIntegerProperty called faceValue. In another class I have an ObservableArrayList of Die called dice and a FilteredList of all the die in dice with a faceValue of 2. The Problem is that when I update the faceValue (ie. roll()) the filteredlist doesn't update with the Die that faceValue=2. I believe the reason is that the filteredlist updates when a ListChangeListener is fired. If i add, subtract elements from dice I don't have a problem but how do I make it so that updating the faceValue on the die counts as a ListChange, or is there even a way to do that.
View Replies
View Related
Feb 10, 2014
I'm trying to make a program with two buttons... when you click the first a label shows a red square when you click the second it shows a green square... right now i have the imageicons as a comment because when they weren't a comment and i ran the program i saw nothing... the same thing happens when i add label2 to panel3.
Java Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI3{
public static void main(String[] args){
//frame
JFrame frame = new JFrame("Test");
[code]...
View Replies
View Related
May 18, 2014
I have made a basic math game that asks you questions and tells you if you have answered them correctly or incorrectly. The game runs great! The thing is that it asks you a question one time and after you answer you have to run the program again. I want to get the program to ask ten questions. After that I want to figure out a scoring system for it but the first step is to get it to ask my ten questions. Here is my code.
package pkgnew;
import java.util.Scanner;
import java.util.Random;
public class New {
public static void main(String args[]) {
[Code] .....
View Replies
View Related
Apr 30, 2015
I just recently started programming and came across the star patterns which normally I am able to do extremely quickly except with this one. I am trying to make a hourglass but I seem to only be able to make the top of it. How to do the opposite so I can finish the hourglass.
package hourglass;
class Hourglass {
public static void main (String[] arguments){
for(int j=1;j<=5;j++) {
for(int i=1;i<=j;i++)
[Code] .....
View Replies
View Related
Jul 17, 2014
where to create the array of chars that hold the letter grade. I am assuming that would be in the main with the other arrays. Passing it into an object creating a method in the record.java. Then displaying it in my main.
Take the Grades program and make a class. An object that hold 5 student names, an array of 5 chars that hold the letter grades, 5 arrays of four doubles each to hold the set of test scores. The class should have methods that return a specific student's name, average test score and a letter grade based on the average. Demonstrate the class in a program that allows the user to enter each student's name and their 4 tests scores. It should then display each student's average and letter grade.This is my main program:
import java.util.Scanner;
public class GradeBook{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Record[] students = {new Record(), new Record(), new Record(), new Record(), new Record()};
for(int j=0; j < 5; j++){
[code]....
View Replies
View Related
Oct 14, 2013
I am screwing around with mouse listeners so i decided to make a basic paint program. It works fine but when it draws more than 100 circles it errors out. i know why its doing it (its because i set the array to 100) but my question is how to make it so it has not limit in the array.
Here is the code
public class PaintProject extends Applet {
int numClicks = 100;
int numCicles;
int xCoord[];
int yCoord[];
boolean paint;
[Code] .....
View Replies
View Related
Mar 11, 2015
i am working on my assignment in Compro 2, we are ask to make a simple calculator program
here's my code;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.BorderLayout;
[code]...
View Replies
View Related
Sep 4, 2014
So obviously I'm new to java and i need to write a program that gives exact change in quarters, dimes, nickels, and pennies. For some reason it seems to work correctly but the nickels never is correct and I'm not sure why, seeing as though everything else is.
Java Code:
public class Coins{
private int c;
public Coins(int coins){
c = coins;
}
public void calculate(){
int quarters = c / 25;
[Code] .....
View Replies
View Related
Nov 19, 2014
My code below is trying to make a connect four program without GUI. I'm having trouble with getting the players to make their moves. What should I put in my "makeAMove" method...
Java Code:
public class Connect
final static int MAXROW = 6;
final static int MAXCOL = 7;
public static void main(String[] args){
[code]....
View Replies
View Related
May 12, 2015
So I'm trying to make a simple program which takes in an argument (target) and then looks through an ArrayList of strings. If it finds a string that begins with (target) then it will return the index of that string. If it doesn't find a string which begins with (target) then it will return -1 instead.
For some reason, the program is always returning -1, rather than the index of the string within the ArrayList when there is one which matches the search criteria.
Here is the code:
public int getIndex(ArrayList<String> text, String target)
{
int i = 0;
int index = -1;
boolean found = false;
[Code].....
View Replies
View Related
Oct 24, 2014
I want to create a program where i can output a number. ex. 3452 And the program will output 3000: three thousand 5: five 53: fifty-three
How can i make the program understand the 10-number system?
View Replies
View Related
Feb 18, 2015
How do I make the file reader object move to the next line if there is no more input on the line. Here is my text and output file as you can see that my text file column cuts off on the 2nd line after 70. I want to read that next line which is 100 into my labs variable however its reading it into my final exams variable. I'll also post the code but I didn't think it was necessary.
textfile:
100908095
1008070
10070
output:
Labs Projects Tests Final Exams
100908095
1008070100
70
[import java.util.Scanner;
import java.io.*;
public class MyGrades
{
public static void main (String[] args) throws IOException
{
int lab, project, test;
int finalExam;//Par and Player values
[Code]...
not sure if I code wrapped it correctly
View Replies
View Related
Jan 13, 2015
how can i make a program that declare the largest and smallest out of three numbers??
/*
* To change this license header, choose License Headers in Project Properties.
int a;
int b;
int c;
[Code]....
View Replies
View Related
Sep 12, 2014
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number;
System.out.println("Input a number");
[code]....
What i want to do is that if the number is greater than 50 to count until 100. else if the number is less than fifty to count for example from 36 to zero.
View Replies
View Related
Apr 13, 2015
I created the following hangman program, but it only works for one word file inputs. How can I modify the program to make it work for files with a multiple word phrase?
/**This program is a basic Hangman game. It takes a word or phrase from a file, and then asks the user to guess the letters in it. The program ends when the user inputs 8 wrong guesses.
*/
import java.util.Scanner;
import java.io.*;
public class hangman{
public static void main (String[] args) throws FileNotFoundException{
Scanner kb = new Scanner(System.in);
String guess;
[Code] ....
View Replies
View Related
Sep 14, 2014
how to make a program that determines the highest value out of the inputted numbers.
View Replies
View Related
May 21, 2014
Ok, so I have to make a program that tells the user to type a sentence and at the end of the sentence a "." must be put to end the program. If a "." period is not entered, it must re-prompt the user to enter a period. On top of all this, the program has to count the number of letters in the sentence. I cant seem to get the while loop working and I cant find anywhere how to make a counter count the number of letters.
import java.io.*;
class Sentence
//The purpose of this program is to
[Code]....
View Replies
View Related
May 7, 2014
I am new to programming and we are writing a program to make a tic tac toe game utilizing inheritance, client-supplier, arrays, and EventButton. I am still trying to wrap my head around events and inheritance. We are instructed to write 4 classes (a TicTacToeModel, a TicTacToeView, a TicTacToe Controller, and a TicTacToeButton). I got the TicTacToeModel down, but am having trouble with the other classes.
Instructions for View: Write a class that implements the UML class diagram below. The only purpose of this class is to draw a picture of the specified TicTacToeModel. For purposes of this assignment, we understand a ‘theme’ to include such things as the background color of the tic‐tac‐toe board, the shape and color of the X indicator and the shape and color of the O indicator. While you are free to select your own personalized ‘theme’ your TicTacToeComponent must always be proportionally ‘correct’ regardless of the client‐determined width and height of the Component.
Instructions for the Controller: Write a controller that controls the game. There is one human player (the X player) and the computer player (the O player). The name of the class must be TicTacToeController. In a sense, the controller is the game since the controller will
1) create a TicTacToeModel
2) create a TicTacToeView and
3) create a TicTacToeButton (you must write this class following the design pattern covered in class lectures), a label, and text field such that when the button is pushed, the player moves into the cell selected by the text field. After every player move, the computer moves into a randomly selected empty cell. When the game is over, a text message must be displayed somewhere on the screen the gives the status of the game. While you are free to change the appearance of the controller, the basic elements must be provided
These are our instructions for the TicTacToeButton: create a TicTacToeButton (you must write this class following the design pattern covered in class lectures), a label, and text field such that when the button is pushed, the player moves into the cell selected by the text field. After every player move, the computer moves into a randomly selected empty cell. When the game is over, a text message must be displayed somewhere on the screen the gives the status of the game.And this is the code I have thus far for the button:
import java.awt.event.*;
import java.awt.*;
public class TicTacToeButton extends EventButton{
public TicTacToeButton(int x, int y, int w, int h) {
super("Move");
setBounds(x, y, w, h);
[code]....
View Replies
View Related
Sep 23, 2014
I continuously get an error for lines 34, 36, and 37 saying that the variables may not have been initialized.
import java.util.Scanner;
import java.util.Random;
public class MathTutor {
public static void main(String[] args) {
Random r = new Random ();
Scanner input = new Scanner (System.in);
/*int min=1;
int max=10;*/
int num1,num2,operation;
int n1= r.nextInt((9+1)+1);
int n2= r.nextInt((9+1)+1);
operation= r.nextInt(3);
int correctAnswer;
int userAnswer;
[code]....
View Replies
View Related