Creating a Main method tester - java

I am trying to create a tester method to display a list.
How do I create a CongressStats object to exercise printPartyBreakdownInSenate method?
package congress;
import java.util.List;
public class CongressMain {
public static void main(String[] args){
}
}
public class CongressStats
{
private int congressNum;
/**
* Create a CongressStats object for the given congress number
*/
public CongressStats(int congressNum)
{
this.congressNum = congressNum;
}
/**
* Calculate and print the number of Democrats, Republicans, and Independents in this Senate
*/
public void printPartyBreakdownInSenate()
{

ok in your main function you would make an object of your class CongressStats.
public class CongressMain {
public static void main(String[] args){
CongressStats cs = new CongressStats(somenumhere);
cs.printPartyBreakdown(); //call your function
}
}
hopefully that helped.

Related

I have assigned value to an array in one class but array shows null in another class, why? (Fraction of code is provided)

public class Graph extends Framework {
Graph graph;
Mapper[] all_mappers=new Mapper[Framework.Mapper];
public void createGraph(Graph graph)
{
for(int i=0;i<Framework.Mapper;i++)
{
all_mappers[i]=new Mapper((double)5);
System.out.println("For mapper "+(i+1)+" following are parameters: ")
System.out.println();
}
}
}
class Mapper {
Mapper mp;
public double size;
Mapper(double size)
{
this.size=size;
}
}
public class Engine {
Engine en;
public void send_mapper(Engine en)
{
for(int i=0;i<Framework.Mapper;i++)
{
Graph array=new Graph();
System.out.println("This is the array:"+array.all_mappers[i]);
}
}
}
public class Framework {
public static int Mapper;
/**
* #param args
* #throws IOException
*/
public static void main(String[] args) throws IOException {
//Loading config.properties file.
Properties prop=new Properties();
FileInputStream ip=new FileInputStream("config.properties");
prop.load(ip);
Mapper= Integer.parseInt(prop.getProperty("Mappers"));
//Calling on function in Graph class
Graph g=new Graph();
g.createGraph(g);
//Calling on function in Engine class
Engine en=new Engine();
en.send_mapper(en);
}
}
On displaying array of objects all_mappers[i].size it shows null while size of all_mappers[i] has been defined by calling the constructor of mapper class. I want to display the size of each mapper through engine class and define size of each mapper in graph class.
Basic points to be remember and follow the standard while writing the java code.
1. Don't name any variable strating with Capital letter like you named Mapper as int. If you are going to declare static varibale name like MAPPER
2. Don't extend the class unless it is very necessary or you are going to follow the OOPS concept i.e class has IS-A or HAS-A releationship. I did not get the point why you have extends Framework in class Graph Ex- **public class Graph extends Framework**
3. If you are going to initialize the size you should always declare as int. I am not sure why you have written constructor like **Mapper(double size)**
So above discussion was regarding basic standard to code in Java. Now I will come to your question why you are getting null as value because when you are calling send_mapper(en) by passing Engine object you are again creating Graph object which will initialize nothing as there is no constructor.
Call below code from Framework class as below.
Graph g=new Graph();
g.createGraph(g);
Engine en=new Engine();
en.send_mapper(en,g);
And edit Engine class method as below
public void send_mapper(Engine en,Graph graph)
{
for(int i=0;i<Framework.MAPPER;i++)
{
System.out.println("This is the array:"+graph.all_mappers[i].size);
}
}
**Corrected Code**
package test;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class Framework {
public static int MAPPER;
/**
* #param args
* #throws IOException
*/
public static void main(String[] args) throws IOException {
//Loading config.properties file.
Properties prop=new Properties();
FileInputStream ip=new FileInputStream("config.properties");
prop.load(ip);
MAPPER= Integer.parseInt(prop.getProperty("mappersSize"));
//Calling on function in Graph class
Graph g=new Graph();
g.createGraph(g);
//Calling on function in Engine class
Engine en=new Engine();
en.send_mapper(en,g);
}
}
************************************
public class Mapper {
public double size;
Mapper(double size)
{
this.size=size;
}
}
*************************************
public class Graph{
Mapper[] all_mappers=new Mapper[Framework.MAPPER];
public void createGraph(Graph graph)
{
for(int i=0;i<Framework.MAPPER;i++)
{
all_mappers[i]=new Mapper((double)5);
System.out.println("For mapper "+(i+1)+" following are parameters: ");
System.out.println();
}
}
}
******************************
public class Engine {
//Engine en;
public void send_mapper(Engine en,Graph graph)
{
for(int i=0;i<Framework.MAPPER;i++)
{
//Graph array=new Graph();
System.out.println("This is the array:"+graph.all_mappers[i].size);
}
}
}
I have just formatted the code and removed unnecessary code. Basic thing we have passed Graph object in Framework class while calling Engine method send_mapper

"Multiple markers at this line" error at constructor

So I am using gpdraw as a library to draw stuff for my computer science class, and I'm trying to run this in Eclipse and I put the main method but I'm still getting errors.
import gpdraw.*;
public class House {
public static void main(String[] args) {
private DrawingTool myPencil;
private SketchPad myPaper;
public House() {
myPaper = new SketchPad(500, 500);
myPencil = new DrawingTool(myPaper);
}
public void draw() {
myPencil.up();
myPencil.turnRight(90);
myPencil.forward(20);
myPencil.turnLeft(90);
myPencil.forward(20);
myPencil.turnRight(20);
myPencil.forward(200);
}
}
}
You're trying to stuff everything into the main method. That won't work. Instead, have main call draw (on an instance of the class, a context which a static method does not have available) and define everything in the class, not a method.
import gpdraw.*;
public class House {
public static void main(String[] args) {
House instance = new House();
instance.draw();
}
private DrawingTool myPencil;
private SketchPad myPaper;
public House() {
myPaper = new SketchPad(500, 500);
myPencil = new DrawingTool(myPaper);
}
public void draw() {
// stuff
}
}
Java does not allow nesting methods and/or constructors.
You need something like this:
import gpdraw.*;
public class House {
private DrawingTool myPencil;
private SketchPad myPaper;
public House() {
myPaper = new SketchPad(500, 500);
myPencil = new DrawingTool(myPaper);
}
public void draw() {
myPencil.up();
myPencil.turnRight(90);
myPencil.forward(20);
myPencil.turnLeft(90);
myPencil.forward(20);
myPencil.turnRight(20);
myPencil.forward(200);
}
public static void main(String[] args) {
// whatever
}
}

How to define Global variable outside class?

I'd like to define variable which can share values to classes.
so I tried as following.
But It occured error.
How to share a value to classes?
package com.company;
/////// Error occurred ///////
int sharedValue = 100; // <- How to share to classes?
//////////////////////////////
public class Main {
public static void main(String[] args) {
sharedValue += 10;
GlobalTest globalTest = new GlobalTest();
globalTest.printGlobalValue();
}
}
class GlobalTest {
void printGlobalValue() {
System.out.println(sharedValue);
}
}
You can declare it as a static value in your class:
public class Main {
public static int sharedValue = 100;
....
}
and access it from other classes using:
Main.sharedValue
use static in a class instead
public class Blah {
public static int a = 100;
}
can be accessed by Blah.a
You can also add public getter method to access sharedValue as below:
public class Main {
private int sharedValue;
public void setSharedVallue(int sv)
{
sharedValue=sv;
}
public int getSharedvalue()
{
return sharedValue;
}
// Other code.
}

no main classes not found

I have few classes in my project.the main class should me SMSMain.But when i run the project it gives no main class found error.I tried to set in in properties(Netbeans) but it doesn't find any.where am I going wrong?here is my code:
package
na.edu.pon.oop210s.s12012.s211045888.sms;
/**
* Student Number: <211045888>
* Date: 3/11/12 7:47 pM
* Exercise: <Exercise 4>
* Created using: <netbeans>
*/
public class sms {
class Student
{
int studentID;
String studentName,course;
public void setName(String studentName){
this.studentName = studentName;
}
public void setNewId(int studentID){
this.studentID = studentID;
}
public void setCourse(String course){
this.course = course;
}
public String toString(){
return studentID +" "+studentName.toString();
}
class lecturer{
int staffID;
String staffName,taughtCourses;
public void setName(String staffName){
this.staffName = staffName;
}
public void setNewId(int staffID){
this.staffID = staffID;
}
public String toString(){
return staffID +" "+staffName.toString();
}
class course{
String code,description;
double units;
course(String code, String CD){
this.code=code.toUpperCase();
description=CD.toUpperCase();
}
public String getCode(){
return code;
}
/**
* #param args the command line arguments
*/
public class SMSMain {
public void main(String[] args) {
// TODO code application logic here
Student a = new Student();
a.studentName = "Maria";
a.studentID = 1236;
System.out.println("Student Name:" + a.studentName);
System.out.println("Student Name:" + a.studentID);
}
}
}}}}
You need to use
public static void main(String[] args){/* ... */}
with the static keyword.
Also, put your class SMSMain in it's own file SMSMain.java. Then import the needed classes (ctrl-shift-o in Eclipse).
Try to always put a class in it's own file, unless you've got a really good reason not to.
You main class must be static.
Like this:
public static void main(String[] args)
{
// main goes here
}
Try replacing public void main(String[] args) { with public static void main(String[] args) {
Your coding style is confusing. Learn the Oracle/Sun Java coding standards.
The main method has to follow the exact signature and be associated with the public outer class, not one of the inner classes.
package na.edu.pon.oop210s.s12012.s211045888.sms;
/**
* Student Number: <211045888>
* Date: 3/11/12 7:47 pM
* Exercise: <Exercise 4>
* Created using: <netbeans>
*/
// bad naming. What's sms? student management system? sado-masochistic society?
public class sms {
public static void main(String [] args) {
}
}

Simple App Won't Compile in Eclipse (with plugin)?

my code, being practically identical to the code given in BlackBerry's tutorial, has a syntax error in Eclipse. i'm sure there is some small but i'm just not seeing, but my coworker could not find it as well. any ideas would be greatly appreciated. thanks!
Code:
pushScreen(new ABCScreen());
Error:
Cannot make a static reference to the
non-static method pushScreen(Screen)
from the type UiApplication
here is the complete source:
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class AwesomeBBCalculator extends UiApplication {
public AwesomeBBCalculator() {
AwesomeBBCalculator app = new AwesomeBBCalculator();
app.enterEventDispatcher();
}
public static void main(String[] args) {
pushScreen(new ABCScreen()); // ERROR LINE
}
}
final class ABCScreen extends MainScreen {
public ABCScreen() {
super();
// add title
LabelField title = new LabelField("Awesome BlackBerry Calculator",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
}
public boolean onClose() {
Dialog.alert("Thanks for using the Awesome BlackBerry Calculator!\nGoodbye.");
System.exit(0);
return true;
}
}
The pushScreen method can only be called within an instance of UiApplication. You are trying to call it from a static main method. That does not work. Do this instead...
public void foo()
{
pushScreen(this);
}
public static void main(String[] args)
{
(new ABCScreen()).foo();
}
public void class1()
{
pushScreen(this);
}
public static void main(String[] args)
{
(new NewScreen()).class1();
}
try making an object for the ABCScreen class and then use it or u may try this also:
UiApplication.getUiApplication().pushScreen(new ABCScreen());

Categories