Creating Multiple Classes

Apr 25, 2014

I was trying to play around a little bit after learning creating multiple classes and stuff.However,i encountered a strange problem with reading a value from the user and then storing it in a variable.The usual way i do it is

Scanner variableName=new Scanner(System.in);
System.out.println(variableName.nextLine());

But when i trying to print the contents of the variable "variableName" the compiler throws a lot of errors .I am attaching how i have tried that out in my code

import java.util.Scanner;
class laptop{
private String modelNumber;
private boolean hasFan;
private float ramSpeed;
protected int numCores;
//private String input;

[code]....

Without the setInfo() in the laptop class the program functions as desired but i intend to ask the user if he wants to modify something and then reflect the same.

View Replies


ADVERTISEMENT

How To Draw Multiple Graphics Inside One JPanel Using Multiple Classes

May 5, 2015

I'm very new to Java, and I am creating a program that takes multiple user input to create one face. I have a class for the eyes, nose, lips, and headshape. For some reason, my program is not drawing the graphics. ***for question purposes, I have only included my head shape class and my test class****

my "test" class:

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class FaceTest
{
public static void main(String[] args)
{
String head = JOptionPane.showInputDialog("Would you like a circle, square, rectangle shaped head?: ");

[Code] ....

View Replies View Related

Creating Three Exception Classes

Sep 28, 2014

Why I have to place two parameters in the Verify constructor. If the numbers are being typed in by the user and passed through the validate method, what is the point of having them in Verify() ?

First, create three exception classes named NumberHighException, NumberLowException, and NumberNegativeException. Both NumberHighException and NumberLowException should be directly subclassed from the Exception class, but NumberNegativeException should be subclassed from NumberLowException. You can use the BadDataException class that was defined in this module as the model for your exception classes.

Next create a class called Verify that will be used to validate that a number is within a specified range. It should have one constructor that has two int parameters. The first parameter is the minimum number in the range, and the second parameter is the maximum number in the range.

In addition to the constructor, the Verify class should have one method that is named validate. The validate method should have a single parameter of data type int. The parameter contains the number that is being validated. If the value of the parameter is less than zero, the method should throw a NumberNegativeException. If the value is less than the minimum value of the range, it should throw a NumberLowException. If the value is greater than the maximum value of the range, it should throw a NumberHighException. If the value is within the specified range, no exception should be thrown.

Once all of these classes are created, create the driver class called Program5. The driver class should instantiate a Verify object with a range of 10 to 100. It should then do the following:

-Prompt the user to input a number within the specified range.
-Use a Scanner to read the user input as an int. You can ensure that an int was entered because the nextInt method throws an --InputMismatchException if any non-digits are entered.
-Call the validate method to validate that the number is within the range.
-Print an appropriate error message if the value is not within the range, or print the value if it is within the range.

View Replies View Related

ArrayList Through Multiple Classes?

Oct 25, 2014

I have to make a retail item class that has description, price and quantity. I then have to make a CashRegister class that calculates subtotal, tax, total and displays it (toString). I also have to make a driver class to call the methods from main. I have no clue what I am doing and what I have done does not work. I am trying to make it sort of like a cart, in the CashRegister it should ask the user to enter the description, quantity purchased and the price. Then it would send that to the arraylist of retailitem. I am having trouble using those items in my other methods and then do not know how to call those in the driver class.. So here are my classes:

public class RetailItem {
private String description;
private int quantity;
private double price; 
public RetailItem(){
description = "";
quantity = 0;

[Code] ....

View Replies View Related

Using Multiple Classes In One File

Mar 1, 2015

I have recently start learning about objects and classes in java. I understand that in order to use multiple classes in one file you need one class to be public and the rest are private. But after that I don't understand how to invoke the private class in the public class. I thought if I tried programming on my own I can have a better understanding of it, but had no success.

View Replies View Related

Multiple Classes In One Package?

Jan 31, 2015

I'm having problems with this I've code like:

Java Code:

Public Class{
Objects
Constructor(){}
ActionListener Method(){}
Main Function()}

[Code]....

but I cannot declare variables in this method, how can I do this what can be the simplest method?

In addition: I've a marksheet class, a calculator class, & in main class I've 2 buttons one for marksheet & one for calculator, how can I make it so that if user clicks on marksheet button that class should be called & so?

View Replies View Related

ArrayList Through Multiple Classes

Oct 25, 2014

Alright.. So I have to make a retail item class that has description, price and quantity. I then have to make a CashRegister class that calculates subtotal, tax, total and displays it (toString). I also have to make a driver class to call the methods from main. I have no clue what I am doing and what I have done does not work. I am trying to make it sort of like a cart, in the CashRegister it should ask the user to enter the description, quantity purchased and the price. Then it would send that to the arraylist of retailitem. I am having trouble using those items in my other methods and then do not know how to call those in the driver class...

So here are my classes: [URL] ....

View Replies View Related

Creating A Program To Call Classes

Aug 15, 2014

The requirements are as follows:Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type Day:

A. Set the day.

B. Print the day.

C. Return the day.

D. Return the next day.

E. Return the previous day.

F. Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add four days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.

G. Add the appropriate constructors.

H. Write the definitions of the methods to implement the operations for the class Day, as defined in A through G.

I. Write a program to test various operations on the class Day.Should I break down my day.java into several separate classes, one for each of the sections (previous, next, etc)?

import java.util.*;
public class MyDay
{
static Scanner readinput = new Scanner(System.in);
String day;
public MyDay(String day)
{
day = "Sunday";

[code]....

Ideally I would like to create a Gui that would have someone type in the day, and then press a button to return the next day, prior day, or have a text input to test for adding X number of days.

View Replies View Related

How To Map Parameters From Multiple Classes Into One Set Of Parameter

Mar 6, 2014

I have multiple Java classes that holds lots of time based parameters, e.g.: (simplified):

Java Code: class Engine {
float rpm;
boolean enabled;
int runningTime;

[code]....

Is there any simply solution to map all these parameters from my TimeRecord to one list(vector,map) of params that user can choose from and see the value in proper form (floating type, boolean type) ?

I just want that user can pick a paremter's name from list or combobox and choose parameter that he wants to see. I don't want to duplicate the data - just some kind of mapping algorithm between list of parameters names and my TimeRecord. eg. some collection class that holds all names and connections: Parameters params; fill combo/list from this class for ( String name : params ) { combo.add(name);}, then when user picks some field from combo: params.getValue( myTimeRecord, name ) return correct parameter value (just my first thought)

Later, I want to replace all floats,ints,booleans with some kind of templated class eg. Value which will hold all kinds of measurement unit with conversion support - but it's only a plan now.

View Replies View Related

How To Have Multiple Classes But Only One Main Method

Nov 1, 2014

I was reading the book, "Head First Java" and it was talking about how there may be multiple classes in a large application, but there will be only one main method.how does it work that way? How can you have multiple classes, but only one main method?

View Replies View Related

Connecting Multiple Classes In Same Package

Jan 16, 2015

I have multiple classes in one package and was wondering how to I use one class while in another?

What I mean is I have one sheet with a class and another sheet with another class. (Separate tabs in Eclipse).

View Replies View Related

Swing/AWT/SWT :: Splitting GUI Into Multiple Classes?

Sep 12, 2014

I'm trying to create a Java swing chess application, but would like to divide the GUI part of it into at least two different classes (possibly more later, but I'm not sure yet). Currently I have a mainGUI class and a ChessBoard class, both of which extend JFrame. I want the main GUI class to contain a JPanel which will house several components, including a JPanel originating from the chessboard class that contains the actual board.

Is there any way to do this? I tried just creating a ChessBoard object in my main GUI class and then adding it to the component, but I got an illegal argument exception, because apparently you can't add one JFrame to another. How to do what I'm trying to do, or just how to split GUIs into multiple classes in general?

View Replies View Related

Java Does Not Support Multiple Inheritance Using Classes

Aug 16, 2014

I studied that java does not support multiple inheritance using classes. It is fine practically, but I have still a question in mind:Lets see the below code.

class Test{
------
------
}

class MyTest extends Test{
---------
---------
}

Here, as we know that that Object is Super class for every class in java. and I have already extends a Test class in MyTest.My question is: How is it possible to extend features of two class at the same time?

Theoretically,

MyTest extends Object &
MyTest extends Test.

View Replies View Related

Deck Card Game - Creating Classes For Code

May 17, 2014

I created the below code and it works fine. It is a deck card game. I just want to divide my code into classes so that it seems more organized.

Java Code:

package getimage;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.*;

[code]....

View Replies View Related

Creating New Data Type VeryLargeInteger With / Without Premade Java Classes

Jan 22, 2014

What I'm doing about it: googling the shit out of my problems, consulting you fine readers, consulting my friends, and yesterday I signed up for Lynda.com. I'm hoping 30hrs+ or so of watching, rewatching, and analyzing the example code will catch me up before I get too behind in CS302

** Assignment Prompt **

Integer types are very convenient, but their limited width and precision makes them unsuitable for some applications where precision is more important than speed. Develop a class VeryLargeInteger that can handle arbitrary long integer numbers (both negative and positive) and the basic arith- metic operations (addition, subtraction, multiplication, division, and remainder).

Hint: The number could be represented as string, the sign could be represented either as boolean or as part of the string.

Note: Implementations of addition/subtraction through repeated use of a constant incremen- t/decrement will not be accepted. Implementations of multiplication and division that rely only on addition and subtraction will not be accepted.

I know I'm going to have to create a separate tester to call on the VeryLargeInteger class and it's math methods. For the new data type, should I convert the integer/string into an array in order to handle the large length of the number? I know he wants us to use recursion for the math methods. My gut tells me addition and subtraction will be slightly easier than multiplication and division. I know I'll have to reference the other methods for division. We aren't allowed to use the BigInteger class.

How I should construct any of the methods.

Java Code:

import java.util.ArrayList;
/**
∗ VeryLargeInteger (VLI) is a class for arbitrary precision integer computation
*/
public class VeryLargeInteger {
private int[] num1;
private int[] num2;
private int[] num3;

[code]....

View Replies View Related

Creating Multiple JTextFields In A Loop

Sep 18, 2014

I need to create a JFrame with a user given amount of text fields in the form of a matrix. Say the user inputs 5 rows and 5 columns, I need to read those ints and create a 5 by 5 box of JTextFields in matrix form (i.e. 25 total text boxes in the form of a box). Here is some of the code I have been trying to use to do this...

int x = 10;
int y = 10;
for(int i = 0; i<rowSize; i++){
y= y+40;
for(int k =0; k<colSize; k++) {
newField = new JTextField("0");
newField.setBounds(x,y,40,20);
win2.add(newField,0);
win2.repaint();
x= x+60;
}
}

In order to go through the loop a given amount of times and create that amount of text fields in the correct places.Am I even close to doing this right?? Cuz I can't get the text fields to even show up on my window.

View Replies View Related

Creating Array With Multiple Data Type?

Oct 10, 2014

i need to create an array with attributes name, gender, phone, age.and then sort acording to age in ascending order.

i created like this,

public class Array{
private String[] name={"ram", "katy", "priti", "john"};
private String[] gender={"male","female","female","male"};
private int[] phone={989898089,89898989,8982989089,898908989};
private int[] age={45,24,30,28};
  public void printarray(){

[code]....

This code sorts the age attribute alone and when printing ouput it swaps one person's age to other person, how to make it correct

View Replies View Related

Creating A Quiz In Java With 3 Multiple Choice Questions

Oct 20, 2014

I have to write a short quiz that consists of 3 multiple choice questions. When the question is answered wrong I have to get it to loop back and ask the same question until the question is answered right.

I think I have the majority of my variables assigned and I have the scanner set up.

System.out.println("Question 1- Which TV sitcom recently had it's 20th anniversary?");
System.out.println("1. Two and a half men " + question1 + ".Friends 3.The Fresh Prince of Belair h.Get a hint");
answer2 = stdIn.nextInt();
if (answer2 != question1) {
System.out.println("Your answer was incorrect, please try again or pick h for a hint");
} else {
}

View Replies View Related

JSF :: Multiple Tool Tips To Be Displayed On Same Page When Click Multiple Image Links

Dec 8, 2014

I have i am trying to implement tooltip through javascript, like when we click on an image link tooltip should be displayed and it should have close button/ close image to close that tooltip.like the same way i will have multiple images on page, when ever i click on the images all tooltips should be displayed on the page when ever i want to close that then only it should close through close button on tooltip.can we do it through java script or will go for jquery.

View Replies View Related

How To Call Classes Within Other Classes

Apr 18, 2014

How do you call classes within other classes? Or can you only call classes through the main?

View Replies View Related

Passing Values To Two Classes And Retrieving Values From Those Classes

Feb 14, 2015

I'm doing an aggregation exercise that's suppose to find the volume and surface area of a cylinder. What I'm trying to do is pass values from one class, to a second class, and that second class passes values to a third class.

This may be a clearer explanation: The first class is the main program which sends values to the second and third class. The second class is used do calculations for a circle (a pre-existing class from another assignment). The third class grabs the values that the second class calculated and calculates those values with the one that was passed from the first class to the third class. The first class then prints the outcome.

Problem is when the program gets to the third class, it just calculates the value from the first class with the default constructor from the second class. It's like the second class never received the values from the first class. I think I'm missing a step, but I don't what it is.

First Class:

package circle;
import java.util.Scanner;
public class CylinderInput
{
static Scanner in = new Scanner(System.in);
public static void main(String[] args)
{
//user defined variable

[Code]...

View Replies View Related

Inner And Anonymous Classes

Dec 26, 2014

Why do we get such output or which line of code produces the output?

1class Egg2 {
2 protected class Yolk {
3 public Yolk() { print("Egg2.Yolk()"); }
4 public void f() { print("Egg2.Yolk.f()");}
5 }
6 private Yolk y = new Yolk();

[Code] ....

/* Output:

1.)Egg2.Yolk()
2.)New Egg2()
3.)Egg2.Yolk()
4.)BigEgg2.Yolk()
5.)BigEgg2.Yolk.f()
*///:~

View Replies View Related

How To Get Two Classes To Get Objects From Each Other

Oct 18, 2014

I'm quite new to Java. I have some trouble with understanding how to get two classes to get objects from each other (if that is the correct term).

Lets say I have a login class, in which I have a method checking what the user has entered into the console (I have not displayed the whole code, but this class works as it should and give the user an option to enter username and password and return it true if entered correct).

public static boolean validateUserPass(String userName, String password) {
String[] user = {"admin"};
String[] passwords = {"firkanten"};
boolean check = false;
for (int i = 0; i < user.length; i++) {
if (userName.equals(user[i])) {
if (password.equals(passwords[i])) {
check = true;

Now, in another class I want a display box to appear on the screen and give the user three options: Click yes, no or cancel. I get this to run perfectly alone, this is not the hard part for me. I want the display box only to appear when the correct username and password is given, but I can't seem to figure out how to do this probably.

View Replies View Related

Using Same Variable Name In Two Classes

Feb 7, 2014

Regarding the code examples in Head First Java, this is from Chapter 5, regarding the beginning creation of the dot com game. There are two classes in quesiton

the first is the SimpleDotComTester class:
public class SimpleDotComTester {
public static void main(String[] args)
{
SimpleDotCom dot = new SimpleDotCom();
int[] locations = {2, 3, 4};
dot.setLocationCells(locations);
String userGuess = "2";
String result = dot.checkYourself(userGuess);
}
}

and the second one is the code for the checkYourself () method in the SimpleDotCom class

public class SimpleDotCom {
int[] locationCells;
int numOfHits = 0;

public void setLocationCells(int[] locs)

[code]....

Now I noticed that both classes use a variable called result; the program runs fine, but assume from the example that you can use the same variable name two different classes;

View Replies View Related

How To Design Classes

Mar 29, 2014

design a class to conduct a survey of three hospitals. you want to know how many sectors (eg operation, children, gastronomic) each hospitals have, and how many doctors there are in each sector.

what is the process of class design?

View Replies View Related

Integrating 2 Different Classes

Jan 5, 2014

I have a Date class and Time class. Is it possible to pass Time object inside Date constructor so that toString function gives output as 12/05/2013 06:31:30 ?

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved