|
Please do not repost the same question.
|
|
|
|
|
Please can someone assist in debug the below code. I am trying to add multiply and division method to the code whereby both methods can accepts two parameters and return a value.
class MathOP{
double num1;
double num2;
//create a method mathadd
double mathadd(){
return num1 + num2;
}
// create a method mathsub
double mathsub(){
return num1 -num2;
}
//set value of mathop for test
void setVal(double x, double y) {
num1 = x;
num2 = y;
}
}
/************************
This is a flower box
Name:
Program name:
Date:
Purpose:
**************************/
class TestMathOP {
public static void main (String args[]) {
MathOP mytest1 = new MathOP();
MathOP mytest2 = new MathOP();
MathOP mytest3 = new MathOP();
double Mathadd;
double Mathsub;
double Mathadd1;
//intitialize each text
mytest1.setVal(5.5, 7.5);
mytest2.setVal(5.5, 7.5);
mytest3.setVal(5.5, 7.5);
//get values
Mathadd = mytest1.mathadd();
System.out.println("The sum of two number is " + Mathadd);
//get values of subtraction
Mathsub = mytest2.mathsub();
System.out.println("The subtraction of two number is " + Mathsub);
boolean Y = true;
System.out.println("Do you want to exist (Y/N)?");
if (Y) {
System.out.println("Thanks for using our system");
}else{
System.out.println("run the program again");
}
}
//Modify the code to prompt for the operation
class TestMathOP1 {
void main (String args[]) {
MathOP mytest3 = new MathOP();
double Mathadd1;
Mathadd1 = mytest3.mathadd();
System.out.println("Enter operator + " + Mathadd1);
System.out.println("Do you want another operation (Y/N)?");
}
}
}
modified 23-Feb-15 20:40pm.
|
|
|
|
|
what u want, i didn't understand bro, your code is running well except run the program again.. call
if u have problem then specify correctly
|
|
|
|
|
I can seem to get the other part of the class to run. I mean after the "run the program again".
|
|
|
|
|
double mathadd(){
return num1 + num2;
}
Your methods should accept local parameters rather than having some other method set them in advance, as shown below:
double mathadd(double num1, double num2) {
return num1 + num2;
}
The same with the subtraction method. You then call it with a stament like:
double result = mytest1.mathadd(5.5, 7.12);
The setval method is redundant and should not be used. You also need to import the MathOP class into your test application, rather than trying to define them in your source.
Take a look at http://docs.oracle.com/javase/tutorial/java/index.html[^] for a more detailed explanation of how to write Java.
|
|
|
|
|
Thanks for responding to my post but I am still have issue compiling the code. Can you see what I am doing wrong in the below code?
package display;
class MathOP{
double num1;
double num2;
double mathadd(){
return num1 + num2;
}
double mathsub(){
return num1 -num2;
}
class MathOP2 extends MathOP {
double mathmul(){
return num1 * num2;
}
double mathdiv(){
return num1 / num2;
}
MathOP2 objMath = new MathOP2();
double return = objMath.mathadd(5.5,5.12);
System.out.println("MathAdd" + Mathadd);
double return = objMath;mathsub(42.0,20.2);
System.out.println("MathSub" + Mathsub);
double return = objMath;mathdiv(60.3,20.2);
System.out.println("MathMul" + Mathmul);
double return = objMath;mathmult(10.4,5.4);
System.out.println("MathDiv" + Mathdiv);
}
}
|
|
|
|
|
Your methods in MathOP and MathOP2 are not using method parameters. They should be of the form:
double mathadd(double num1, double num2){
return num1 + num2;
}
As I suggested in my previous message, you should go and work through the Java tutorials.
|
|
|
|
|
//solv by akshgods
/*
i deleted many thing which is not required in your program,
so always try to make simple and easy dear.
sorry for late reply
*/
package javaapplication8;
import java.util.*;
class MathOP{
//create a method mathadd
double mathadd(double a,double b){
return a+b;
}
// create a method mathsub
double mathsub(double x,double z){
return x-z;
}
}
//TestMathOp class
class TestMathOP {
public static void main (String args[])
{
MathOP mytest1 = new MathOP();
MathOP mytest2 = new MathOP();
double add;
double sub;
/*
scanner class for taking integer input
*/
Scanner sc=new Scanner(System.in);
int f=0;
while(f==0)//while for reapitation of program
{
add = mytest1.mathadd(5.5,7.5);
System.out.println("The sum of two number is " +add);
//get values of subtraction
sub = mytest2.mathsub(5.5,7.5);
System.out.println("The subtraction of two number is " +sub);
System.out.println("Do you want to exist (Y=1/N=0)?");
f=sc.nextInt();//here we are taking value from keybord 1 or 0 for exit or reapeat programm
if (f==1)
{
System.out.println("Thanks for using our system");
break;
}
else
{
System.out.println("run the program again");
}
}
}
}
|
|
|
|
|
Please format your code properly when posting answers.
|
|
|
|
|
what to format ? an how? give example with my post
|
|
|
|
|
Like this
double mathsub(double x,double z){
return x-z;
}
surrounding your code with <pre> tags, by using the code button on the edit window.
|
|
|
|
|
Today I was testing the ArrayList of the Java because I've just started learning the Java after C#. Concept that underlies is the generic, similar to the C#, API is somewhat similar.
But there was a problem, that I faced, I started with the String class objects in the ArrayList and I found surprising that the list allows all type of objects in it. For example the following code from my HelloWorld application.
List list = new ArrayList<String>();
list.add(new JavaHelloCollections());
list.add("String data");
list.add(4345345345.9898996756);
for(Object o : list) {
System.out.println(o);
}
Well, firstly it compiles all fair and doesn't only compile it also prints the result; a little doubty result for the float (or double) type data.
javahellocollections.JavaHelloCollections@529e0c79
String data
4.3453453459899E9
I am using NetBeans and I am really confused of how can this happen. Does JVM perform some .toString() checks there or what? Where actually does this type check or type conversion happens?
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
What you have actually created is a simple List that will accept any type. Try
ArrayList<String> list = new ArrayList<String>();
|
|
|
|
|
+1 Thanks Richard.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Afzaal Ahmad Zeeshan wrote: Where actually does this type check or type conversion happens?
Although the previous comment pointed out your code problem it didn't answer this question.
The answer is that this occurs in the compiler and only in the compiler. Quite possible and easy to inject something wrong into it if one is not careful or one just wants to circumvent it.
One will often run into this with situations like the following.
void method(List<String> list) { ... }
void method(List<Integer> list) { ... }
Although these look like two different overloads after the compiler gets done with them the signature will resolve to exactly the same overload (the compiler will throw an error about that.)
|
|
|
|
|
Yeah that makes sense. Thanks for your clarification. Now it does make sense for why did I get problems. But, isn't the List generic? Why would it throw error for attaching a type to the List?
Secondly, I am still just learning and not building any application so "careless" won't apply, I was trying to make the application throw error but it accepted it that cause me to ask the question here. I will continue trying more such "careless" attempts in order to learn more.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Afzaal Ahmad Zeeshan wrote: Why would it throw error for attaching a type to the List?
Not sure what you mean by "throw error" but the compiler validates types for generics and will produce a compiler error when it finds those.
Other than that it is possible to put the wrong type in a list because the compiled version doesn't have a type. An error will occur later because as the compiler produces code it puts in checks which impact what happens when you get something out of the list. And one of those checks will fail. Seeing such a error isn't a good thing since it can be very confusing.
|
|
|
|
|
need a simple java code for transport ticketing
|
|
|
|
|
|
That depends on the level of the project. You can create a single page application with a single
System.out.println("Ticket booked!");
or you can create an entire application for keeping the ticket records, user records and other transport means information and so on. But first, come up with a basic algorithm and the code that you've tried; atleast the algorithm. Asking a one-liner question doesn't help you or us.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Greetings, everything in this code works except when the i input the weight and height the result is not shown in the textbox (v_text).. please help me
package bmicalculator;
import static com.sun.corba.se.impl.orbutil.CorbaResourceUtil.getText;
import java.lang.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JPanel;
import javax.swing.JFileChooser;
import java.awt.Container;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
public class BMICalculator extends JFrame implements ActionListener, PropertyChangeListener
{
JPanel p_format;
JPanel p_text;
JLabel f_name;
JLabel height;
JLabel weight;
JTextField n_text;
JFormattedTextField h_text;
JFormattedTextField w_text;
JFormattedTextField v_text;
JTextField r_text;
JTextArea a_text;
JButton v_bmi;
JButton result;
JButton r_done;
JButton r_save;
JButton r_reset;
JButton f_open;
JFileChooser chooser = new JFileChooser();
String Height, Weight;
double BMI;
static String output = "Results";
static int jopIcon = JOptionPane.QUESTION_MESSAGE;
boolean bFlag = true;
Font font;
static int f_size = 16;
static int style = Font.PLAIN;
static String font_name = " Harrington ";
double number1;
double number2;
public BMICalculator (String s)
{
super(s);
font = new Font(font_name, style, f_size);
getContentPane().setLayout(new GridLayout(1,2));
p_format = new JPanel();
p_text = new JPanel();
p_text.setLayout(null);
f_name = new JLabel(" Name: ");
f_name.setSize(120,60);
f_name.setLocation(30, 30);
f_name.setForeground(Color.DARK_GRAY);
n_text = new JTextField(40);
n_text.setSize(120,60);
n_text.setLocation(70,30);
height = new JLabel(" Height(meter) : ");
height.setSize(90, 60);
height.setLocation(30,60 );
height.setForeground(Color.RED);
h_text = new JFormattedTextField();
h_text.setColumns(10);
h_text.setLocation(70, 60);
h_text.setValue(new Double(number1));
h_text.addPropertyChangeListener("value", this);
weight = new JLabel(" Weight(kilogram): ");
weight.setSize(90, 60);
weight.setLocation(30, 90);
weight.setForeground(Color.RED);
w_text = new JFormattedTextField( );
w_text.setColumns(10);
w_text.setLocation(70, 90);
w_text.setValue(new Double(number2));
w_text.addPropertyChangeListener("value", this);
v_bmi = new JButton("Your BMI: ");
v_bmi.setSize(90, 60);
v_bmi.setLocation(30,190);
v_bmi.setForeground(Color.BLUE);
v_bmi.addPropertyChangeListener("value", this);
v_bmi.addActionListener(this);
v_text = new JFormattedTextField( );
v_text.setColumns(10);
v_text.setLocation(70, 190);
v_text.setValue(BMI);
v_text.addPropertyChangeListener("value", this);
result = new JButton("Your level: ");
result.setSize(60, 60);
result.setLocation(30,250);
result.setForeground(Color.BLUE);
result.addActionListener(this);
r_text = new JTextField(45);
r_text.setSize(90, 60);
r_text.setLocation(70, 250);
r_text.addActionListener(this);
a_text = new JTextArea(30,45);
a_text.setSize(200,200);
a_text.setLocation(30, 300);
r_done = new JButton(" Done! ");
r_done.setSize(120,30);
r_done.setLocation(30, 350);
r_done.setForeground(Color.BLACK);
r_done.addActionListener(this);
r_save = new JButton(" Save ");
r_save.setSize(120,30);
r_save.setLocation(60, 350);
r_save.setForeground(Color.MAGENTA);
r_save.addActionListener(this);
r_reset = new JButton(" Reset ");
r_reset.setSize(120,30);
r_reset.setLocation(90, 350);
r_reset.setForeground(Color.RED);
r_reset.addActionListener(this);
f_open = new JButton(" Open ");
f_open.setSize(120,30);
f_open.setLocation(120, 350);
f_open.setForeground(Color.BLUE);
f_open.addActionListener(this);
JScrollPane textScrollPane=new JScrollPane(a_text);
textScrollPane.setSize(300,300);
textScrollPane.setLocation(30,300);
p_format.setBackground(Color.PINK);
p_format.add(f_name);
p_format.add(n_text);
p_format.add(height);
p_format.add(h_text);
p_format.add(weight);
p_format.add(w_text);
p_format.add(v_bmi);
p_format.add(v_text);
p_format.add(result);
p_format.add(r_text);
p_format.add(a_text);
p_format.add(r_done);
p_format.add(r_save);
p_format.add(r_reset);
p_format.add(f_open);
p_format.add(textScrollPane);
getContentPane().add(p_format);
}
public static void main(String args[])
{
BMICalculator k=new BMICalculator("SHARIMAH SAKKA Project");
k.setSize(550, 700);
k.show();
k.setVisible(true);
k.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void BMIResult()
{
Height = height.getText();
Weight = weight.getText();
number1 = Double.parseDouble(Height);
number2 = Double.parseDouble(Weight);
BMICalculate(number1, number2);
}
public void BMICalculate(double number1, double number2)
{
try
{
number1 = (double) ((Number)h_text.getValue());
number2 = (double) ((Number)w_text.getValue());
BMI = (number2 / ((number1) * (number1)));
}
catch (NumberFormatException nfe)
{
output += "\n\n Whoa! Input error: must enter valid integers";
jopIcon = JOptionPane.ERROR_MESSAGE;
}
}
void saveToFile()
{
if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION)
{
File saveFile = chooser.getSelectedFile();
try
{
PrintWriter out = new PrintWriter (new FileWriter(saveFile));
out.print (a_text.getText());
out.close();
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, "Could not save the file " + e.getMessage());
}
}
}
void loadFromFile()
{
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
File loadFile = chooser.getSelectedFile();
try
{
BufferedReader in = new BufferedReader (new FileReader(loadFile));
a_text.setText("");
String nextLine = in.readLine();
while (nextLine != null)
{
a_text.append(nextLine + "\n");
nextLine = in.readLine();
}
in.close();
}
catch (IOException ie)
{
JOptionPane.showMessageDialog(null, "Could not load the file " + ie.getMessage());
}
}
}
@Override
public void actionPerformed(ActionEvent x)
{
if(x.getSource()==v_bmi)
{
v_text.setValue(new Double(BMI));
}
if(x.getSource()==result)
{
r_text.getText();
if (BMI >= 29)
{
r_text.setText(" High Risk zone(UnHealthy).Please start losing weight!It's a MUST!!! ");
}
else if (BMI <= 25 || BMI < 28.9)
{
r_text.setText(" Moderate Risk zone.Please start going on diet and lose some weight! ");
}
else if (BMI <= 18.5 || BMI < 24.9)
{
r_text.setText(" Low Risk zone(Healthy).Hopefully you can maintain this way! ");
}
else if (BMI < 18.4)
{
r_text.setText(" Need to start eating more. Too skinny and unhealthy for your body. ");
}
}
if(x.getSource()==r_done)
{
String s= "Name: " + n_text.getText()+ "\n"+"Height: " + h_text.getText()+"\n"+"Weight: " + w_text.getText()+"\n"+"BMI: " + v_text.getText()+"\n"+"Result: " + r_text.getText();
a_text.setText(s);
}
if(x.getSource()==r_reset)
{
String s=n_text.getText() +h_text.getText()+w_text.getText()+ v_text.getText()+r_text.getText();
n_text.setText(null);
h_text.setText(null);
w_text.setText(null);
v_text.setText(null);
r_text.setText(null);
a_text.setText(null);
}
if(x.getSource()==r_save)
{
saveToFile();
}
if(x.getSource()== f_open)
{
loadFromFile();
}
}
@Override
public void propertyChange(PropertyChangeEvent x)
{
Object source = x.getSource();
if(source==v_bmi)
{
String b;
b = Double.valueOf(BMI).toString();
v_text.setValue(new Double(b));
}
}
}
|
|
|
|
|
Too much code, so I'm not going to look through all of it.
This caught my attention as "obviously wrong" though: else if (BMI <= 25 || BMI < 28.9) that's really just BMI < 28.9 , the other part is redundant. It's probably meant to be >= 25 ?
But then the logic also doesn't quite work, because clearly there is a gap between 25 and 24.9, a rather large gap at that - if I counted them correctly, there are 28147497671065 doubles that have a value strictly between 24.9 and 25, which implies that almost all of them won't be generated by sensible inputs, but see for example 90kg / 1.90m2.
Also, BMICalculate does some unexpected things, taking nice arguments and then ignoring them and pulling its values from some control.
|
|
|
|
|
which project i will do in third year. Now i am in second year Bs.C IT.......
Please suggest.........
|
|
|
|
|
Anything you can think of that is a) within your skill level, and b) a subject that you understand. If you are really stuck then ask your professor to allocate you something.
|
|
|
|
|
Write a compiler in java.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|