Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Angel***

[Resolvido] Funções de uma Calculadora

Recommended Posts

Bom Dia,

estou implementando uma calculadora e tenho duas duvidas,

primeira: como fazer raiz quadrada e fatorial

segunda: como converto de binario para oct?

 

Se puderem me ajudar agradeço!!

 

segue uma parte do código

 

package Clau;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


public class Calculadora extends JPanel implements ActionListener {
  
	int aux;
	String operacao = "";
	double armazena = 0;  
	double calculo = 0;
   
 
	private static final long serialVersionUID = 1L;

	//  mostra os valores
	private static JTextField texto;
	
		// botões de 0 a 9
 		private JButton botao1, botao2, botao3,	botao4, botao5,
						botao6, botao7, botao8,	botao9, botao0;

 		//Calculos
		private JButton btvirgula, btfatorial, btpotencia,
		 				btraiz,  botaomais,	 botaomenos,
		 				btmulti, btdividir,  btponto;
							  
		private JButton btlimpar, btigual;
		
		//botoes com letras
		private JButton botaoA, botaoB,
						botaoC, botaoD,
						botaoE, botaoF;
		
		// botões de seleção
			private JRadioButton hex, dec,oct,bin;
			private ButtonGroup buttonGroup= new ButtonGroup();
		
				
		public Calculadora (){
			
			buttonGroup = new javax.swing.ButtonGroup();
			hex = new javax.swing.JRadioButton("hex");
			dec = new javax.swing.JRadioButton("dec");  
			oct = new javax.swing.JRadioButton("oct");  
			bin = new javax.swing.JRadioButton("bin"); 
		
		
			botao1 = new JButton ("1");
		   ......
			

			//tamanho da tela
			setPreferredSize (new Dimension (459, 234));
			setLayout (null);
			setBackground(Color.pink);		
			
			

			//adicionando os componentes na tela
			add (botao1); 
			........

			//tamanho e localização dos componentes 
			botao1.setBounds (100, 75, 45, 20);
		   .......
			 
			botao1.addActionListener( this );
			.....

		}

		public static void main (String[] args) {

			JFrame frame = new JFrame ("* * Calculadora * *");//nome 
			JFrame.setDefaultLookAndFeelDecorated(true);
			frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
			frame.getContentPane().add (new Calculadora());//chama a calc
			frame.pack();
			frame.setVisible (true);
			
			texto.setEditable(false);//não editavel o campo que mostra os numeros
			
	}

		
		public void actionPerformed(ActionEvent a) {
			double aux;
			Object obj = a.getSource();
			
			if (obj == botao1) texto.setText(texto.getText().concat("1"));
			if (obj == botao2) texto.setText(texto.getText().concat("2"));
			if (obj == botao3) texto.setText(texto.getText().concat("3"));
			if (obj == botao4) texto.setText(texto.getText().concat("4"));
			if (obj == botao5) texto.setText(texto.getText().concat("5"));
			if (obj == botao6) texto.setText(texto.getText().concat("6"));
			if (obj == botao7) texto.setText(texto.getText().concat("7"));
			if (obj == botao8) texto.setText(texto.getText().concat("8"));
			if (obj == botao9) texto.setText(texto.getText().concat("9"));
			
			if (obj == btvirgula)texto.setText(texto.getText().concat(","));
			if (obj == btponto) texto.setText(texto.getText().concat("."));
			if (obj == botaoA) texto.setText(texto.getText().concat("A"));
			if (obj == botaoB) texto.setText(texto.getText().concat("B"));
			if (obj == botaoC) texto.setText(texto.getText().concat("C"));
			if (obj == botaoD) texto.setText(texto.getText().concat("D"));
			if (obj == botaoE) texto.setText(texto.getText().concat("E"));
			if (obj == botaoF) texto.setText(texto.getText().concat("F"));	   
			
			
			 if (texto.getText().equals("")) { // equals => comparação de objetos  
							 aux = 0;  
						 } else {  
							aux = Double.valueOf(texto.getText());  
						 }  
				   
						 
						 if (obj == btvirgula) {   //virgula
							 if (aux == 0){  
								 texto.setText("0.");   
							 }
							 else{  
								if (((texto.getText()).indexOf(".")) ==-1){  
									 texto.setText(texto.getText()+".");  
				   
								 }  
							 }			 	   
						 }  
						 if (obj == botaomais) {  
										texto.setText("");  
										  armazena = aux;  
										  operacao = "+";  
							}	 
						
			
		 }	
		

}

}

Muito obrigada!

Compartilhar este post


Link para o post
Compartilhar em outros sites
primeira: como fazer raiz quadrada e fatorial

segunda: como converto de binario para oct?

 

System.out.println(Math.sqrt(34.9478));

 

long fatorial = 1;

long A = 5;

for(;A>1;A--)

fatorial*=A;

 

 

String bin = Integer.toBinaryString(1823); //Decimal p/ binário

String oc = Integer.toOctalString(Integer.valueOf(bin,2)); // Binário p/ octal

Compartilhar este post


Link para o post
Compartilhar em outros sites

System.out.println(Math.sqrt(34.9478));

 

long fatorial = 1;

long A = 5;

for(;A>1;A--)

fatorial*=A;

 

 

String bin = Integer.toBinaryString(1823); //Decimal p/ binário

String oc = Integer.toOctalString(Integer.valueOf(bin,2)); // Binário p/ octal

Muito obrigada!

como eu estava implementando as conversões com JRadioButton, fiz um pouco diferente.

mas deu tudo certinho!

Muito obrigada mesmo!!

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.