Create A Well-encapsulated Class?
Oct 3, 2014How can one create a well-encapsulated class?What are the principles to be followed?
View RepliesHow can one create a well-encapsulated class?What are the principles to be followed?
View RepliesHow can i define a class which defines the object which is encapsulated and the attributes protected from external access, and their value retrieved and defined by public methods in java...
View Replies View RelatedIn the class below I'm trying to create a class that will accept dates in various formats and create a range. The first constructor is easy because I send it the begin date and end date as Date objects. Now I want to send a month(and year) in a constructor and derive the begin and end dates from it. In my constructor that accepts the month/year I need to put the this(startDate, endDate) at the top to be allowed, but the parameters are not built yet.
package com.scg.athrowaway;
import java.util.Calendar;
import java.util.Date;
public class DateRange {
private Date startDate;
private Date endDate;
[code].....
How to create object for "class B" and call the "function_B" from other different class D where class D has no connection with class A? Here is my program.
public class A(){
void print(){}
}
class B{
void function_B(){}
}
class C{
void function_C(){}
}
Here, A, B, C are in the same package. But class D is in different package.
Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. There is also a MyDate class as explained below. A person has a name, address, phone number, and email address. A student has a status (freshman, sophomore, junior, or senior). Define the status as an integer which can have the value 0 (for "Freshman"),
1 (for "Sophomore"),
2 (for "Junior"), and
3 (for "Senior"),
but don't allow the status to be set to any other values. An employee has an office, salary, and dateHired. The dateHired is a MyDate field, which contains the fields: year, month, and day. The MyDate class does not explicitly inherit from any class, and it should have a no-arg constructor that sets the year, month, and day to the current year, month, and day. The MyDate class should also have a three-argument constructor that gets three int arguments for the year, month and day to set the year, month and day.
A faculty member has office hours and a rank. Define the rank as a String (for values like "Professor" or "Instructor"). A staff member has a title, which is also a String. Use data types for the fields as specified, or where one is not specified, use a data type that is appropriate for the particular field. Write a test program called TestEveryone.java that creates a Person, Student, Employee, Faculty, and Staff object, and invoke their toString() method (you don't need to call the objects' toString() method explicitly).
Note: Your MyDate.java class is the object class that your dateHired field is created from in the Employee.java class.
Do not use the Person, Employee or Faculty classes defined on pages 383 and 384 of the book. Create new ones.Here is the code I have so far concerning the employee and MyDate.
public class Employee extends Person {
private String office;
private double salary;
//private MyDate dateHired;
//7 argument constructor for employee
public Employee(String name, String phoneNumber, String email, String address, String office, double salary /*MyDate dateHired*/) {
super(name, phoneNumber, email, address);
[code]....
The assignment is to create a SmartString class that implements a SmartStringInterface class (created by professor) and implements a few methods. We are basically taking a string and then taking various substrings and inserting, deleting them and undoing changes as well. Here are the methods in the interface to use along with the parameters.
public interface SmartStringInterface {
public void insert(int pos, String sstring);
public void delete(int pos, int count);
public void undo();
public String toString();
The Undo is supposed to be able to be called multiple times (to be tested using a driver program that we must create) but the part that's got me is that the changes are only supposed to be stored. Currently, I am storing the "new" string after each change onto a stack, so that undo can just pop off the stack and it will revert to the previous string. Professor said that was wrong, so I don't know how to do it. Here is what I have so far (some of the code we have is using default StackADT stuff from our book, so if you need that I can post as well. You can see in the undo method where I currently save the string. We can use multiple stacks if needed, but the less the better. Must use at least 1. The exception code is already coded for us in another file also. I am only having to code these methods and the driver to test.
import java.util.Arrays;
public class ArrayStack<T> implements StackADT<T>
private final static int DEFAULT_CAPACITY = 100
private int top;
private T[] stack;
[code]...
I'm new to Java and I have an assignment to create a Sphere class that will allow you to create Sphere objects using the code below. Then create a program called SphereTester that prompts the user for the radii of two spheres in meters. The program should display which sphere is larger and by how many cubic meters and display only four digits after the decimal point. I have the sphere class given to us for the assignment which is this:
Java Code: public class Sphere {
// instance variable (i.e., a field)
private double radius;
// constructor for the Sphere class
public Sphere(double r) {
radius = r;
[code]....
Why we create a driver class?Instead of creating a driver class, if we want to compile our code so will it show output? Let say, we've created a class GradeBook of the institution for students.So they can easily view their profile information and scores in different semesters.so when we have created a class for this purpose, should we create a driver class or not?What is the big advantage of creating a driver class?
View Replies View RelatedCan I declare a class as
public static final?
Because I can declare a variable as public static final pi=3.14;
Why we can't create object of abstract class ,when we can create its constructor?
View Replies View RelatedI'm trying to get to grips with the AndEngine, so I've recently gotten hold of their Cookbook, but there's a section that's confusing. I'm using the Eclipse IDE with the section is asking that I create two inner classes to an ObjectFactory class. I'm not sure if what it wants me to do is doable in java or specifically in Eclipse. This is the section of code the book is asking me to create.
Java Code:
package com.example.helloworld;
public class ObjectFactory {
public static LargeObject createLargeObject(final int pX, final in pY){
return new LargeObject(pX,pY);
}
public static SmallObject createLargeObject(final int pX, final in pY){
return new SmallObject(pX,pY);
}
} mh_sh_highlight_all('java');
Now this is returning errors that I should declare these two as individual classes, but I feel like I am missing something. Earlier the book asks you to create this BaseObject class.
Java Code:
package com.example.helloworld;
public class BaseObject {
@SuppressWarnings("unused")
private int mX;
@SuppressWarnings("unused")
private int mY;
[code]...
It then mentions that the two classes LargeObject and SmallObject are inner classes of BaseObject and extends this class. The ObjectFactory class is meant to determine which subtype of the base class that needs, as well as define the objects properties.
I want to create a class that converts currency. I stored 2 conversion rate as constant (final) variables. In the main method, I ask the user to enter the amount in US dollars that they want to convert. Then I ask the user to enter the currencyType ("Bitcoin" or "Chuck E Cheese").
However I am stuck in the while loop:
I want to use a while loop that basically does this: If the user entered anything else ( not Bitcoin or Chuck E Chesse), then tell the user to try again, and keep reading Strings until they enter a correct currency type. If the user 3 consecutive incorrect currency types, then exit the program.
Take a look at what I have so far.. when I run the program it does not quite do what I want it to do Also, there's something wrong with one of my if statements and I want to figure out how to fix it.
Java Code:
import java.util.Scanner;
public class CurrencyConverter
{
public static void main(String[] args)
{
System.out.println("----------------------------------");
System.out.println(" Currency Converter");
System.out.println("----------------------------------");
[Code]...
This is how I want my project to look like:
I'm new to java and trying to learn the ropes on Netbeans!What I want to do is make a class which does the following
-Asks for and reads a first number on the command line.
-Asks for and reads a second number using a JOptionPane
-Asks for and reads your name using a JOptionPane
I want it to output the name and the remainder of the first number divided by the second number.
I know that System is a final class and it cannot be instantiated, out is a static final variable of type PrintStream in System class and println is a method in PrintStream class.Still I don't understand why we use System.out to call println() method.To my knowledge a method can be called using an object reference, in case of static behaviors we use classname. Then why here we are using System.out.println? Can't we just create an object of PrintStream class and call the println() method as PrintStream class can be instantiated.Are there any ways of calling a method apart from those I know(I have mentioned above what I know)?
View Replies View RelatedSo I am making a JPanel with two JLabels and it isn't working right.
import java.awt.*;
import javax.swing.*;
public class IntroPanel extends JPanel
{
public IntroPanel() {
setBackground(Color.green);
[Code] ....
For some reason
JLabel 11 = new JLabel("Layout Manger Demonstration");
JLabel 12 = new JLabel ("Choose a tab to see an example of a layout manger.");
add(11);
add(12);
is not registering the JLabel or add functions.
I'm learning about abstract classes and I have to create an abstract auto class with make and price of a car, then two classes with a different type of car, and finally a main to use them. Everything seems to work and when I run it it's fine but I do get an error on the main that I'm not using the local variable buick1 and acura1.I'm curious because, while it runs for me, I want to make sure I'm doing it right and don't know of another way to do the output than this. I've put all four classes but the issue is on the last one (5 and 7).
public abstract class Auto
{
protected String makeCar;
protected double priceCar;
public Auto(String newMake)
{
makeCar = newMake;
[code]....
I have an assignment to create a Sphere class that will allow you to create Sphere objects using the code below. Then create a program called SphereTester that prompts the user for the radii of two spheres in meters. The program should display which sphere is larger and by how many cubic meters and display only four digits after the decimal point. I have the sphere class given to us for the assignment which is this:
public class Sphere {
// instance variable (i.e., a field)
private double radius;
// constructor for the Sphere class
public Sphere(double r) {
radius = r;
[code]....
Here is a sample run of what the final result should look like
Enter the radius of a sphere (in meters): 1
Enter the radius of a 2nd sphere (in meters): 2
Sphere 2 is greater than Sphere 1 by 29.3215 cubic meters
Animal ob1=new Lion();
Here we create ob1 for Animal class or Lion class.
public abstract class Animal {
public abstract void eat();
public void breathe(){
[Code] ....
interface Interface{
class B{
}
}
I know that we can create an inner class inside an interface but i want to know that why we'll create an inner class inside an interface. I mean what is the use of creating inner class inside an interface and what is the advantage of it.
I want to create a Student class that is able to search, borrow, return, and reserve books. This is what I have done so far:
public class Student
{
private String username;
[Code].....
I think that I might need an array where to remember all of the books that the student has borrowed, but I'm not sure how to do that as well.
I'm curious why ACM isn't letting me create an object from a different class, maybe I am missing something?
my main class:
import java.awt.Color;
import acm.program.*;
import acm.graphics.*;
public class MyClass extends GraphicsProgram {
private static final int WINDOW_W = 500;
private static final int WINDOW_H = 500;
[code]....
I have also noticed when trying to extend 'ConsoleProgram' it does the same thing with line printing (except when using system.out).
I've been writing classes over and over for school. So I create a class outside of my main class. I create a new constructor and then create objects from my main class. I hope that makes sense. So i use methods in that class to work with the object. So I have an object name I've created <dot> method name. So I can create objects and then use methods from the class, but I'm wondering can I create a method in my main class and use it on that object? I don't understand how to do that.
View Replies View RelatedSo in the code below I create an instance of my own triangle class and use one of its methods. The thing is I use one of my triangle classes methods in a method other the main method of my main program so I'm thinking it can't access it?
Any way here's the code for my triangle class
import java.util.Scanner;
public class QudratullahMommandi_Triangle_06 {
Scanner keyboard = new Scanner(System.in);
private double side1;
private double side2;
private double side3;
[Code] ....
and here's the error message
QudratullahMommandi_S_06.java:46: error: cannot find symbol
{ triangle1.outPut();
^
symbol: variable triangle1
location: class QudratullahMommandi_S_06
1 error
I am new to java. While compiling the "moooooo" program an error is displayed.
Error -- error while writing MooseGreetings :MooseGreetings.class
<access denied>.
What to do?
I have a shop and I use visual studio.net to create an Inventory application to manage my shop. So I am familiar with vb.net codes.
Now, I'm trying to built another Inventory application for my shop using java, 'coz now I'm using Linux Ubuntu for my OS desktop and there's no visual studio.net in linux.
So in vb.net, there's a module that I use for database connection that I can call from any form that I needed.
I searched google for the equivalent module for java and there is none. But, there's an example that use a new java class.
Here's the code in MySQLConnect.java :
Java Code:
public class MySQLConnect {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/dbInventory";
static final String USER = "root";
static final String PASS = "root";
public static Connection ConnectDb() {
[Code] ....
Assignment:
Create a class; call it Lab4a that will have one method called pull. This method does not return anything and requires no parameters.
-In the method, create three random integers in the range 1 to 7. The method will then display the three numbers to the terminal window.
-Now create a driver program, called SlotMachine, to invoke the pull method of the Lab4a class.
-As a refresher, you will have a main method in the driver class that will create an object of Lab4a and then use the only method of this object.
-In your driver program, invoke the roll method 10 times.
-See the back of this lab for an example of the output.
This is what I have so far.
Slot machine
import java.util.Random;
public class Lab4a {
public static void main (String[]args) {
Random pull=new Random();
[code]....