2 Replies - 106 Views - Last Post: 13 September 2012 - 10:37 AM Rate Topic: -----

#1 braincofe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-July 12

how to add 2 number using java mvc framework...

Posted 13 September 2012 - 10:09 AM

Hello fellow....I'm quite new here
I starting right know studied java mvc framework this is my first coding..i just wondering what's wrong for my coding i just add the 2 input integer and add it but the output display is zero...

so far this is my code

class Main{

	public static void main(String [] args){

		ContactModel model = new ContactModel();
		ContactView view = new ContactView(model);
		ContactController controller = new ContactController(model,view);
		}
	}

class ContactModel{

	private int num1;
	private int num2;

	public int getNum1(){
		return num1;
		}

	public void setNum1(int num1){
		this.num1=num1;
		}

	public int getNum2(){
		return num2;
		}

	public void setNum2(int num2){
		this.num2=num2;
		}

	public String Display(){         //is it ok if I use String instead of int?
		return "Sum: "+(getNum1()+getNum2());
		}
	}

class ContactView{

	ContactModel model;

	public ContactView(ContactModel model){
		this.model=model;
		}

	public void num1(){
		print("enter num1: ");
		}

	public void num2(){
		print("enter num2: ");
		}

	public void display(){
		println(model.Display());
		}

	public void print(String str){
			System.out.print(str);
		}

	public void println(String str){
		System.out.println(str);
		}
	}

import java.util.Scanner;

class ContactController{

	ContactModel model;
	ContactView view;

	public ContactController(ContactModel model, ContactView view){
		this.model=model;
		this.view=view;
		view();
		}

	public void view(){

		ContactModel insert = new ContactModel();

		view.num1();
		insert.setNum1(IntegerInput());

		view.num2();
		insert.setNum2(IntegerInput());

		view.display();
		}

	static  int IntegerInput(){
		Scanner sc = new Scanner(System.in);
		int input= sc.nextInt();
		return input;
		}


	}




thanks advanced

sorry for my newbiesness....

Is This A Good Question/Topic? 0
  • +

Replies To: how to add 2 number using java mvc framework...

#2 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: how to add 2 number using java mvc framework...

Posted 13 September 2012 - 10:27 AM

Not sure why you are creating a new ContactModel in the view() method. You've already passed a reference to a model, so use that one

public void view(){

		//ContactModel insert = new ContactModel();

		view.num1();
		int aNum1 = IntegerInput();

		model.setNum1(aNum1);

		view.num2();
		int aNum2 = IntegerInput();
		model.setNum2(aNum2);

		view.display();
	}


Edit: sorry for the extra variables. Just something I do while debugging, just take note of the change from insert to model

This post has been edited by SwiftStriker00: 13 September 2012 - 10:29 AM

Was This Post Helpful? 0
  • +
  • -

#3 braincofe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-July 12

Re: how to add 2 number using java mvc framework...

Posted 13 September 2012 - 10:37 AM

thank you very so much..... :bananaman: :bananaman:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1